SPARQL Querying Transitive

最后都变了- 提交于 2019-12-12 07:26:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!