Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 939
Sam Avatar asked Dec 20 '11 01:12

Sam


2 Answers

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.

like image 108
RobV Avatar answered Nov 11 '22 16:11

RobV


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.

like image 40
Jörn Hees Avatar answered Nov 11 '22 14:11

Jörn Hees