相当于 JSON-Schema“uniqueItems”的 SHACL

问题描述

所以我的任务是将描述本体的 JSON-Schema 转换为 SHACL。

JSON-Schema 具有 uniqueItems 构造,顾名思义,它强制数组中的所有元素都是唯一的。就我而言,仅考虑 String 类型的元素。

SHACL 中是否有类似的结构? sh:disjoint 需要明确的唯一路径,因此它不适用。我正在考虑创建一个 SPARQL 约束,但由于我的实例都是匿名节点,我还没有能够让它工作。

编辑:添加示例

有效的 JSON:

{
                "name": "Robert Lewandowski","mBox": "[email protected]","contributor_id": {
                    "identifier": "https://orcid.org/TEST","type": "orcid"
                },"role": [
                    "ContactPerson","DataManager"
                ]
            }

无效的 JSON:

{
                "name": "Robert Lewandowski","ContactPerson"
                ]
            }

有效的 TTL:

madmp:contributor [ foaf:mBox "[email protected]" ;
                    foaf:name "Robert Lewandowski" ;
                    madmp:contributor_id  [ terms:identifier "https://orcid.org/TEST" ;
                                           madmp:identifier_type  "orcid"
                                          ] ;
                    madmp:role ( "ContactPerson" "DataManager")
                  ] ;

无效的 TTL:

madmp:contributor [ foaf:mBox "[email protected]" ;
                    foaf:name "Robert Lewandowski" ;
                    madmp:contributor_id  [ terms:identifier "https://orcid.org/TEST" ;
                                           madmp:identifier_type  "orcid"
                                          ] ;
                    madmp:role ( "ContactPerson" "ContactPerson")
    

解决方法

所以这是我想出的解决方案。它使用 dash vocabulary 首先确保 rdf:List 结构有效。然后 SPARQL 约束确保这些值是唯一的。

它可能不漂亮,但对我有用。

sh:property [
            sh:path madmp:role;
            sh:name "The Role Schema";
            sh:description "Type of contributor";
            sh:node dash:ListShape;
            sh:minCount 1 ;
            sh:property [
                sh:path ( [ sh:zeroOrMorePath rdf:rest ] rdf:first ) ;
                sh:datatype xsd:string ;
                sh:minCount 1 ;
            ]
        ];
        sh:sparql [
            sh:message "Contributor {?name} has role {?role} more than once ({?roleCount} times).";
            sh:prefixes (madmp: rdf: foaf:);
            sh:select """
              PREFIX madmp: <https://w3id.org/madmp/terms#>
              PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
              prefix foaf:  <http://xmlns.com/foaf/0.1/>

              SELECT $this ?role ?name (COUNT(?role) AS ?roleCount)
              WHERE {
                  $this $PATH ?contributor.
                  ?contributor madmp:role/rdf:rest*/rdf:first ?role;
                  foaf:name ?name
              }
              GROUP BY $this ?name ?role
              HAVING (?roleCount > 1)
            """
        ]