[英]SHACL sh:rule with sh:condition in SPARQL in Topbraid Composer
示例本體有兩個類( :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 ;
.
創建了帶有sh:rule
( :SPARQLRule_1
) 的 NodeShape :NodeShapeRule
NodeShapeRule。 此規則創建新的三元組。 使用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"
正確的解釋是 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 ;
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.