如何查询实例之间路径的OWL模式

时间:2017-06-21 12:16:08

标签: sparql rdf owl

假设我有一个名为A(比如说)的类,另一个名为(B)的类等等......

现在,我想找到A和B之间的关系。

条件一:

假设它们都由域A和范围B的属性连接。

现在,我如何找到属性,给出两个A和B类。

条件二:

假设它们都由一个由属性连接的中间类C连接。

现在,给定这两个类,我如何找到中间类和属性?

SPARQL对此有帮助吗?如果或如果没有,怎么样?

实施例 OWL ontology, created from WebVOWL 这是我的猫头鹰结构。

现在,给定类“讲师”和“部门”,使用SPARQL,我怎么知道worksin是连接两个类的属性,它是单向/双向的?

<!-- OWL Class Definition - Instructor Type -->
<owl:Class rdf:about="http://www.example.com/sample#instructor">

    <rdfs:label>The instructor type</rdfs:label>
    <rdfs:comment>The class of all instructor types.</rdfs:comment>

</owl:Class>

<!-- OWL Class Definition - Department Type -->
<owl:Class rdf:about="http://www.example.com/sample#department">

    <rdfs:label>The department type</rdfs:label>
    <rdfs:comment>The class of all department types.</rdfs:comment>

</owl:Class>



<!-- Define the works in property -->
<owl:ObjectProperty rdf:about="http://www.example.com/sample#worksin">
    <rdfs:domain rdf:resource="http://www.example.com/sample#instructor" />
    <rdfs:range rdf:resource="http://www.example.com/sample#department" />
</owl:ObjectProperty>

使用以下SPARQL查询,我能够检索与该类关联的实例。但是,我如何找到加入该类的属性。

select ?b where {
     ?b <http://www.example.com/sample#worksin> <http://www.example.com/sample#history>
}

TL; DR 实际上我正在寻找的是,给定类,我想找到连接类的中间类和属性。这样,我就可以查询实例了。

完整OWL文件

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sample="http://www.example.com/sample#">

    <!-- OWL Header Example -->
    <owl:Ontology rdf:about="http://www.example.com/sample">
        <dc:title>The example.com Example Instructor Ontology</dc:title>
        <dc:description>An example ontolgy for instructor and where he or she works</dc:description>
    </owl:Ontology>

    <!-- OWL Class Definition - Instructor Type -->
    <owl:Class rdf:about="http://www.example.com/sample#instructor">

        <rdfs:label>The instructor type</rdfs:label>
        <rdfs:comment>The class of all instructor types.</rdfs:comment>

    </owl:Class>

    <!-- OWL Class Definition - Department Type -->
    <owl:Class rdf:about="http://www.example.com/sample#department">

        <rdfs:label>The department type</rdfs:label>
        <rdfs:comment>The class of all department types.</rdfs:comment>

    </owl:Class>


    <!-- Define the instructor name property -->
    <owl:DatatypeProperty rdf:about="http://www.example.com/sample#instructorname">
        <rdfs:domain rdf:resource="http://www.example.com/sample#instructor" />
    </owl:DatatypeProperty>

    <!-- Define the salary property -->
    <owl:DatatypeProperty rdf:about="http://www.example.com/sample#salary">
        <rdfs:domain rdf:resource="http://www.example.com/sample#instructor" />
    </owl:DatatypeProperty>

    <!-- Define the department name property -->
    <owl:DatatypeProperty rdf:about="http://www.example.com/sample#departmentname">
        <rdfs:domain rdf:resource="http://www.example.com/sample#department" />
    </owl:DatatypeProperty>


    <!-- Define the building property -->
    <owl:DatatypeProperty rdf:about="http://www.example.com/sample#building">
        <rdfs:domain rdf:resource="http://www.example.com/sample#department" />
    </owl:DatatypeProperty>

    <!-- Define the budget property -->
    <owl:DatatypeProperty rdf:about="http://www.example.com/sample#budget">
        <rdfs:domain rdf:resource="http://www.example.com/sample#department" />
    </owl:DatatypeProperty>

    <!-- Define the works in property -->
    <owl:ObjectProperty rdf:about="http://www.example.com/sample#worksin">
        <rdfs:domain rdf:resource="http://www.example.com/sample#instructor" />
        <rdfs:range rdf:resource="http://www.example.com/sample#department" />
    </owl:ObjectProperty>


    <!-- Define the Einstein class instance -->
    <rdf:Description rdf:about="http://www.example.com/sample#einstein">

        <!-- Einstein is an individual (instance) of the instructor class -->
        <rdf:type rdf:resource="http://www.example.com/sample#instructor"/>

        <!-- Einstein name stored under -->
        <sample:instructorname>Einstein</sample:instructorname>

        <!-- Einstein earns 95000 -->
        <sample:salary>95000</sample:salary>

        <!-- Einstein works in Physics Department -->
        <sample:worksin rdf:resource="http://www.example.com/sample#physics"/>

    </rdf:Description>

    <rdf:Description rdf:about="http://www.example.com/sample#wu">
        <rdf:type rdf:resource="http://www.example.com/sample#instructor"/>
        <sample:instructorname>Wu</sample:instructorname>
        <sample:salary>90000</sample:salary>
        <sample:worksin rdf:resource="http://www.example.com/sample#finance"/>
    </rdf:Description>

    <rdf:Description rdf:about="http://www.example.com/sample#singh">
        <rdf:type rdf:resource="http://www.example.com/sample#instructor"/>
        <sample:instructorname>Singh</sample:instructorname>
        <sample:salary>80000</sample:salary>
        <sample:worksin rdf:resource="http://www.example.com/sample#finance"/>
    </rdf:Description>

    <rdf:Description rdf:about="http://www.example.com/sample#elsaid">
        <rdf:type rdf:resource="http://www.example.com/sample#instructor"/>
        <sample:instructorname>El Said</sample:instructorname>
        <sample:salary>60000</sample:salary>
        <sample:worksin rdf:resource="http://www.example.com/sample#history"/>
    </rdf:Description>

    <rdf:Description rdf:about="http://www.example.com/sample#califieri">
        <rdf:type rdf:resource="http://www.example.com/sample#instructor"/>
        <sample:instructorname>Califieri</sample:instructorname>
        <sample:salary>62000</sample:salary>
        <sample:worksin rdf:resource="http://www.example.com/sample#history"/>
    </rdf:Description>

    <!-- Now for department table -->
    <rdf:Description rdf:about="http://www.example.com/sample#physics">
        <rdf:type rdf:resource="http://www.example.com/sample#department"/>
        <sample:departmentname>Physics</sample:departmentname>
        <sample:building>Watson</sample:building>
        <sample:budget>70000</sample:budget>
    </rdf:Description>

    <rdf:Description rdf:about="http://www.example.com/sample#finance">
        <rdf:type rdf:resource="http://www.example.com/sample#department"/>
        <sample:departmentname>Finance</sample:departmentname>
        <sample:building>Painter</sample:building>
        <sample:budget>120000</sample:budget>
    </rdf:Description>

    <rdf:Description rdf:about="http://www.example.com/sample#history">
        <rdf:type rdf:resource="http://www.example.com/sample#department"/>
        <sample:departmentname>History</sample:departmentname>
        <sample:building>Painter</sample:building>
        <sample:budget>50000</sample:budget>
    </rdf:Description>

