使用RDFLib获取SPARQL查询中相交的类

2024-05-16 02:15:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类ABC,它是一个subClassOfXYZ,定义为ABC的交集,如下所示:

<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>

如何使用RDFLib中的SPARQL查询通过类XYZ访问AB和{}类?在

下面的查询返回一个空节点rdflib.term.BNode('_L'),我不知道如何处理bNode。在

^{pr2}$

我需要一个接收XYZ并返回以下内容的查询:

A
B
C

Tags: 定义rdfresourceclassaboutabcxyzrestriction
1条回答
网友
1楼 · 发布于 2024-05-16 02:15:30

首先,XYZ既不是subClassOfABC,也不是subClassOf它们的交集A and B and C(我使用的是manchester syntaax (see here))。在

在您的代码片段中,您将XYZ定义为equivalentTo(这意味着它也是subClassOf)的三个anynomous (see here)类的交集;它们是(affects some A) and (hasMaker some B) and (hasPathologicalProcess some C)。这个交集并不等同于A and B and C(Manchester语法中some代表someValuesFrom)。在

要理解someValuesFrom限制的含义,请参阅OWL的documentation (see here)

The value constraint owl:someValuesFrom is a built-in OWL property that links a restriction class to a class description or a data range. A restriction containing an owl: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. In other words, it defines a class of individuals x for which there is at least one y (either an instance of the class description or value of the data range) such that the pair (x,y) is an instance of P. This does not exclude that there are other instances (x,y') of P for which y' does not belong to the class description or data range.

编辑2:

既然您已经理解了owl:someValuesFrom的含义,并且正如@AKSW所建议的那样,这里有一个简单明了的SPARQL查询。但是,您不能仅仅使用rdfs:subClassOf来检索AB和{}!应首先检索限制,然后访问属性及其定义的类,如下所示:

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
} 

编辑2结束

通过修改数据模型来解决其他问题。

因此,首先,您的查询应该返回匿名类(affects some A) and (hasMaker some B) and (hasPathologicalProcess some C),这是您定义的交集。但是,由于它是一个匿名类,它将返回一个空节点B_node,而不是一个具体的类。要返回一个具体的类,您应该将这个匿名交集定义为这个交集的本体中的一个类;例如,您可以创建类anyn_intersection,如下所示:

^{pr2}$

因此,您的查询将获得该类^ {< CD2>}而不是空白节点。在

现在,如果你想在结果中得到所有的(affects some A)(hasMaker some B),和{},你应该首先确保推理器正在运行,因为这是一个隐式知识,第二个你应该对每个匿名交集类,以与上面的anyn_intersection相似的方式定义一个具体的交集类。例如,可以为匿名限制类定义anyn_AffectsSomeA,如下所示:

^{3}$

然后必须定义两个类似的类anyn_hasMakerSomeB和{}。最后,将anyn_intersection定义为anyn_AffectsSomeAanyn_hasMakerSomeB和{}的交集。在

编辑1:

我不知道rdfLib中是否有一些特定的功能可以让您检索匿名类定义。这可能会解决你的问题,而不必按照我建议的方式来定义。另外,您应该确保reasoner正在运行。在

编辑1结束:

相关问题 更多 >