SPARQL Querying Transitive
Asked Answered
C

2

11

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.
Caban answered 20/12, 2011 at 1:36 Comment(0)
T
15

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.

Tantalum answered 20/12, 2011 at 6:51 Comment(2)
Thanks again for the help. Going to definitely check out the link on SPARQL syntaxCaban
If the thing you're querying supports OWL reasoning, specifically a profile of OWL that includes transitivity and you've defined the property as a TransitiveProperty, then you don't need any special SPARQL syntax, you can just use the property normally and the reasoner will do the right thing.Weekender
C
5

While RobV's answer is correct in your case, I think the bi-directional nature of owl:sameAs is worth mentioning.

Let's extend your example by this:

:a owl:sameAs :d.
:e owl:sameAs :d.

In that case a simple owl:sameAs+ would not suffice to find :e, so maybe use something like (owl:sameAs|^owl:sameAs)+ to find the whole equivalence tree. Be aware that depending on the endpoint this might cause loops.

Also there might be implementation specific extensions to handle owl:sameAs reasoning, such as in Virtuoso:

DEFINE input:same-as "yes"
select * where { :a ?p ?o. }

returning also ?p and ?o that are issued for :b, :c, :d and :e.

Contend answered 29/5, 2012 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.