Search code examples
rdfsparqljenaowlinference

SPARQL Querying Transitive


I am a beginner to SPARQL and was wondering if there was a query which could help me return transitive relations. For example the n3 file below I would want a query that would return "a is the sameas c" or something along those lines. Thanks

@prefix : <http://websitename.com/links/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

:a owl:sameas :b.
:b owl:sameas :c.

Solution

  • You can use property paths if you are using a suitably enabled SPARQL 1.1 engine, you've tagged your question Jena so I assume you are using its ARQ engine which supports this feature.

    So you can write a query like the following:

    PREFIX owl: <http://www.w3.org/2002/07/owl#>
    SELECT *
    WHERE
    {
      ?x owl:sameAs+ ?y
    }
    

    Note the + after the predicate, used to indicate that it should look for relationships composed of one/more steps.

    The syntax for property paths can be found here and is very regular expression like. The only downside of queries using this is that you don't get any information about how long the paths are or what the intermediate nodes are.