</rdf:RDF>

2 个答案:

答案 0 :(得分:2)

如果您已经拥有该图表,我不会理解这个难点,但要保持简短:

任务1:

SELECT DISTINCT ?p WHERE {
?p rdfs:domain :A ; # properties having domain :A
   rdfs:range :B    # properties having range :B
}

任务2:

SELECT DISTINCT ?p1 ?p2 ?cls WHERE {
 ?p1 rdfs:domain :A ;      # properties having domain :A
     rdfs:range ?cls .     # and as range the intermediate class
 ?p2 rdfs:domain ?cls ;    # which is on the other hand the domain of another property
     rdfs:range :C         # that has the range :C
}

答案 1 :(得分:1)

如果您添加$stream = fopen('s3://bucket/key', 'a'); fwrite($stream, 'Hello!'); fclose($stream); 属性作为hasWorker的反转,并且您使用带有owl推理的triplestore,则可以使用类似以下查询的内容来处理

之类的关系
worksin

对于任何数量的跳跃和任何属性的选择都非常难以概括:path between two resources

三元组:

A p1 B p2 C

查询:

@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ns0: <http://www.example.com/sample#> .

<http://www.example.com/sample#worksin>
  a owl:ObjectProperty ;
  owl:inverseOf <http://www.example.com/sample/hasWorker> ;
  rdfs:domain <http://www.example.com/sample#instructor> ;
  rdfs:range <http://www.example.com/sample#department> .

<http://www.example.com/sample/hasWorker> a owl:ObjectProperty .
<http://www.example.com/sample#departmentname>
  a owl:DatatypeProperty ;
  rdfs:domain <http://www.example.com/sample#department> .

<http://www.example.com/sample#instructorname>
  a owl:DatatypeProperty ;
  rdfs:domain <http://www.example.com/sample#instructor> .

<http://www.example.com/sample#department> a owl:Class .
<http://www.example.com/sample#instructor> a owl:Class .
<http://www.example.com/sample#califieri>
  a owl:NamedIndividual, <http://www.example.com/sample#instructor> ;
  ns0:worksin ns0:history ;
  ns0:instructorname "Califieri" .

ns0:einstein
  a owl:NamedIndividual, ns0:instructor ;
  ns0:worksin ns0:physics ;
  ns0:instructorname "Einstein" .

ns0:elsaid
  a owl:NamedIndividual, ns0:instructor ;
  ns0:worksin ns0:history ;
  ns0:instructorname "El Said" .

ns0:finance
  a owl:NamedIndividual, ns0:department ;
  ns0:departmentname "Finance" .

ns0:history
  a owl:NamedIndividual, ns0:department ;
  ns0:departmentname "History" .

ns0:physics
  a owl:NamedIndividual, ns0:department ;
  ns0:departmentname "Physics" .

ns0:singh
  a owl:NamedIndividual, ns0:instructor ;
  ns0:worksin ns0:finance ;
  ns0:instructorname "Singh" .

ns0:wu
  a owl:NamedIndividual, ns0:instructor ;
  ns0:worksin ns0:finance ;
  ns0:instructorname "Wu" .

结果:

SELECT DISTINCT  ?aInst ?p1 ?bInst ?p2 ?cInst 
WHERE
{
  ?aInst  a                     ?a .
  ?a a owl:Class .

  ?bInst  a                     ?b .
  ?b a owl:Class .

  ?cInst  a                     ?c .
  ?c a owl:Class .

  ?aInst  ?p1                   ?bInst .
  ?bInst  ?p2                   ?cInst

  filter (?aInst != ?bInst )
  filter (?bInst != ?cInst )
  filter (?aInst != ?cInst )

  filter (?p1 != <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> )

  filter (?p2 != <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> )
}
相关问题