如何为实例之间的路径查询 OWL 模式
How to query OWL schema for paths between instances
假设,我有一个 class 名为 A(比方说),另一个 class 名为 (B) 等等...
现在,我想找出 A 和 B 之间的关系。
条件一:
我们假设它们都通过 属性 与域 A 和范围 B 连接。
现在,我如何找出 属性,给定两个 classes A 和 B。
条件二:
让我们假设它们都通过中间 class C 连接,而中间 class C 通过 属性.
连接
现在,给定这两个 classes,我如何找到中间 classes 和属性?
SPARQL 对此有帮助吗?如果或如果不是,如何?
例子
这是我的 owl 结构。
现在,给定 classes "instructor" 和 "department",使用 SPARQL,我如何发现 worksin 是 属性,它连接了 classes 是 unidirectional/bidirectional?
<!-- 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 查询,我能够检索与 class 关联的实例。但是,我如何找出加入 class.
的 属性
select ?b where {
?b <http://www.example.com/sample#worksin> <http://www.example.com/sample#history>
}
TL;DR
我实际上要寻找的是,给定 classes,我想找到中间 classes 和加入 classes 的属性。这样,我就可以查询实例了。
完整 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>
如果你已经有了图表,我不明白其中的困难,但为了简短起见:
任务 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
}
如果添加 hasWorker
属性 作为 worksin
的逆运算,并且使用具有 owl 推理的三元组,则可以使用类似于以下查询的内容对于
这样的关系
A p1 B p2 C
对于任意数量的跃点和任何属性选择,真的很难将其推广:path between two resources
三元组:
@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> )
}
结果:
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
| aInst | p1 | bInst | p2 | cInst |
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
| <http://www.example.com/sample#elsaid> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#history> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#califieri> |
| <http://www.example.com/sample#califieri> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#history> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#elsaid> |
| <http://www.example.com/sample#wu> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#finance> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#singh> |
| <http://www.example.com/sample#singh> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#finance> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#wu> |
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
假设,我有一个 class 名为 A(比方说),另一个 class 名为 (B) 等等...
现在,我想找出 A 和 B 之间的关系。
条件一:
我们假设它们都通过 属性 与域 A 和范围 B 连接。
现在,我如何找出 属性,给定两个 classes A 和 B。
条件二:
让我们假设它们都通过中间 class C 连接,而中间 class C 通过 属性.
连接现在,给定这两个 classes,我如何找到中间 classes 和属性?
SPARQL 对此有帮助吗?如果或如果不是,如何?
例子
现在,给定 classes "instructor" 和 "department",使用 SPARQL,我如何发现 worksin 是 属性,它连接了 classes 是 unidirectional/bidirectional?
<!-- 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 查询,我能够检索与 class 关联的实例。但是,我如何找出加入 class.
的 属性select ?b where {
?b <http://www.example.com/sample#worksin> <http://www.example.com/sample#history>
}
TL;DR 我实际上要寻找的是,给定 classes,我想找到中间 classes 和加入 classes 的属性。这样,我就可以查询实例了。
完整 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>
如果你已经有了图表,我不明白其中的困难,但为了简短起见:
任务 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
}
如果添加 hasWorker
属性 作为 worksin
的逆运算,并且使用具有 owl 推理的三元组,则可以使用类似于以下查询的内容对于
A p1 B p2 C
对于任意数量的跃点和任何属性选择,真的很难将其推广:path between two resources
三元组:
@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> )
}
结果:
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
| aInst | p1 | bInst | p2 | cInst |
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
| <http://www.example.com/sample#elsaid> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#history> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#califieri> |
| <http://www.example.com/sample#califieri> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#history> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#elsaid> |
| <http://www.example.com/sample#wu> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#finance> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#singh> |
| <http://www.example.com/sample#singh> | <http://www.example.com/sample#worksin> | <http://www.example.com/sample#finance> | <http://www.example.com/sample/hasWorker> | <http://www.example.com/sample#wu> |
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+