在SPARQL中删除不需要的超类答案 [英] Removing unwanted superclass answers in SPARQL
问题描述
我有一个包含分类学层次结构的OWL文件,我想编写一个查询,其中答案包括每个人及其直系分类学父级.这是一个示例(完整的查询更加混乱).
I have an OWL file that includes a taxonomic hierarchy that I want to write a query where the answer includes each individual and its immediate taxonomic parent. Here's an example (the full query is rather messier).
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http:://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <urn:ex:> .
:fido rdf:type :Dog .
:Dog rdfs:subClassOf :Mammal .
:Mammal rdfs:subClassOf :Vertebrate .
:Vertebrate rdfs:subClassOf :Animal .
:fido :hasToy :bone
:kitty rdf:type :Cat .
:Cat rdfs:subClassOf :Mammal .
:kitty :hasToy :catnipMouse .
此查询可以满足我的要求.
And this query does what I want.
prefix rdf: <http:://www.w3.org/1999/02/22-rdf-syntax-ns#> .
prefix : <urn:ex:> .
SELECT ?individual ?type
WHERE {
?individual :hasToy :bone .
?individual rdf:type ?type .
}
问题在于,我宁愿使用OWL文件的合理版本,当然也包括其他语句:
The problem is that I'd rather use a reasoned-over version of the OWL file, which unsurprisingly includes additional statements:
:fido rdf:type :Mammal .
:fido rdf:type :Vertebrate .
:fido rdf:type :Animal .
:kitty rdf:type :Mammal .
:kitty rdf:type :Vertebrate .
:kitty rdf:type :Animal .
现在查询产生有关Fido是哺乳动物等的其他答案.我可以放弃使用文件的合理版本,或者,由于SPARQL查询是从Java调用的,因此我可以做很多其他查询以查找出现的最小包容性类型.我的问题是,是否只有返回Dog方法才有合理的纯SPARQL解决方案.
And now the query results in additional answers about Fido being a Mammal, etc. I could just give up on using the reasoned version of the file, or, since the SPARQL queries are called from java, I could do a bunch of additional queries to find the least inclusive type that appears. My question is whether there is a reasonable pure SPARQL solution to only returning the Dog solution.
推荐答案
一个通用的解决方案是确保您只要求使用 direct 类型.如果满足以下条件,则类C
是实例X
的直接类型:
A generic solution is that you make sure you ask for the direct type only. A class C
is the direct type of an instance X
if:
-
X
的类型为C
- 没有
C'
这样的:-
X
的类型为C'
-
C'
是C
的子类
-
C'
不等于C
-
X
is of typeC
- there is no
C'
such that:X
is of typeC'
C'
is a subclass ofC
C'
is not equal toC
(顺便说一句,最后一个条件是必要的,因为在RDF/OWL中,子类关系是自反的:每个类都是其自身的子类)
(that last condition is necessary, by the way, because in RDF/OWL, the subclass-relation is reflexive: every class is a subclass of itself)
在SPARQL中,它变成这样:
In SPARQL, this becomes something like this:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <urn:ex:> .
SELECT ?individual ?type
WHERE {
?individual :hasToy :bone .
?individual a ?type .
FILTER NOT EXISTS { ?individual a ?other .
?other rdfs:subClassOf ?type .
FILTER(?other != ?type)
}
}
根据用于执行这些查询的API/三重存储/库,还可能存在其他特定于工具的解决方案.例如,Sesame API(公开:我在Sesame开发团队中)可以选择出于单个查询的目的而禁用推理:
Depending on which API/triplestore/library you use to execute these queries, there may also be other, tool-specific solutions. For example, the Sesame API (disclosure: I am on the Sesame dev team) has the option to disable reasoning for the purpose of a single query:
TupleQuery query = conn.prepareTupleQuery(SPARQL, "SELECT ...");
query.setIncludeInferred(false);
TupleQueryResult result = query.evaluate();
Sesame还提供了一个可选的附加推断器(称为直接类型推断器"),它引入了可以查询的附加虚拟"属性,例如sesame:directType
,sesame:directSubClassOf
等.其他工具无疑也具有相似的选项.
Sesame also offers an optional additional inferencer (called the 'direct type inferencer') which introduces additional 'virtual' properties you can query, such as sesame:directType
, sesame:directSubClassOf
, etc. Other tools will undoubtedly have similar options.
这篇关于在SPARQL中删除不需要的超类答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!