I want to write a property function extension sparql with arq jena, how can I write? Request:
SELECT *
WHERE {?Person f:Next(name) ?x.}
my function code:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.jena.atlas.logging.Log;
import org.apache.jena.graph.Node;
import org.apache.jena.query.QueryBuildException;
import org.apache.jena.query.QueryException;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.sparql.engine.ExecutionContext;
import org.apache.jena.sparql.engine.QueryIterator;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.engine.iterator.QueryIterNullIterator;
import org.apache.jena.sparql.pfunction.PFuncSimple;
import org.apache.jena.sparql.pfunction.PFuncSimpleAndList;
import org.apache.jena.sparql.pfunction.PropFuncArg;
import org.apache.jena.sparql.pfunction.PropertyFunction;
import org.apache.jena.sparql.pfunction.PropertyFunctionFactory;
import org.apache.jena.sparql.util.IterLib;
public class Next implements PropertyFunctionFactory {
@Override
public PropertyFunction create(final String uri)
{
return new PFuncSimple()
{
@Override
public QueryIterator execEvaluated(final Binding parent, final Node subject, final Node predicate, final Node object, final ExecutionContext execCxt)
{
Model model = ModelFactory.createDefaultModel();
InputStream is = null;
try {
is = new BufferedInputStream(
new FileInputStream( "C:\\\\fichier rdf/journal.webscience.org-vivo.rdf"));
} catch (FileNotFoundException ex) {
Logger.getLogger(haschild.class.getName()).log(Level.SEVERE, null, ex);
}
model.read(new InputStreamReader(is), "");
StmtIterator iter = model.listStatements();
extract the sebject , predicate and object from rdf:
for (;iter.hasNext();) {
Statement stmt = iter.nextStatement();
Resource sub = stmt.getSubject();
Property pred = stmt.getPredicate();
RDFNode obj = stmt.getObject();
comparison the suject and predicate of the rdf with subject and predicate of the request
if ((sub.toString().equals(subject.toString()))|| (pred.toString().equals(predicate.toString())))
return new QueryIterPlainWrapper ((Iterator<Binding>) obj,execCxt);
}
return null;
}
};
}
}
and how i can register my function
Property functions look in syntax like regular properties. There isn't a special syntax.
?Person :somePropertyFunction ?x .
The property function has access to the subject and object of the triple pattern. There is also help for when subject or object are an RDF list.
Usually the arguments go as the object or object list and results are as a subject or subject list.
You shouldn't need to use .toString
.
You can't cast obj
to an Iterator<Binding>
.
Take a look at some existing property functions. splitIRI
is a simple one. concat
might be useful to look at - it takes a list of argument (object position) and returns a subject (variable).