Custom Functions in SPARQL with the Jena API

first time here. I was hoping someone could help me create custom SPARQL functions for use in the Jena API (ARQ). I need SPARQL to do some aggregation, and I know that it already implements avg, count, min, max and sum, but I also need to be able to standard deviation and median (I also need a range, but this can only be using min and max).

I was hoping the query might be similar to what you use for already implemented functions:

PREFIX example: <http://www.examples.com/functions#> PREFIX core: <http://www.core.com/values#> SELECT (stddev(?price) as ?stddev) WHERE { ?s core:hasPrice ?price } 

I donโ€™t know if this is possible or not, but if I need to use it, like other user-defined functions, it will also be good if it still gets the standard deviation of the results.

All I know is that the functions will be written in Java, which I already know pretty well. So I was wondering if anyone knew of a good way to do this or where to start looking for some advice. I tried looking for documentation on it, but there seems to be nothing. Any help would be greatly appreciated.

Thanks in advance.

+4
source share
3 answers

I'm not sure you can do what you want without changing the grammar.

SUM (...), for example, is a keyword defined by a SPARQL grammar:

The filter function or property function is probably not what you are looking for.

By the way, you also don't get STDDEV in SQL. Is it because two passes over the data are needed?

+2
source

Aggregate functions are a special case of SPARQL (and therefore ARQ) functions. I think that in ARQ it is not easy to expand the set of aggregate functions, while it is easy (and documented) to expand the set of filter functions and the function of functions. You could in any case calculate the standard deviation with something like this:

 PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#> PREFIX core: <http://www.core.com/values#> SELECT ( afn:sqrt( sum( (?price - ?avg) * (?price - ?avg) ) / (?count - 1) ) as ?stddev ) WHERE { ?s core:hasPrice ?price . { SELECT (avg(?price) as ?avg) (count(*) as ?count) WHERE { ?s core:hasPrice ?price } } } 

In any case, I have to use afn: sqrt, which is a โ€œproprietaryโ€ ARQ function, not in the SPARQL 1.1 draft, so this query will not work on frameworks other than Jena

+2
source

Yes, ARQ is expanding in various ways. the ARQ extensions page would be the best place to start.

0
source

All Articles