How to query for all direct subclasses in SPARQL?

我怕爱的太早我们不能终老 提交于 2019-12-12 02:27:34

问题


I have A, B and C as classes related by the transitive property isSubClassOf. So A isSuclassOF B and B isSubClassOf C. So by inference we have A isSubClassOf C.

My question: How can I write a SPARQL query to just return back for each Class its direct only subclass number. for example

A    0 
B    1
C    1  

回答1:


Within the standard SPARQL language, you can do this by querying for those subclasses where no other subclass exists "in between", like so:

 SELECT ?directSub ?super 
 WHERE { ?directSub rdfs:subClassOf ?super .
         FILTER NOT EXISTS { ?otherSub rdfs:subClassOf ?super. 
                             ?directSub rdfs:subClassOf ?otherSub .
                             FILTER (?otherSub != ?directSub)
         }
 }

If you want to count the number of subclasses, you will need to adapt the above query using the COUNT and GROUP BY operators.

Many SPARQL engines offer some shortcuts for querying direct subclasses, however. For example in Sesame, when querying programmatically, you can disable inferencing for the duration of the query by setting a boolean property on the Query object to false. It also offers an additional reasoner which can be configured on top of a datastore and which allows you to query using a "virtual" property, sesame:directSubClassOf (as well as sesame:directType and sesame:directSubPropertyOf).

Other SPARQL engines have similar mechanisms.



来源:https://stackoverflow.com/questions/23699246/how-to-query-for-all-direct-subclasses-in-sparql

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