问题
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.
回答1:
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.
回答2:
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
.
来源:https://stackoverflow.com/questions/8569810/sparql-querying-transitive