0

示例本体有两个类(:MyClass:Value)和两个属性(:MyObjProp:MyDataProp)。

:MyClass
  a owl:Class ;
  a sh:NodeShape ;
  rdfs:subClassOf owl:Thing ;
.
:MyDataProp
  a owl:DatatypeProperty ;
  rdfs:domain :MyClass ;
  rdfs:range xsd:string ;
.
:MyObjProp
  a owl:ObjectProperty ;
  rdfs:domain :MyClass ;
  rdfs:range :Value ;
.
:Value
  a owl:Class ;
  rdfs:subClassOf owl:Thing ;
.

添加了一些实例。

:MyClass_1
  a :MyClass ;
  :MyDataProp :Value_1 ;
  :MyObjProp :Value_1 ;
.
:MyClass_2
  a :MyClass ;
  :MyObjProp :Value_2 ;
.
:Value_1
  a :Value ;
.
:Value_2
  a :Value ;
.

:NodeShapeRule创建了一个带有sh:rule( )的 NodeShape :SPARQLRule_1。此规则创建新的三元组。使用sh:condition该规则应将其限制为目标的子集。

:NodeShapeRule
  a sh:NodeShape ;
  sh:rule :SPARQLRule_1 ;
  sh:targetClass :MyClass ;
.
:SPARQLRule_1
  a sh:SPARQLRule ;
  sh:condition :NodeShapeConditionSPARQL ;
  sh:construct """
    PREFIX : <http://example.org/ex#>
    CONSTRUCT
    {
        $this :MyDataProp \"New input\" .
    }
    WHERE
    {
        $this :MyObjProp ?p .
    }
    """ ;
.

对于限制,定义了两个等效的 NodeShape。第一个约束使用 sh:property,另一个使用 sh:sparql。

:NodeShapeConditionProperty
  a sh:NodeShape ;
  sh:property [
      sh:path :MyObjProp ;
      sh:description "NodeShapeConditionProperty" ;
      sh:hasValue :Value_1 ;
    ] ;
  sh:targetClass :MyClass ;
.
:NodeShapeConditionSPARQL
  a sh:NodeShape ;
  sh:sparql [
      sh:message "NodeShapeConditionSPARQL" ;
      sh:prefixes <http://example.org/ex> ;
      sh:select """
        PREFIX : <http://example.org/ex#>
        SELECT $this
        WHERE
        {
            $this :MyObjProp ?prop .
        }
        """ ;
    ] ;
  sh:targetClass :MyClass ;
.

在使用 Topbraid Composer 进行推理时,我收到了两种解决方案的不同结果。只有带有 sh:property 的解决方案才能提供预期的响应。拜托,有人可以解释一下这种行为吗?

:MyClass_1 :MyDataProp "New input"
4

1 回答 1

1

正确的解释是 SPAQRL 查询为 SELECT 查询中的每个结果(行)产生一个约束冲突。因此,如果 SPARQL 查询没有返回结果(行),那么一切都很好,规则将触发。这种设计的原因是这使得 SPARQL 查询能够返回更多关于违规的信息,例如焦点节点 ( $this) 和值节点 ( ?value)。

更改:NodeShapeConditionSPARQL它会对不存在的结果产生冲突,然后两种解决方案的行为方式相同。

:NodeShapeConditionSPARQL
  a sh:NodeShape ;
  sh:sparql [
      sh:message "NodeShapeConditionSPARQL" ;
      sh:prefixes <http://example.org/ex> ;
      sh:select """
        PREFIX : <http://example.org/ex#>
        SELECT $this
        WHERE
        {
            FILTER NOT EXISTS { $this :MyObjProp ?anyProp } .
        }
        """ ;
    ] ;
  sh:targetClass :MyClass ;
.
于 2021-12-21T16:57:20.107 回答