gpt4 book ai didi

validation - 如何通过 SHACL 强制数据属性值的唯一性

转载 作者:行者123 更新时间:2023-12-03 07:54:31 27 4
gpt4 key购买 nike

我无法弄清楚如何通过 SHACL 强制数据属性值的唯一性。

以下摘录是 Henriette Harmse 提供的示例的精简版本在 her personal blog .

假设我们有以下数据:

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

ex:Alice
a ex:Person ;
ex:ssn "987-65-4321" .

ex:Bob
a ex:Person ;
ex:ssn "987-65-4321" .

下面是相应的形状定义,它允许属性 ex:ssn 的最大值为 1,并限制其格式:

@prefix dash: <http://datashapes.org/dash#> .
@prefix ex: <http://example.com/ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:PersonShape
a sh:NodeShape ;
sh:targetClass ex:Person ;
sh:property
[
sh:path ex:ssn ;
sh:maxCount 1 ;
sh:datatype xsd:string ;
sh:pattern "^\\d{3}-\\d{2}-\\d{4}$" ;
] .

以上数据自然符合,无违规举报。然而,我还想施加一个额外的限制,即每个人的 ssn 号码应该是唯一的,并且没有两个人可以共享相同的 ssn 号码。在这种情况下,数据将无效,因为 ex:Aliceex:Bob 具有相同的 ssn。这如何在 SHACL 中表达?

最佳答案

如果您的应用程序仅限于 SHACL Core,并且您可以稍微放宽每个属性的全局要求,而不是特定于某些类,则可以对 ex:ssn 的所有对象使用反向 sh:maxCount 1 约束。

ex:ssnShape
a sh:NodeShape ;
sh:targetObjectsOf ex:ssn ;
sh:property [
sh:path [ sh:inversePath ex:ssn ] ;
sh:maxCount 1 ;
] .

(注意:我还没有测试过这一点,但目的是对于每个不同的 SSN,它都会检查是否最多有一个指向它的传入链接,并且 ex:ssn 属性沿相反方向行走)。

关于validation - 如何通过 SHACL 强制数据属性值的唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76363045/

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