2

I successfully implemented sparql queries using custom ARQ functions using the following (custom function code):

public class LevenshteinFilter extends FunctionBase2 {

    public LevenshteinFilter() { super() ; }

    public NodeValue exec(NodeValue value1, NodeValue value2){
        LevenshteinDistance LD=new LevenshteinDistance();
        int i = LD.apply(value1.asString(), value2.asString()); 
        return NodeValue.makeInteger(i); 
    }
}

it works fine when I query against a Model loaded from a turtle file, like this:

InputStream input = QueryProcessor.class.getClassLoader().getResourceAsStream("full.ttl");
            model = ModelFactory.createMemModelMaker().createModel("default");
            model.read(input,null,"TURTLE"); // null base URI, since model URIs are absolute
            input.close();

with the query being sent like this :

String functionUri = "http://www.example1.org/LevenshteinFunction"; 
        FunctionRegistry.get().put(functionUri , LevenshteinFilter.class);

        String s = "whatever you want";
        String sparql = prefixes+" SELECT DISTINCT ?l WHERE { ?x rdfs:label ?l . " +  "FILTER(fct:LevenshteinFunction(?l, \"" + s + "\") < 4) }";                                              
        Query query = QueryFactory.create(sparql);
        QueryExecution qexec = QueryExecutionFactory.create(query, model); 
        ResultSet rs = qexec.execSelect();

However, if i use a working fuseki endpoint for the same dataset (full.ttl) like this :

fusekiUrl="http://localhost:3030/ds/query";

sending the query like this (using QueryExecutionFactory.sparqlService(fusekiUrl,query) instead of QueryExecutionFactory.create(query,model) ):

String functionUri = "http://www.example1.org/LevenshteinFunction"; 
        FunctionRegistry.get().put(functionUri , LevenshteinFilter.class);

        String s = "whatever you want";
        String sparql = prefixes+" SELECT DISTINCT ?l WHERE { ?x rdfs:label ?l . " + "FILTER(fct:LevenshteinFunction(?l, \"" + s + "\") < 4) }";                                       
        Query query = QueryFactory.create(sparql);
        QueryExecution qexec = QueryExecutionFactory.sparqlService(fusekiUrl,query); 
        ResultSet rs = qexec.execSelect();

Then I don't get any results back. In both cases I printed out the FunctionRegistry and they contain exactly the same entries, especially :

key=http://www.example1.org/LevenshteinFunction value: org.apache.jena.sparql.function.FunctionFactoryAuto@5a45133e

Any clue ?

Thanks

Back on that question that I finally solved. There was several issues, one which is the fact that (obviously !!) Remote endpoint and client are running on different jvms.

To get the thing working, do the following (for a stupid MyFilter custom function - i.e strlen) :

1) Deploy the custom functions classes jar on fuseki server

2) Modify the fuseki config :

add [] ja:loadClass "my.functions.package.MyFilter"

where MyFilter implementation is :

import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.function.FunctionBase1;


public class MyFilter extends FunctionBase1 {

public MyFilter() { super() ; }

public NodeValue exec(NodeValue value1){

        int d = value1.asString().length();
        return NodeValue.makeInteger(new Integer(d)); 
    }
}

3) add the following prefix to the context:

PREFIX f: <java:my.functions.package.>

Note that "my.functions.package." is the package of MyFilter class, not the class itself --> this means that you never call a class method in sparql queries but only a class that implements org.apache.jena.sparql.function.FunctionBaseX where X is the number of argument of your filter function

4) Write (for instance) the query like this:

SELECT DISTINCT ?l
WHERE { ?x skos:prefLabel ?l .  
  FILTER (f:MyFilter(?l) < 20) 
}

EDIT: step 2) is not necessary

Marc Agate
  • 61
  • 1
  • 4
  • Fuseki needs the UDF class in the classpath, i.e. you have to build a JAR file and it this to the classpath of Fuseki. – UninformedUser Dec 26 '17 at 16:37
  • Closed . See https://lists.apache.org/thread.html/e9a120ef8a3fc1c86f224d365437b5af8922cd93aff7d34cbda9429d@%3Cusers.jena.apache.org%3E for details – Marc Agate Dec 26 '17 at 21:27

0 Answers0