Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use user defined functions in fuseki?

I have a Fuseki endpoint running on some server. I would like to pass user defined functions using Jena's com.hp.hpl.jena.sparql.function library. Unfortunately, I get the error:

URI <java:path.to.functions.halfString> has no registered function factory

I made sure to add the class (the jar containing the file) to the classpath and I can access this class from other applications that use this class on that server.

The example case that I am trying for now is some function that takes the subject of all the triples in the graph and returns the first half of each subject.

As a reference, I added the function below:

public class halfString extends FunctionBase1
{
    public halfString() { super() ; }

    public NodeValue exec(NodeValue nv1)
    {
        if (!nv1.isString())
        {
            return nv1;
        }

        String hey = nv1.toString();
        int mid = hey.length() / 2;
        String nay = hey.substring(0, mid);

        return NodeValue.makeString(nay);
    }
}

Here is the SPARQL query that I used:

PREFIX f: <path.to.functions.>

SELECT ?half ?s ?o ?g
WHERE {

    ?s ?p ?o

    BIND (f:halfString(str(?s)) as ?half)
}

Running Fuseki (using the default configuration that was given with fuseki):

cd FUSEKI_HOME
./fuseki-server --mem /ds
like image 954
ybt195 Avatar asked Nov 01 '22 16:11

ybt195


1 Answers

The problem is not with Fuseki. The java documentation states that when using the java command with the -jar option,

the JAR file is the source of all user classes, and other user class path settings are ignored.

Simply adding the jar file with the user defined functions to the CLASSPATH variable will not solve the problem as that environment variable will be ignored. Additionally, using the --classpath or -cp option will also be ignored.

In order for Fuseki to be able to load the jar files, you need to add the location to your jar file with your user defined functions to the Class-Path key in the fuseki-server.jar's manifest file.

To do this, run:

jar umf manifest-file fuseki-server.jar

manifest-file:

Class-Path: path/to/functions/udf.jar

As reference, this describes the process of adding classes to a jar files class path in more detail.

like image 196
ybt195 Avatar answered Nov 08 '22 08:11

ybt195