SPARQL Query Error with OPTION(TRANSITIVE) on Jena

2019-05-23 10:56发布

问题:

I have the following Query

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?type
WHERE
{
   {
      SELECT *
      WHERE
      {
           ?x rdfs:subClassOf ?type .
      }
   }
   OPTION (TRANSITIVE, t_distinct, t_in (?x), t_out (?type) ) .
   FILTER (?x = <http://dbpedia.org/ontology/Hospital>)
}

It works fine when i send it to Virtuoso endpoint but does not work on my Jena instance. In specific i get the following error:

INFO  [1] 400 Parse error: 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?type
WHERE
{
   {
      SELECT *
      WHERE
      {
           ?x rdfs:subClassOf ?type .
      }
   }
   OPTION (TRANSITIVE, t_distinct, t_in (?x), t_out (?type) ) .
   FILTER (?x = <http://dbpedia.org/ontology/Hospital>)
}
Lexical error at line 12, column 39.  Encountered: " " (32), after : "OPTION" (17 ms)

In case this a Virtuoso specific function, I would appreciate to know an equivalent for this query that would work with *Jena/Standard SPARQL). The expected output should be:

http://dbpedia.org/ontology/Building
http://dbpedia.org/ontology/ArchitecturalStructure
http://dbpedia.org/ontology/Place
http://dbpedia.org/ontology/d0:Location

which represents all superclasses for "Hospital"

回答1:

This is the expected behavior. This part of the query:

OPTION (TRANSITIVE, t_distinct, t_in (?x), t_out (?type) ) 

is not standard SPARQL 1.1 but it is a Virtuoso specific extension.

Jena is a SPARQL 1.1 compliant implementation.

The following query does the same thing using standard SPARQL 1.1 syntax, and should work with both Fuseki and Virtuoso (just tested on the dbpedia endpoint and got the same result):

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?type
WHERE
{
  {
   SELECT *
   WHERE
    {
       ?x rdfs:subClassOf+ ?type .
    }
  }
  FILTER (?x = <http://dbpedia.org/ontology/Hospital>)
}

The feature used is the "property path".

See http://www.w3.org/TR/sparql11-query/