SPARQL:获取某个类的子类的所有实体
我必须在 SPARQL 中获取 C 类及其子类(直接或间接)的所有实例。
我可以通过这种方式获取 C 的所有直接子类:
SELECT ?entity
WHERE {
?subclass rdfs:subClassOf :C .
?entity rdf:type ?subclass .
}
但是我无法获取间接子类的实例,也无法获取 C 的任何实例
。据我所知(我已经预先计算了它们)所有子类(直接和C) 的间接查询,我可以构建一个动态查询,是否可以构建如下查询?
SELECT ?entity
WHERE {
?entity rdf:type in <list>.
}
谢谢大家。
编辑:
我刚刚解决了它,即使以一种不优雅的方式。
SELECT ?entity
WHERE {
{ ?entity rdf:type :C }
UNION { ?entity rdf:type :SubClass1 }
UNION { ?entity rdf:type :SubClass2 }
UNION { ?entity rdf:type :SubClass3 }
}
I've to get all the instances of a class C and subclasses (direct or indirect) of C, in SPARQL.
I can get all the direct subclasses of C in this way:
SELECT ?entity
WHERE {
?subclass rdfs:subClassOf :C .
?entity rdf:type ?subclass .
}
But I can't get the instances of an indirect subclass and neither any instance of C.
As I know (I've pre-calculated them) all the subclasses (direct and indirect of C), and I can build a dynamic query, is it possible build a query like the following one?
SELECT ?entity
WHERE {
?entity rdf:type in <list>.
}
Thanks to everyone.
EDIT:
I've just solved it, even if in a not elegant way.
SELECT ?entity
WHERE {
{ ?entity rdf:type :C }
UNION { ?entity rdf:type :SubClass1 }
UNION { ?entity rdf:type :SubClass2 }
UNION { ?entity rdf:type :SubClass3 }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更好的解决方案是使用 SPARQL 1.1 中的属性路径表达式,
这将被重写为:
A better solution is to use property path expressions in SPARQL 1.1
This would be rewritten as:
基于 SPARQL 1.1 规范 正确的方法将会是:
如果不支持属性路径,就无法表达任意长度的类层次结构。
Based on the SPARQL 1.1 specification the proper way to do it would be:
Without support for property paths there is no way of expressing class hierarchies of arbitrary length.
为了完整起见,现在,如果图表具有基本的 RDFS 推理(或更好)可用,您可以执行直接查询:
从技术上讲,这意味着不需要属性路径,但如今所有相关的 SPARQL 查询引擎(如果不是 RDF*)至少为 1.1符合。
如果使用预处理推理计算的引擎并根据类层次结构的深度,此解决方案通常比以前的解决方案更快。
For completeness, these days, if the graph has basic RDFS inferencing (or better) available, you can do a direct query:
Technically, this means no property path required, but all relevant SPARQL query engines today are at least 1.1 if not RDF* compliant.
If using an engine that preprocesses the inference calculations and depending on the depth of the class hierarchy, this solution is generally faster than the previous solutions.