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

get the intersected classes in SPARQL query using RDFLib

我有一个 class ABCsubClassOf class XYZ 并定义为 classes 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>

如何使用 SPARQL 查询通过 class XYZ 访问 classes ABC在 RDFLib 中?

下面的查询 returns 一个空白节点 rdflib.term.BNode('_L') 我不知道如何处理 BNodes。

PREFIX rdf: h<ttp://www.w3.org/2000/01/rdf-schema#>

SELECT ?subclass
WHERE {<XYZ> <rdf:subClassOf> <subclass>} 

我需要一个接收 XYZ 和 returns 的查询:

A
B
C

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

在您的代码段中,您将 XYZ 定义为三个 anynomous (see here) [=108= 的交集的 equivalentTo(这意味着也是 subClassOf) ]是的;这是 (affects some A) and (hasMaker some B) and (hasPathologicalProcess some C)。此交集不等同于 A and B and C(曼彻斯特语法中的 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.

EDIT2:

现在您应该已经理解了 owl:someValuesFrom 的含义,正如@AKSW 所建议的,这是一个简单的 SPARQL 查询。但是,您无法仅使用 rdfs:subClassOf 检索 ABC!您应该首先检索限制,然后访问 属性 和定义它的 class,如下所示:

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
} 

EDIT2 结束

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

所以首先,您的查询应该 return 匿名 class (affects some A) and (hasMaker some B) and (hasPathologicalProcess some C) 这是您定义的交集。然而,由于它是一个匿名的 class,一个空白节点 B_node 将被 return 编辑,而不是一个具体的 class。要 return 一个具体的 class,您应该在您的 ontology 中将此匿名交叉路口定义为 class;例如,您可以创建 class 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>

因此,您的查询将得到这个 class anyn_intersection 而不是空白节点。

现在如果你想在结果中得到所有的(affects some A)(hasMaker some B)(hasPathologicalProcess some C),你应该先 确保推理者是运行,因为这是隐性知识,second你应该每个匿名交叉点class,用类似于上面anyn_intersection的方式定义一个具体的交集class。例如,您可以为匿名限制class定义anyn_AffectsSomeAaffects 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>

那你得定义两个类似class的anyn_hasMakerSomeBanyn_hasPathologicalProcessSomeC。最后,您将 anyn_intersection 定义为 anyn_AffectsSomeAanyn_hasMakerSomeBanyn_hasPathologicalProcessSomeC.

的交集

编辑1:

我不知道 rdfLib 中是否有某些特定功能可以让您检索匿名 classes 定义。这可能会解决您的问题,而不必按照我建议的方式进行定义。此外,您应该确保推理器是 运行.

EDIT1 结束: