Python 使用RDFLib获取SPARQL查询中的相交类
我有一个类Python 使用RDFLib获取SPARQL查询中的相交类,python,sparql,rdf,owl,rdflib,Python,Sparql,Rdf,Owl,Rdflib,我有一个类ABC,它是类XYZ的子类,定义为类a,B,C的交叉点,如下所示: <Class rdf:about="&clink;ABC"> <equivalentClass> <Class> <intersectionOf rdf:parseType="Collection"> <Restriction> &l
ABC
,它是类XYZ
的子类,定义为类a
,B
,C
的交叉点,如下所示:
<Class rdf:about="&clink;ABC">
<equivalentClass>
<Class>
<intersectionOf rdf:parseType="Collection">
<Restriction>
<onProperty rdf:resource="&clink;affects"/>
<someValuesFrom rdf:resource="&clink;A"/>
</Restriction>
<Restriction>
<onProperty rdf:resource="&clink;hasMarker"/>
<someValuesFrom rdf:resource="&clink;B"/>
</Restriction>
<Restriction>
<onProperty rdf:resource="&clink;hasPathologicalProcess"/>
<someValuesFrom rdf:resource="&clink;C"/>
</Restriction>
</intersectionOf>
</Class>
</equivalentClass>
<rdfs:subClassOf rdf:resource="&clink;XYZ"/>
</Class>
我需要一个接收XYZ
并返回以下内容的查询:
A
B
C
首先,XYZ
既不是a
,B
,C
的子类,也不是的子类,它们的交集a和B以及C
(我正在使用)
在代码段中,您将XYZ
定义为三个类的交集的等价于
(这意味着也是子类的一部分);这是(影响一些A)和(hasMaker一些B)以及(hasPathologicalProcess一些C)
。此交叉点不等同于A和B以及C
(some
在曼彻斯特语法中代表somevalues from
)
要了解限制中的somevalues的含义,请参阅OWL的说明:
值约束owl:someValuesFrom
是一个内置的owl属性
将限制类链接到类描述或数据范围。A.
包含owl:someValuesFrom
约束的限制描述了
至少有一个财产价值的所有个人的类别
所涉及的是类描述的实例或中的数据值
数据范围。换句话说,它定义了一类个体x
至少有一个y
(类的一个实例
数据范围的描述或值),以使对(x,y)
为
P
的实例。这并不排除还有其他实例
(x,y')
的P
,y'
不属于类描述或
数据范围
EDIT2:
现在,您应该已经理解了owl:someValuesFrom
的含义,正如@AKSW所建议的,这里是一个简单的SPARQL查询。但是,仅使用rdfs:subassof
无法检索A
、B
和C
!应首先检索限制,然后访问属性及其定义的类,如下所示:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?eqclass ?restriction ?onProp ?someValuesFrom where {
{?eqclass owl:equivalentClass/owl:intersectionOf _:b. _:b rdf:first ?restriction}
# First anonymous class (restriction) in the collection
UNION { ?eqclass owl:equivalentClass/owl:intersectionOf/(rdf:rest+/rdf:first+)* ?restriction.}
# getting other anonymous classes (restriction) using property paths and rdf:first and rdf:rest used in RDF collections.
?restriction rdf:type owl:Restriction.
# individuals of type owl:Restriction
?restriction owl:onProperty ?onProp.
# the property the restriciton is defined on which
?restriction owl:someValuesFrom ?someValuesFrom.
# the class of the owl:someValuesFrom property
}
因此,您的查询将得到该类<代码> ANYNION交叉点< /代码>,而不是空白节点。
现在,如果您想在结果中获得所有(影响部分A)
、(hasMaker部分B)
和(haspathylogicalprocess部分C)
,您应该首先确保推理程序正在运行,因为这是一个隐式知识,其次您应该为每个匿名交集类,以与上述anyn\u交叉口
类似的方式定义具体交叉口类别。例如,您可以为匿名限制类定义anyn\u AffectsSomeA
:影响某些A
,如下所示:
<owl:Class rdf:about="myPrefix#anyn_intersection">
<owl:equivalentClass>
<owl:Class>
<owl:intersectionOf rdf:parseType="Collection">
<owl:Restriction>
<owl:onProperty rdf:resource="myPrefix#affects"/>
<owl:someValuesFrom rdf:resource="myPrefix#A"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="myPrefix#hasMaker"/>
<owl:someValuesFrom rdf:resource="myPrefix#B"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="myPrefix#hasPathologicalProcess"/>
<owl:someValuesFrom rdf:resource="myPrefix#C"/>
</owl:Restriction>
</owl:intersectionOf>
</owl:Class>
</owl:equivalentClass>
</owl:Class>
<owl:Class rdf:about="myPrefix#anyn_AffectsSomeA">
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="myPrefix#affects"/>
<owl:someValuesFrom rdf:resource="myPrefix#A"/>
</owl:Restriction>
</owl:equivalentClass>
</owl:Class>
然后您必须定义两个类似的类anyn\u hasMakerSomeB
和anyn\u hasPathologicalProcessomec
。最后,您将anyn\u交集
定义为anyn\u AffectsSomeA
,anyn\u hasMakerSomeB
,以及anyn\u hasPathologicalProcessomec
的交集
EDIT1:
我不知道rdfLib
中是否有一些特定的功能可以让您检索匿名类定义。这可能会解决你的问题,而不必按照我建议的方式来定义它。此外,您应该确保reasoner正在运行
EDIT1的结尾:问题是关于使用SPARQL获取对象属性限制的填充项。我不明白为什么有人应该在这里引入一些人工类。答案应该更多地显示OWL映射到RDF图的方向,然后编写相应的SPARQL查询,这通常是更复杂的,因为OWL构造可以包含多于一个三倍的事实。请参阅我的EdET2的答案。不过没关系,谢谢你的回答。
<owl:Class rdf:about="myPrefix#anyn_AffectsSomeA">
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="myPrefix#affects"/>
<owl:someValuesFrom rdf:resource="myPrefix#A"/>
</owl:Restriction>
</owl:equivalentClass>
</owl:Class>