Sparql 如何查询OWL模式中实例之间的路径
假设,我有一个名为a(比如)的类,另一个名为(B)的类,依此类推 现在,我想找出a和B之间的关系 条件一: 假设它们都由域a和范围B的属性连接 现在,给定两个类A和B,我如何找到这个属性 条件二: 让我们假设它们都由一个中间类C连接,这个中间类是通过属性连接的 现在,给定这两个类,我如何找到中间类和属性 SPARQL在这方面有帮助吗?如果没有,怎么做 范例 这是我的猫头鹰结构 现在,考虑到类“讲师”和“部门”,使用SPARQL,我如何发现worksin是连接这两个类的属性,并且是单向/双向的Sparql 如何查询OWL模式中实例之间的路径,sparql,rdf,owl,Sparql,Rdf,Owl,假设,我有一个名为a(比如)的类,另一个名为(B)的类,依此类推 现在,我想找出a和B之间的关系 条件一: 假设它们都由域a和范围B的属性连接 现在,给定两个类A和B,我如何找到这个属性 条件二: 让我们假设它们都由一个中间类C连接,这个中间类是通过属性连接的 现在,给定这两个类,我如何找到中间类和属性 SPARQL在这方面有帮助吗?如果没有,怎么做 范例 这是我的猫头鹰结构 现在,考虑到类“讲师”和“部门”,使用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>
}
选择?b其中{
B
}
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>
example.com示例讲师本体
为讲师提供的一个例子,以及他或她在哪里工作
讲师类型
所有讲师类型的课程。
部门类型
所有部门类型的类。
爱因斯坦
95000
吴
90000
辛格
80000
艾尔说
60000
加利福尼亚
62000
物理
沃森
70000
财务
画家
120000
历史
画家
50000
如果您已经有了图表,我不理解其中的困难,但请尽量简短:
任务1:
选择DISTINCT?p其中{
?p rdfs:domain:A;#具有domain:A的属性
rdfs:range:B#具有range:B的属性
}
任务2:
选择不同的p1?p2?cls,其中{
?p1 rdfs:domain:A;#具有domain:A的属性
rdfs:范围?cls.#和作为中间类的范围
?p2 rdfs:domain?cls;#另一方面,它是另一个属性的域
rdfs:range:C#其范围为:C
}
如果添加一个hasWorker
属性作为worksin
的逆属性,并使用带有owl推理的triplestore,则可以使用类似以下查询的内容来查询以下关系:
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" .
@前缀owl:。
@前缀rdfs:。
@前缀ns0:。
猫头鹰:对象属性;
猫头鹰:倒数;
rdfs:域;
rdfs:范围。
猫头鹰:对象属性。
owl:DatatypeProperty;
rdfs:域。
owl:DatatypeProperty;
rdfs:域。
猫头鹰:上课。
猫头鹰:上课。
一只猫头鹰:名字叫个人;
ns0:在ns0:历史中工作;
ns0:instructorname“Califieri”。
ns0:爱因斯坦
一只猫头鹰:NamedIndividual,ns0:讲师;
ns0:工作于ns0:物理学;
ns0:instructorname“Einstein”。
ns0:elsaid
一只猫头鹰:NamedIndividual,ns0:讲师;
ns0:在ns0:历史中工作;
ns0:instructorname“El Said”。
ns0:金融
猫头鹰:NamedIndividual,ns0:department;
ns0:部门名称“财务”。
ns0:历史
猫头鹰:NamedIndividual,ns0:department;
ns0:部门名称“历史”。
ns0:物理学
猫头鹰:NamedIndividual,ns0:department;
ns0:系名“物理学”。
ns0:辛格
一只猫头鹰:NamedIndividual,ns0:讲师;
ns0:工作于ns0:财务;
ns0:instructorname“Singh”。
ns0:wu
一只猫头鹰:NamedIndividual,ns0:讲师;
ns0:工作于ns0:财务;
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
哪里
{
是的。
猫头鹰:上课。
?垃圾箱a?b。
猫头鹰:上课。
?辛斯特a?c。
猫头鹰:职业。
?aInst?p1?垃圾箱。
?料仓?p2?料仓
过滤器(?aInst!=?箱)
过滤器(?料仓!=?料仓)
过滤器(?aInst!=?cInst)
过滤器(?p1!=)
过滤器(?p2!=)
}
结果:
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
| 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> |
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
|aInst | p1 | bInst | p2 | cInst|
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+-------------------------------------------+-----------------------------------------+-----------------------------------------+-------------------------------------------+-------------------------------------------+
这篇文章写得很好,但看起来不太好