[英]get the intersected classes in SPARQL query using RDFLib
I have a class ABC
that is a subClassOf
the class XYZ
and defined as intersections of classes A
, B
, C
as: 我有一个类
ABC
是一个subClassOf
类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>
How I can get access to the classes A
, B
and C
through the class XYZ
using SPARQL query in RDFLib? 如何使用RDFLib中的SPARQL查询通过
XYZ
类访问A
, B
和C
类?
The following query returns a blank node rdflib.term.BNode('_L')
and I don't know how to handle the BNodes. 以下查询返回一个空白节点
rdflib.term.BNode('_L')
,我不知道如何处理BNodes。
PREFIX rdf: h<ttp://www.w3.org/2000/01/rdf-schema#>
SELECT ?subclass
WHERE {<XYZ> <rdf:subClassOf> <subclass>}
I need a query that receive the XYZ
and returns this: 我需要一个接收
XYZ
并返回以下内容的查询:
A
B
C
First of all, XYZ
is neither a subClassOf
A
, B
, C
nor a subClassOf
their intersection A and B and C
(I am using manchester syntaax (see here) ). 首先,
XYZ
既不是subClassOf
A
, B
, C
也不是subClassOf
它们的交点A and B and C
(我用的曼彻斯特syntaax(见这里) )。
In your snippet, you define XYZ
to be equivalentTo
(which implies being a subClassOf
as well) of the intersection of three anynomous (see here) classes; 在你的代码片段,您可以定义
XYZ
是equivalentTo
(这意味着是一个subClassOf
的三个路口的为好) anynomous(见这里)类; which are (affects some A) and (hasMaker some B) and (hasPathologicalProcess some C)
. 这是
(affects some A) and (hasMaker some B) and (hasPathologicalProcess some C)
。 This intersection is not equivalent to A and B and C
( some
in Manchester syntax stands for someValuesFrom
). 此交集不等同于
A and B and C
(在曼彻斯特语法中, some
代表someValuesFrom
)。
To understand what someValuesFrom
restriction means, see the documentation (see here) of OWL: 要了解
someValuesFrom
限制的含义,请参阅 OWL的文档(请参阅此处) :
The value constraint
owl:someValuesFrom
is a built-in OWL property that links a restriction class to a class description or a data range.值约束
owl:someValuesFrom
是一个内置的OWL属性,它将限制类链接到类描述或数据范围。 A restriction containing anowl:someValuesFrom
constraint describes a class of all individuals for which at least one value of the property concerned is an instance of the class description or a data value in the data range.包含
owl:someValuesFrom
约束的限制描述了一个所有个体的类,对于这些个体,至少相关属性的一个值是类描述的实例或数据范围内的数据值。 In other words, it defines a class of individuals x for which there is at least oney
(either an instance of the class description or value of the data range) such that the pair(x,y)
is an instance ofP
.换句话说,它定义了一个至少有一个
y
的个体x的类(类描述的实例或数据范围的值),使得对(x,y)
是P
的实例。 This does not exclude that there are other instances(x,y')
ofP
for whichy'
does not belong to the class description or data range.这并不排除
P
其他实例(x,y')
的y'
不属于类描述或数据范围。
EDIT2: EDIT2:
Now that you should have understood what owl:someValuesFrom
means, and as @AKSW suggests, here is a straightforward SPARQL query. 现在,您应该已经了解
owl:someValuesFrom
含义,并且正如@AKSW所建议的那样,这是一个简单的SPARQL查询。 However, you cannot retrieve A
, B
, and C
simply using rdfs:subClassOf
! 但是,您不能仅使用
rdfs:subClassOf
来检索A
, B
和C
! You should first retrieve the restriction, then access the property and the class it is defined on, as follows: 您应该首先检索该限制,然后访问该属性及其定义的类,如下所示:
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
}
End of EDIT2 EDIT2结束
Other work around through modifying your data model. 通过修改数据模型的其他方法。
So first, your query should return the anonymous class (affects some A) and (hasMaker some B) and (hasPathologicalProcess some C)
which is the intersection you define. 因此,首先,您的查询应返回匿名类
(affects some A) and (hasMaker some B) and (hasPathologicalProcess some C)
,这是您定义的交集。 However, as it is an anonymous class, a blank node B_node
will be returned for it, rather than a concrete class. 但是,由于它是一个匿名类,
B_node
将为它返回一个空白节点B_node
,而不是一个具体的类。 To return a concrete class, you should define this anonymous intersection as a class in your ontology for this intersection; 要返回一个具体的类,您应该将此匿名交集定义为该交集的本体中的一个类。 for example, you can create the class
anyn_intersection
, as follows: 例如,您可以创建类
anyn_intersection
,如下所示:
<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>
Thus, your query will get this class anyn_intersection
instead of the blank node. 因此,您的查询将获得此类
anyn_intersection
而不是空白节点。
Now If you want to get all of (affects some A)
, (hasMaker some B)
, and (hasPathologicalProcess some C)
in the results, you should first make sure that a reasoner is running because this is an implicit knowledge and second you should for each anonymous intersection class, define a concrete intersection class in a similar way to the anyn_intersection
above. 现在,如果要在结果中获取所有
(affects some A)
, (hasMaker some B)
和(hasPathologicalProcess some C)
,则应首先 确保推理程序正在运行,因为这是一个隐式知识, 其次您应该对于每个匿名交集类,以与上述anyn_intersection
类似的方式定义一个具体的交集类 。 For example, you can define anyn_AffectsSomeA
for the anonymous restriction class: affects some A
as follows: 例如,您可以为匿名限制类定义
anyn_AffectsSomeA
: affects some A
如下:
<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>
Then you have to define two similar classes anyn_hasMakerSomeB
and anyn_hasPathologicalProcessSomeC
. 然后,您必须定义两个类似的类
anyn_hasMakerSomeB
和anyn_hasPathologicalProcessSomeC
。 Finally, you define anyn_intersection
as the intersection of anyn_AffectsSomeA
, anyn_hasMakerSomeB
, and anyn_hasPathologicalProcessSomeC
. 最后,将
anyn_intersection
定义为anyn_AffectsSomeA
, anyn_hasMakerSomeB
和anyn_hasPathologicalProcessSomeC
。
EDIT1: EDIT1:
I am not aware if there is some specific feature in rdfLib
that enables you to retrieve anonymous classes definitions. 我不知道
rdfLib
中是否有某些特定功能rdfLib
来检索匿名类定义。 That might solve your problem without having to define it the way I suggest. 这可能可以解决您的问题,而不必按照我建议的方式进行定义。 Additionally, you should make sure a reasoner is running.
此外,您应确保推理机正在运行。
End of EDIT1: EDIT1的结尾:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.