[英]Protégé: Différent results between DL Query and SPARQL Query
Here is a small ontology called wildlife.owl
, created by Protégé, in which I have classes animal
, carnivore
, herbivore
, lion
, giraffe
, and individuals Léo
(a lion
), Gigi
(a giraffe
) and Giginou
(also a giraffe
).这里是一个小本体叫wildlife.owl
,通过被保护人在我所创建类animal
, carnivore
, herbivore
, lion
, giraffe
,和个人Léo
(一lion
), Gigi
(一giraffe
)和Giginou
(也是giraffe
)。 In the ontology I only declare that lion ⊏ carnivore ⊏ animal
.在本体中我只声明lion ⊏ carnivore ⊏ animal
。
When I ask for the instances of animal
in the DL Query tab of Protégé, I get, among others, Léo
(which is a lion
, hence a carnivore
, hence an animal
).当我在 Protégé 的 DL Query 选项卡中询问animal
的实例时,我得到,其中包括Léo
(它是lion
,因此是carnivore
,因此是animal
)。
But when I write the following SPARQ Query:但是当我编写以下 SPARQ 查询时:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX me: <file:wildlife.owl#>
SELECT ?b
WHERE { ?b rdf:type me:animal }
I don't get any instance.我没有得到任何实例。 Same result when I replace me:animal
by me:carnivore
.当我用me:carnivore
替换me:animal
,结果相同。 Only when I replace it by me:lion
I get the desired result Léo
.只有当我用me:lion
替换它时, me:lion
才能得到想要的结果Léo
。
Why is DL Query doing the inference (allowing me to obtain Léo
as instance of the animal
class) and not SPARQL Query?为什么 DL Query 进行推理(允许我获取Léo
作为animal
类的实例)而不是 SPARQL Query?
What can I do to get the same result in SPARQL Query?我该怎么做才能在 SPARQL 查询中获得相同的结果?
My next question concerns Python: when I send the SPARQL Query using Owlready2 and RDFlib, again I get no result:我的下一个问题与 Python 相关:当我使用 Owlready2 和 RDFlib 发送 SPARQL 查询时,我再次没有得到任何结果:
from owlready2 import * from rdflib import * onto = get_ontology("wildlife.owl").load() sync_reasoner([onto]) graph = default_world.as_rdflib_graph() print(list(graph.query_owlready(""" PREFIX rdf-syntax: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX me: <file:wildlife.owl#> SELECT ?b WHERE { ?b rdf-syntax:type me:animal . }""")))
How can I get this query use OWL Reasoner?如何使用 OWL Reasoner 获取此查询?
When calling the reasoner, Owlready does not retain trivia inferences, such as is-a transitivity (eg the fact that lion is an animal).在调用推理器时,Owlready 不保留琐碎的推理,例如 is-a 传递性(例如,狮子是动物的事实)。
For trivial inferences, you should rather use SPARQL, as in the example below.对于琐碎的推理,您应该使用 SPARQL,如下例所示。 The ?any_animal
variable contains all animal subclasses (including animal
itself), thank to the SubclassOf*
SPARQL syntax (the *
indicates transitivity). ?any_animal
变量包含所有动物子类(包括animal
本身),这要?any_animal
SubclassOf*
SPARQL 语法( *
表示传递性)。 Then, we take any instance of ?any_animal
class.然后,我们采用?any_animal
类的任何实例。
from owlready2 import *
from rdflib import *
onto = get_ontology("http://test.org/wildlife.owl")
with onto:
class animal(Thing): pass
class carnivore(animal): pass
class lion(carnivore): pass
lion()
default_world.graph.dump()
graph = default_world.as_rdflib_graph()
print(list(graph.query_owlready("""
PREFIX rdf-syntax: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX me: <http://test.org/wildlife.owl#>
SELECT ?b WHERE {
?any_animal <http://www.w3.org/2000/01/rdf-schema#subClassOf>* me:animal .
?b rdf-syntax:type ?any_animal .
}""")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.