以特定 class 的所有目标为目标,但 SHACL 中的指定实例除外
Target all of a certain class, except for a specified instance in SHACL
我想使用 SHACL 检查建设项目的传入数据集。为此,我在 class 级别定义了几个约束。在我的用例中,我需要能够针对指定实例的这些约束做出项目特定的例外。这在 SHACL 中可能吗?如何建模?
最终,我想在不同的形状图 (SG) 中描述这些约束:
- 一般SG,无一例外地守规矩;
- 项目 SG,它包含特定项目相关的约束和通用 SG 中约束的例外情况。
我可以想到诸如 SHACL-SPARQL 之类的解决方法,或者先针对一般 SG 检查,然后使用过滤器忽略项目特定的异常,但我想知道是否有针对这种情况的更干净的解决方案。
这里有一个简化的例子来说明这个问题:
数据图可能如下所示:
@prefix schema: <http://www.example.org/schema#> .
@prefix : <http://www.example.org/data#> .
schema:Building a owl:Class .
:CorrectBuilding a schema:Building ;
schema:owner "John Doe" ;
schema:otherProp "Some other prop" .
:IncorrectBuilding a schema:Building ;
schema:otherProp "Some other prop" .
:BuildingWithException a schema:Building ;
schema:otherProp "Some other prop" .
一般的 SG 可能是这样的:
@prefix schema: <http://www.example.org/schema#> .
@prefix : <http://www.example.org/data#> .
@prefix gsg: <http://www.example.org/generalsg#>
gsg:PersonShape
a sh:NodeShape ;
sh:targetClass schema:Building ;
sh:property [
sh:path schema:owner ;
sh:minCount 1 ;
] .
使用上述和项目特定的 SG,验证者应该 return 违反 :IncorrectBuilding
,但不违反 :BuildingWithException
。
如何为 :BuildingWithException
创建项目特定的例外?
感谢阅读,让我知道您的想法。
我认为这是使用 SHACL-AF Custom Targets 的情况,如果您可以在您的环境中使用它们:
@prefix schema: <http://www.example.org/schema#> .
@prefix : <http://www.example.org/data#> .
@prefix gsg: <http://www.example.org/generalsg#>
@prefix sh: <http://www.w3.org/ns/shacl#> .
gsg:
sh:declare [
sh:prefix "schema" ;
sh:namespace "http://www.example.org/schema#" ;
] ;
sh:declare [
sh:prefix "data" ;
sh:namespace "http://www.example.org/data#" ;
] .
gsg:PersonShapeWithException
a sh:NodeShape ;
sh:target [
a sh:SPARQLTarget ;
sh:prefixes gsg: ;
sh:select """
SELECT ?node
WHERE {
?node a schema:Building
FILTER (?node != data:IncorrectBuilding)
}
""" ;
] ;
sh:property [
sh:path schema:owner ;
sh:minCount 1 ;
] .
产生:
[ a sh:ValidationReport ;
sh:conforms false ;
sh:result [ a sh:ValidationResult ;
sh:focusNode :BuildingWithException ;
sh:resultMessage "Property needs to have at least 1 values, but found 0" ;
sh:resultPath schema:owner ;
sh:resultSeverity sh:Violation ;
sh:sourceConstraintComponent sh:MinCountConstraintComponent ;
sh:sourceShape []
]
] .
我想使用 SHACL 检查建设项目的传入数据集。为此,我在 class 级别定义了几个约束。在我的用例中,我需要能够针对指定实例的这些约束做出项目特定的例外。这在 SHACL 中可能吗?如何建模?
最终,我想在不同的形状图 (SG) 中描述这些约束:
- 一般SG,无一例外地守规矩;
- 项目 SG,它包含特定项目相关的约束和通用 SG 中约束的例外情况。
我可以想到诸如 SHACL-SPARQL 之类的解决方法,或者先针对一般 SG 检查,然后使用过滤器忽略项目特定的异常,但我想知道是否有针对这种情况的更干净的解决方案。
这里有一个简化的例子来说明这个问题:
数据图可能如下所示:
@prefix schema: <http://www.example.org/schema#> .
@prefix : <http://www.example.org/data#> .
schema:Building a owl:Class .
:CorrectBuilding a schema:Building ;
schema:owner "John Doe" ;
schema:otherProp "Some other prop" .
:IncorrectBuilding a schema:Building ;
schema:otherProp "Some other prop" .
:BuildingWithException a schema:Building ;
schema:otherProp "Some other prop" .
一般的 SG 可能是这样的:
@prefix schema: <http://www.example.org/schema#> .
@prefix : <http://www.example.org/data#> .
@prefix gsg: <http://www.example.org/generalsg#>
gsg:PersonShape
a sh:NodeShape ;
sh:targetClass schema:Building ;
sh:property [
sh:path schema:owner ;
sh:minCount 1 ;
] .
使用上述和项目特定的 SG,验证者应该 return 违反 :IncorrectBuilding
,但不违反 :BuildingWithException
。
如何为 :BuildingWithException
创建项目特定的例外?
感谢阅读,让我知道您的想法。
我认为这是使用 SHACL-AF Custom Targets 的情况,如果您可以在您的环境中使用它们:
@prefix schema: <http://www.example.org/schema#> .
@prefix : <http://www.example.org/data#> .
@prefix gsg: <http://www.example.org/generalsg#>
@prefix sh: <http://www.w3.org/ns/shacl#> .
gsg:
sh:declare [
sh:prefix "schema" ;
sh:namespace "http://www.example.org/schema#" ;
] ;
sh:declare [
sh:prefix "data" ;
sh:namespace "http://www.example.org/data#" ;
] .
gsg:PersonShapeWithException
a sh:NodeShape ;
sh:target [
a sh:SPARQLTarget ;
sh:prefixes gsg: ;
sh:select """
SELECT ?node
WHERE {
?node a schema:Building
FILTER (?node != data:IncorrectBuilding)
}
""" ;
] ;
sh:property [
sh:path schema:owner ;
sh:minCount 1 ;
] .
产生:
[ a sh:ValidationReport ;
sh:conforms false ;
sh:result [ a sh:ValidationResult ;
sh:focusNode :BuildingWithException ;
sh:resultMessage "Property needs to have at least 1 values, but found 0" ;
sh:resultPath schema:owner ;
sh:resultSeverity sh:Violation ;
sh:sourceConstraintComponent sh:MinCountConstraintComponent ;
sh:sourceShape []
]
] .