Search code examples
mathsparqljenalogarithm

Logarithm Function in SPARQL Query


I am trying to create a SPARQL query that performs the logarithm function on the returned results. I have implemented the Jena SPARQL engine in my java program, but have only been able to find these available functions : http://jena.sourceforge.net/ARQ/library-function.html

Does anybody know of a way to take the logarithm (preferably the natural log) of a SPARQL return variable?

Example query that works:

SELECT DISTINCT ((?Transactions_Num) AS ?BusinessValue) 
WHERE {{?BusinessProcess relation:Transactions_Num ?Transactions_Num ;} }

Example of query that I want to work (though currently does not):

SELECT DISTINCT (LOG(?Transactions_Num) AS ?BusinessValue) 
WHERE {{?BusinessProcess relation:Transactions_Num ?Transactions_Num ;} }

Thanks you very much for the help in advance!


Solution

  • Log isn't part of the standard or ARQ's additions, however it's very easy to write your own.

    package app;
    
    public class log extends FunctionBase1
    {
        public log() { super() ; }
    
        public NodeValue exec(NodeValue v)
        {
            return Math.log(v.getDouble());
        }
    }
    

    The easiest way to register it is like this:

    FunctionRegistry.get().put("http://example.org/function#log", log.class) ;
    

    You can then use it like this:

    PREFIX myfun: <http://example.org/function#>
    SELECT DISTINCT (myfun:log(?Transactions_Num) AS ?BusinessValue)
    {
       ...
    }