SPARQL query to find all sub classes and a super class of a given class

爷,独闯天下 提交于 2019-12-06 04:30:50

问题


I need to write a SPARQL query to find a superclass/subclasses of a given class.

For example, given http://139.91.183.30:9090/RDF/VRP/Examples/Phenomenon.rdf RDFS vocabulary file, I want to find the superclass of 'AcousticWave' (which is 'Wave').

Similarly if user enters 'Wave', I want to get all sub classes of 'Wave' (which are 'AcousticWave', 'GravityWave', 'InternalWave' and Tide').

How would I write such SPARQL query?


回答1:


The predicate used in rdfs for state sub/super class relationships is rdfs:subClassOf. With that in mind you just need to write triple patterns in your SPARQL query that bind that predicate and the subject or object that you want to match --- AcousticWave in your case.

I hope that the following queries are self-explanatory.

for super classes ...

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ns: <http://www.domain.com/your/namespace/>

SELECT ?superClass WHERE { ns:AcousticWave rdfs:subClassOf ?superClass . }

And for sub classes ...

SELECT ?subClass WHERE { ?subClass rdfs:subClassOf ns:Wave . }

If you want to retrieve the labels for every subclass of ns:Wave you would do something like ...

SELECT ?subClass ?label WHERE { 
        ?subClass rdfs:subClassOf ns:Wave . 
        ?subClass rdfs:label ?label . 
}

If you need the transitive closure of sub/super classes then you have two options:

  1. Iterate recursively over these queries until you have collected the closure.
  2. Pass your RDF data through a RDF/RDFS reasoner to forward chain all entailments and assert these in your RDF database.


来源:https://stackoverflow.com/questions/7557564/sparql-query-to-find-all-sub-classes-and-a-super-class-of-a-given-class

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