Evaluating strings in sparql

Hi all, is it possible to evaluate a string in sparql?

Example:

select ?value where {
?s foo:frequency ?a .
?s foo:formula ?formula.

bind( eval (?formula) as ?value)
}

Where the ?formula would be a string like this:
?formula = "?a + 10" (it is store in the database as a string)

I want to evaluate the expression ?a + 10 during execution time.

Is that functionality available?

Best,

Out of the box you can't do something like that but Stardog makes it easy to write user defined functions that could do something like that. See this example.

Your query would need to look a little bit different like the following:

select ?value where {
?s foo:frequency ?a .
?s foo:formula ?formula.

bind( myfunc:eval (?formula, ?a) as ?value)
}

I was working on writing a user defined function that would execute javascript expressions that would be similar to this but haven't gotten around to finishing it. I might be motivated to finish it if it was something that you'd find useful :wink:

PS I'm assuming you're looking for something generic and want to use some additional logic for selecting the formula and/or have a large number of formulas. You can evaluate basic arithmetic expressions directly in SPARQL.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.