我有一个关于 SPARQL 的问题。我有一个动物本体:
Animals (is a superclass with object property <hasColor>)
------ Mammals (subclass of Animals)
------------- Dog (subclass of Mammals)
---------------- dog1 (a instance with property <hasColor>="white")
---------------- dog2 (a instance with property <hasColor>="red" )
------ Bird (subclass of Animals)
是否可以使用 SPARQL 找到“所有动物,即‘白色’”或“所有动物实例”?倒退:我怎么知道一个实例(dog1)是否属于动物?
注意 : 类层次结构的深度和广度是事先未知的。
下面的查询也不起作用
SELECT ?x WHERE {?x rdfs:subClassOf :Animals . ?x :hasСolor "white"}
下一个查询(查找所有“白色”的动物)仅在类层次结构的深度已知时才有效。
(因此,如果层次结构已知,我是否可以执行指定的步骤(从层次结构的顶部到底部)以达到目标:在这种情况下为 2 个步骤。
SELECT ?z WHERE {
?x rdfs:subClassOf :Animals .
?y rdfs:subClassOf ?x .
?z rdf:type ?y .
?z :hasColor "white"
}
下一个例子也是如此——“找到所有的动物实例”
SELECT ?z WHERE {
?x rdfs:subClassOf :Animals .
?y rdfs:subClassOf ?x .
?z rdf:type ?y .
}
如果层次结构未知,该怎么办?
查询将使用 处理深发展 (是 Jena 的一个组件)。
我想要类似的东西:
select ?x where {?x rdfs:subClassOf :Animals . ?x :hasСolor "white"})
更新。 “查找所有‘白色’动物(实例)”的解决方案可能如下所示:
SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals . ?y rdf:type ?x . ?y :hasColor "white"}
最佳答案
您可以在 SPARQL 查询中使用传递性(使用 *):
SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals .
?y rdf:type ?x .
?y :hasColor "white" }
关于semantics - SPARQL:如何获取本体的实例,类层次结构的深度未知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9942230/