【问题标题】:get the intersected classes in SPARQL query using RDFLib使用 RDFLib 获取 SPARQL 查询中的相交类
【发布时间】:2017-12-18 14:36:08
【问题描述】:

我有一个 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 访问类 ABC

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

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

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

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

A
B
C

【问题讨论】:

    标签: python sparql rdf owl rdflib


    【解决方案1】:

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

    在您的 sn-p 中,您将 XYZ 定义为三个 anynomous (see here) 类的交集的 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)

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

    EDIT2:

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

    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的方式定义一个具体的交集类。例如,您可以为匿名限制类定义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 结束:

    【讨论】:

    • 问题是关于使用 SPARQL 获取对象属性限制的填充物。我不明白为什么有人应该在这里引入一些人工类。答案应该更倾向于显示 OWL 到 RDF 图的映射,然后编写相应的 SPARQL 查询,这通常更复杂,因为 OWL 构造可以包含多个三元组。
    • 查看我的Edit2的答案。
    • TO 可能不会将查询视为“直截了当”,但这很好:D
    • 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 2016-12-12
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多