首页 > 解决方案 > 如何在 RDF/OWL 中为复合键建模

问题描述

我正在尝试将我当前的关系数据库转换为 RDF/OWL 三重存储。我遇到的一件事是当我有一个具有多个值的复合/复合键的桥接/连接表时。例如,我有以下内容:

我不确定我将如何对数据建模Equipment :hasPoint ...?特定点可能是不同的商品,具体取决于它所在的设备类型。

感谢任何帮助。

标签: constraintsrdfowl

解决方案


首先,也许你可以改造东西,摆脱:EquipmentPoints. 它们可能只是关系建模的产物。RDF 属性可能有多个值。有关更多详细信息,请参见此处

为了清楚起见,我将稍微简化您的数据模型:

  • Equipment (EquipmentId)
  • EquipmentPoints (EquipmentId, PointId)
  • Point (PointId)

RDF

RDF 是无模式的,RDF 中没有约束。

您可以对事物进行建模,如另一个答案所示:

@prefix : <https://stackoverflow.com/q/51974155/7879193#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

:equipment1 rdf:type :Equipment .
:equipment2 rdf:type :Equipment .

:point1 rdf:type :Point .
:point2 rdf:type :Point .

:equipmentPoint1 rdf:type :EquipmentPoint .
:equipmentPoint2 rdf:type :EquipmentPoint .

:equipmentPoint1 :hasEquipment :equipment1 ;
                 :hasPoint :point1 .

:equipmentPoint2 :hasEquipment :equipment1 ;
                 :hasPoint :point1 . # "constraint violation"

为了定义约束,您可以使用 SHACL 之类的语言。不幸的是,SHACL 中没有复合键的核心约束组件,应该使用基于 SPARQL 的约束

:EquipmentPointShape a sh:NodeShape ;
    sh:targetClass :EquipmentPoint ;
    sh:sparql [
        sh:message "Violation!" ;
        sh:severity sh:Violation ;
        sh:select """
                  SELECT ?this {
                      ?point1 ^:hasPoint  ?this, ?that .
                      ?equipment ^:hasEquipment  ?this, ?that .
                      FILTER (?this != ?that)
                  }
                  """
        ] .

猫头鹰

OWL 是为推理而设计的,而不是为约束检查而设计的。有关更多详细信息,请参阅此答案。但是,您可以使用OWL 2 keys

首先,添加一些本体“样板”:

[] rdf:type owl:Ontology .

:Equipment rdf:type owl:Class .
:Point rdf:type owl:Class .
:EquipmentPoint rdf:type owl:Class .

:hasPoint rdf:type owl:ObjectProperty .
:hasEquipment rdf:type owl:ObjectProperty .

:equipment1 rdf:type owl:NamedIndividual .
:equipment2 rdf:type owl:NamedIndividual .

:point1 rdf:type owl:NamedIndividual .
:point2 rdf:type owl:NamedIndividual .

:equipmentPoint1 rdf:type owl:NamedIndividual .
:equipmentPoint2 rdf:type owl:NamedIndividual .

现在你有了正确的 Turtle 序列化本体。然后加:

:EquipmentPoint owl:hasKey (:hasEquipment
                            :hasPoint
                           ) .

[ rdf:type owl:AllDifferent ;
  owl:distinctMembers (:equipmentPoint1
                       :equipmentPoint2
                      )
] .

推理者会推断出你的本体是不一致的。


请注意,OWL 中没有唯一名称假设,并且存在开放世界假设。

删除后

[ rdf:type owl:AllDifferent ;
  owl:distinctMembers (:equipmentPoint1
                       :equipmentPoint2
                      )
] .

推理者会推断

:equipmentPoint1 owl:sameAs :equipmentPoint2 .

推荐阅读