Binding variable inside custom function

Is there any way/example about how to access binding variables inside my custom function?

I do not want to pass as parameter but access the binding variables inside the function I am defining.

I want to access all binding variables in the method:

protected Value internalEvaluate(final Value… theArgs) throws ExpressionEvaluationException

Would that be possible?


I don’t think that’s possible with a function. You can’t really access much other than the arguments. I believe that you can do what you’re looking for with a custom property function but they are significantly more complex. I’m still working working out all the details of how it’s done but I’d be happy to have someone else asking questions on how it’s done do help me figure it out.

What exactly are you looking to accomplish?

As I mentioned above, I want to access the bind variables inside my function definition, dynamically.

Is there any repo with an example of a custom property function ? This may be an alternative to what I need to do.

We do have an example of a custom property function in our examples repo

I meant, what are you trying to accomplish with your function? If you can explain what your goal is someone might be able to suggest a better way to get there.

There is an example in the stardog-examples repo under the pfExample branch. I’m guessing that the reason it’s not in the main branch is that unlike custom functions property functions give you a lot of access and there is a lot of room to mess things up. It’s not exactly a secret that it’s there so I hope no one minds my pointing out but I will repeat the warning one more time, property functions are a loaded gun pointed at your foot. Proceed with caution.

I want to have a way to evaluate an complex expression at query time.

For example:
Suppose I have the triple
foo:1 foo:formula “?a + ?b” .
foo:1 foo:a “10” .
foo:1 foo:b “14” .

I want to evaluate ?a + ?b at query time.

Select ?value where {
?s foo:formula ?formula .
foo:1 foo:a ?a .
foo:1 foo:b ?b .

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

Notice that the formula is any expression and the number of variable is not constant.

I am trying to achieve it via a custom function but apparently it forces me to have a fix number of parameter.

Any idea how to achieve it?

I can’t think of a way to do exactly what you’re asking for but can you step back one level higher and explain why you need to evaluate functions this way?

Ok, I think there may be a way but it’s completely hackish and I encourage you to include more details about why you need that functionality because I suspect that there is a much better way to accomplish what you’re looking to do but for the sake of giving as complete an answer as possible here goes…

Select ?value where {
?s foo:formula ?formula .
foo:1 foo:a ?a .
foo:1 foo:b ?b .

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

The above query doesn’t quite make sense but I think I get what you’re after. You want to execute the ?formula of ?s with whatever arguments are listed on ?s

Assume you have the following data

:myEval1 :formula "a + b" ;
              :a 1 ;
              :b 2 .

:myEval2 :formula "a * b * c" ;
              :a 1 ;
              :b 2 ;
              :c 3 .

:a a :Arg .
:b a :Arg .
:c a :Arg .

You could write a function that takes the first argument as the function and the second argument a comma separated dictionary of arguments and evaluate it with a query that looks like this

select myFunc(?formula, ?args) where {
    select ?s (group_concat(concat(str(?p), '=', str(?o)); separator=',') as ?args) where { ?s ?p ?o . ?p a :Arg } group by ?s 
  ?s :formula ?formula

the inputs to the function should look like this

function     args
a + b,
a * b * c,,

Your function would need to be able to parse the argument string and evaluate the function. You might also be able to use an aggregate function.