Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom calculator function #612

Open
ch-sander opened this issue Aug 6, 2024 · 5 comments
Open

Custom calculator function #612

ch-sander opened this issue Aug 6, 2024 · 5 comments

Comments

@ch-sander
Copy link

Suppose I want to display certain mathematical functions as a value in a separate column in the query result that cannot be defined dynamically by the user in the aggregate widget. Can I define a class with a SPARQL query that performs the calculation and returns the result as a number?

Imagine I want the logarithm or some specific fraction calculated on the basis of some domain URI, e.g. the number of sentences divided by the number of pages in some book. The
BOOK URI would be the domain and SENTENCES PER PAGE the property, the result a XSD:DECIMAL. So, the table should have the columns BOOK and SENTENCES PER PAGE.

@ch-sander
Copy link
Author

Pseudocode

PREFIX grace: <http://www.graceful17.org/ontology/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?book ?sentences ?pages ?sentencesPerPage WHERE {
  ?book a grace:Book ;
        grace:numberOfSentences ?sentences ;
        grace:numberOfPages ?pages .
  
  BIND(xsd:decimal(?sentences) / xsd:decimal(?pages) AS ?sentencesPerPage)
}

@ch-sander
Copy link
Author

ch-sander commented Aug 6, 2024

I might be wrong but custom data sources seem to cover only object properties, not literals, as range.

But I imagine that there is a runtime variable that could bind ?sentencesPerPage so that a numeral being returned as literal.

@tfrancart
Copy link
Contributor

Suppose I want to display certain mathematical functions as a value in a separate column in the query result that cannot be defined dynamically by the user in the aggregate widget. Can I define a class with a SPARQL query that performs the calculation and returns the result as a number?

No. This is clearly out of scope of what Sparnatural can do. We are not trying to cover 100% of SPARQL; there is currently no plan to support this.

I might be wrong but custom data sources seem to cover only object properties, not literals, as range.
But I imagine that there is a runtime variable that could bind ?sentencesPerPage so that a numeral being returned as literal.

Yes, you can write a custom datasource that returns a literal. And yes, you can use a BIND and do some calculation in this custom datasource. But this will not allow a user to do this calculation in the final query.
The documentation on datasources does not cover how to return a literal, and it needs to be updated. I will create a separate issue. To do this the SPARQL query needs to return the variables ?value and ?label instead of ?uri and ?label

@ch-sander
Copy link
Author

I think I found the function in the JavaScript that replaces $this with the sparqlString for a class or property, but I couldn't find a function that allowed to change the syntax of the pattern to allow for a BIND statement so that the original object of the triple is bound.

I also realized that in the yasgui plugins library exist many custom calculation functions that don't seem to be included in the aggregate widget either, but I didn't look closely, tbh.

@ch-sander
Copy link
Author

Great.
I didn't mean to allow the user to define the function or calculation but just to call it via UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants