gpt4 book ai didi

java - 检索 OWL 交集类隐含的父类(super class)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:47:43 26 4
gpt4 key购买 nike

OWL 本体可能有 A、B 和 C 类,以及公理(DL 表示法):

A ⊑ (B ⊓ C)

或近似曼彻斯特 OWL 语法:

A subClassOf (B and C)

A是B的子类,A是C的子类在逻辑上是正确的,但是三元组

A rdfs:subClassOf B
A rdfs:subClassOf C

不一定出现在 OWL 本体的 RDF 序列化中。例如,考虑 Protégé 中的这个非常简单的本体及其在 RDF/XML 和 Turtle 中的 RDF 序列化:

enter image description here

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://stackoverflow.com/q/19924861/1281433/sample.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://stackoverflow.com/q/19924861/1281433/sample.owl"/>
<owl:Class rdf:about="http://stackoverflow.com/q/19924861/1281433/sample.owl#C"/>
<owl:Class rdf:about="http://stackoverflow.com/q/19924861/1281433/sample.owl#B"/>
<owl:Class rdf:about="http://stackoverflow.com/q/19924861/1281433/sample.owl#A">
<rdfs:subClassOf>
<owl:Class>
<owl:intersectionOf rdf:parseType="Collection">
<owl:Class rdf:about="http://stackoverflow.com/q/19924861/1281433/sample.owl#B"/>
<owl:Class rdf:about="http://stackoverflow.com/q/19924861/1281433/sample.owl#C"/>
</owl:intersectionOf>
</owl:Class>
</rdfs:subClassOf>
</owl:Class>
</rdf:RDF>
@prefix :      <http://stackoverflow.com/q/19924861/1281433/sample.owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://stackoverflow.com/q/19924861/1281433/sample.owl>
a owl:Ontology .

:B a owl:Class .

:C a owl:Class .

:A a owl:Class ;
rdfs:subClassOf [ a owl:Class ;
owl:intersectionOf ( :B :C )
] .

序列化有一个 rdfs:subClassOf 的三元组,但对象不是 :B:C,所以查询像

:A rdfs:subClassOf ?superclass

不会返回 :A 的父类(super class)。我如何编写一个 SPARQL 查询来返回 :A 的那些父类(super class)?

最佳答案

听起来您有一个类是某个交集类的子类。例如,您可能有

StudentPersonenrolledIn some Course

在 Protégé OWL 本体编辑器中,这看起来像:

Definition of student in Protege

如果您为子类编写 SPARQL 查询,例如,

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?subclass ?superclass where {
?subclass rdfs:subClassOf ?superclass
}

并且您没有推断其他数据的推理器,您不会在结果中看到 Student 作为子类,但您可能会看到一个空白(匿名)节点:

---------------------------------------------------------
| subclass | superclass |
=========================================================
| <http://www.examples.org/school#Student> | _:b0 |
---------------------------------------------------------

要理解为什么会这样,您需要看一下本体的 RDF 序列化。在这种情况下,它是(在 RDF/XML 中):

<rdf:RDF
xmlns="http://www.examples.org/school#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.examples.org/school"/>
<owl:Class rdf:about="http://www.examples.org/school#Course"/>
<owl:Class rdf:about="http://www.examples.org/school#Person"/>
<owl:Class rdf:about="http://www.examples.org/school#Student">
<rdfs:subClassOf>
<owl:Class>
<owl:intersectionOf rdf:parseType="Collection">
<owl:Class rdf:about="http://www.examples.org/school#Person"/>
<owl:Restriction>
<owl:onProperty>
<owl:ObjectProperty rdf:about="http://www.examples.org/school#enrolledIn"/>
</owl:onProperty>
<owl:someValuesFrom rdf:resource="http://www.examples.org/school#Course"/>
</owl:Restriction>
</owl:intersectionOf>
</owl:Class>
</rdfs:subClassOf>
</owl:Class>
</rdf:RDF>

或者在更易读的 Turtle 中(也更像 SPARQL 查询语法):

@prefix :      <http://www.examples.org/school#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Student a owl:Class ;
rdfs:subClassOf [ a owl:Class ;
owl:intersectionOf ( :Person [ a owl:Restriction ;
owl:onProperty :enrolledIn ;
owl:someValuesFrom :Course
] )
] .

:Person a owl:Class .

:enrolledIn a owl:ObjectProperty .

:Course a owl:Class .

<http://www.examples.org/school>
a owl:Ontology .

事实上,数据中有一个Student rdfs:subClassOf [ ... ] 三元组,但是[ ... ] 是一个空白节点;它是一个匿名的 owl:Class,它是其他一些类的交集。推理者可以告诉你,如果 X ⊑(YZ)则 XYXZ,但 SPARQL 查询本身不会这样做。不过,您可以像这样创建更复杂的 SPARQL 查询:

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 ?subclass ?superclass where {
{ ?subclass rdfs:subClassOf ?superclass }
union
{ ?subclass rdfs:subClassOf [ owl:intersectionOf [ rdf:rest* [ rdf:first ?superclass ] ] ] }
}
--------------------------------------------------------------------------------------
| subclass | superclass |
======================================================================================
| <http://www.examples.org/school#Student> | _:b0 |
| <http://www.examples.org/school#Student> | <http://www.examples.org/school#Person> |
| <http://www.examples.org/school#Student> | _:b1 |
--------------------------------------------------------------------------------------

两个空白节点分别是匿名交集类和匿名限制类(enrolledIn some Course)。如果你只想要 IRI 结果,你可以使用 filter:

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 ?subclass ?superclass where {
{ ?subclass rdfs:subClassOf ?superclass }
union
{ ?subclass rdfs:subClassOf [ owl:intersectionOf [ rdf:rest* [ rdf:first ?superclass ] ] ] }

filter( isIRI( ?superclass ) )
}
--------------------------------------------------------------------------------------
| subclass | superclass |
======================================================================================
| <http://www.examples.org/school#Student> | <http://www.examples.org/school#Person> |
--------------------------------------------------------------------------------------

现在,作为最后一步,如果您想让查询更小一些,因为这两个 unioned 模式的唯一区别是连接 ?subclass 的路径> 和 ?superclass,您实际上可以只用一个属性路径来编写它。 (虽然,如 Sparql query Subclass or EquivalentTo 中所述,如果您这样做,您可能会遇到 Protégé 的一些问题。)我们的想法是您可以重写它:

{ ?subclass rdfs:subClassOf ?superclass }
union
{ ?subclass rdfs:subClassOf [ owl:intersectionOf [ rdf:rest* [ rdf:first ?superclass ] ] ] }

像这样,通过使用属性路径,这也消除了对空白节点的需要:

?subclass ( rdfs:subClassOf |
( rdfs:subClassOf / owl:intersectionOf / rdf:rest* / rdf:first ) ) ?superclass

你可以将其进一步简化为

?subclass rdfs:subClassOf/((owl:intersectionOf/rdf:rest*/rdf:first)+) ?superclass

你甚至可以从中删除一层括号,使其成为

?subclass rdfs:subClassOf/(owl:intersectionOf/rdf:rest*/rdf:first)+ ?superclass

但是你必须开始记住优先规则,这可不是什么有趣的事情。虽然查询有效:

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 ?subclass ?superclass where {
?subclass rdfs:subClassOf/(owl:intersectionOf/rdf:rest*/rdf:first)+ ?superclass
filter( isIRI( ?superclass ) )
}
--------------------------------------------------------------------------------------
| subclass | superclass |
======================================================================================
| <http://www.examples.org/school#Student> | <http://www.examples.org/school#Person> |
--------------------------------------------------------------------------------------

关于java - 检索 OWL 交集类隐含的父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19924861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com