Search code examples
sparqljenapropertypath

SPARQL Query with property path not working


I want to make a query that does the following: Select all triples (s,p,o) if there exists a path with the length of at least 2 edges from s to o with the property p. So all edges of the path have to be labelled with p. I tried the following:

select  ?s <http://dbpedia.org/ontology/isPartOf> ?o
WHERE { 
?s <http://dbpedia.org/ontology/isPartOf>{2,} ?o.
?s <http://dbpedia.org/ontology/isPartOf> ?o 
}

I executed it with the Jena API:

ParameterizedSparqlString parameterizedSparql = new ParameterizedSparqlString(model);
parameterizedSparql.setCommandText(sparql);
Query query = QueryFactory.create(parameterizedSparql.asQuery().toString(), Syntax.syntaxARQ);
QueryExecutionFactory.create(query, model).execSelect();

I used Syntax.syntaxARQ so that it should understand property paths.

It gives me the following error:

Exception in thread "main" org.apache.jena.query.QueryParseException: Encountered " "{" "{ "" at line 3, column 42.
Was expecting one of:
<IRIref> ...
<PNAME_NS> ...
<PNAME_LN> ...
<BLANK_NODE_LABEL> ...
<VAR1> ...
<VAR2> ...

Can you please show me how I can make the query correctly?


Solution

  • Also, as @AKSW noted, the {2,} syntax from the SPARQL 1.1 Working Draft didn't make it into the final SPARQL 1.1 spec, so you can't rely on it being supported by every SPARQL processor.

    You can use the {2,} syntax with Virtuoso, which is the engine powering the public DBpedia endpoint, but to do so through Jena, you have to either use "extended syntax" (Syntax.syntaxARQ) or bypass the ARQ parser.

    It appears that your immediate issue comes down to a bug in Jena, where ParameterizedSparqlString.asQuery() does not currently support "extended syntax" (Syntax.syntaxARQ) queries; parameterizedSparql.toString() should be sufficient, as commented by @AndyS.