SPARQL:如何获取本体的实例,类层次结构的深度是未知的?

时间:2012-03-30 11:41:09

标签: semantics sparql jena ontology

我对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"}

下一个查询(查找所有动物,即'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 .
}

如果hierarchie不知道该怎么办?

查询将使用 SDB Jena 的组件)进行处理。

我想要的东西: select ?x where {?x rdfs:subClassOf :Animals . ?x :hasСolor "white"})

UPD。“查找所有动物(实例),即'白色'”的解决方案可能如下所示:

  

SELECT?y WHERE {?x rdfs:subClassOf *:动物。 ?rdf:输入?x。 ?ÿ   :hasColor“white”}

1 个答案:

答案 0 :(得分:12)

您可以在SPARQL查询中使用传递性(使用*):

SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals . 
                  ?y rdf:type ?x . 
                  ?y :hasColor "white" }