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

时间:2017-12-18 14:36:08

标签: python sparql rdf owl rdflib

我的班级ABCsubClassOf班级XYZ,并定义为班级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查询通过类A访问类BCXYZ

以下查询返回空白节点rdflib.term.BNode('_L'),我不知道如何处理BNode。

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

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

我需要一个接收XYZ的查询并返回此信息:

A
B
C

1 个答案:

答案 0 :(得分:3)

首先,XYZ既不是subClassOf ABC也不是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(曼彻斯特语法中的some代表someValuesFrom)。

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

  

值约束owl:someValuesFrom是内置的OWL属性   将限制类链接到类描述或数据范围。一个   包含owl:someValuesFrom约束的限制描述了一个   所有个人的类,其中至少有一个属性值   有关是类描述的实例或数据值   数据范围。换句话说,它定义了一类个体x   至少有一个y(该类的一个实例)   描述或数据范围的值)使得(x,y)对是   P的实例。这并不排除存在其他情况   (x,y') P的{​​{1}} y'不属于班级描述或   数据范围。

<强> EDIT2:

既然你应该理解owl:someValuesFrom的含义,而且正如@AKSW所说,这是一个简单的SPARQL查询。但是,您无法使用A检索BCrdfs:subClassOf!您应首先检索限制,然后访问属性及其定义的类,如下所示:

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

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

首先,您的查询应该返回匿名类(affects some A) and (hasMaker some B) and (hasPathologicalProcess some C),这是您定义的交集。但是,由于它是一个匿名类,因此将为其返回空白节点B_node,而不是具体类。要返回一个具体的类,您应该将此匿名交集定义为此交集的本体中的类;例如,您可以创建类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>

因此,您的查询将获得此类anyn_intersection而不是空白节点。

现在,如果您希望获得结果中的所有(affects some A)(hasMaker some B)(hasPathologicalProcess some C),则应首先 确保推理器正在运行因为这是一个隐含的知识而第二你应该为每个匿名交集类,以与anyn_intersection <类似的方式定义一个具体的交集类/ em>以上。例如,您可以为匿名限制类定义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>

然后你必须定义两个类似的类anyn_hasMakerSomeBanyn_hasPathologicalProcessSomeC。最后,您将anyn_intersection定义为anyn_AffectsSomeAanyn_hasMakerSomeBanyn_hasPathologicalProcessSomeC的交集。

<强> EDIT1:

我不知道rdfLib中是否有某些特定功能可以让您检索匿名类定义。这可能会解决您的问题,而无需按照我的建议来定义它。此外,您应该确保推理器正在运行。

EDIT1结束: