SPARQL 查询以查找给定类的所有子类和超类 [英] SPARQL query to find all sub classes and a super class of a given class

查看:71
本文介绍了SPARQL 查询以查找给定类的所有子类和超类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 SPARQL 查询来查找给定类的超类/子类.

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

例如,给定 http://139.91.183.30:9090/RDF/VRP/Examples/Phenomenon.rdf RDFS词汇文件,我想找到'AcousticWave'(也就是'Wave')的超类.

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').

同样,如果用户输入Wave",我想获取Wave"的所有子类(即AcousticWave"、GravityWave"、InternalWave"和Tide").

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

我将如何编写这样的 SPARQL 查询?

How would I write such SPARQL query?

推荐答案

rdfs 中用于状态子/超类关系的谓词是 rdfs:subClassOf.考虑到这一点,您只需要在您的 SPARQL 查询中编写三重模式,将谓词和您想要匹配的主题或对象绑定起来 --- AcousticWave 在您的情况下.

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.

对于超级类...

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 . }

对于子类...

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

如果您想检索 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. 递归地遍历这些查询,直到收集到闭包.
  2. 通过 RDF/RDFS 推理器传递您的 RDF 数据以转发所有蕴涵并在您的 RDF 数据库中断言这些.

这篇关于SPARQL 查询以查找给定类的所有子类和超类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