简体   繁体   English

相当于 JSON-Schema “uniqueItems”的 SHACL

[英]SHACL equivalent of JSON-Schema “uniqueItems”

So I'm tasked with converting a JSON-Schema describing an ontology to SHACL.所以我的任务是将描述本体的 JSON-Schema 转换为 SHACL。

JSON-Schema has the uniqueItems construct, which, as the name suggests, forces all elements in an array to be unique. JSON-Schema 具有uniqueItems构造,顾名思义,它强制数组中的所有元素都是唯一的。 For my purposes, only elements of type String are considered.就我而言,只考虑 String 类型的元素。

Is there a similar construct in SHACL? SHACL 中是否有类似的结构? sh:disjoint requires explicit unique paths so it doesn't apply. sh:disjoint需要明确的唯一路径,因此它不适用。 I'm considering creating a SPARQL constraint, though since my instances are all anonymous nodes, I haven't been able to get it to work yet.我正在考虑创建一个 SPARQL 约束,但由于我的实例都是匿名节点,我还没有能够让它工作。

Edit: Adding examples编辑:添加示例

Valid JSON:有效的 JSON:

{
                "name": "Robert Lewandowski",
                "mbox": "robert@bayern.de",
                "contributor_id": {
                    "identifier": "https://orcid.org/TEST",
                    "type": "orcid"
                },
                "role": [
                    "ContactPerson",
                    "DataManager"
                ]
            }

Invalid JSON:无效的 JSON:

{
                "name": "Robert Lewandowski",
                "mbox": "robert@bayern.de",
                "contributor_id": {
                    "identifier": "https://orcid.org/TEST",
                    "type": "orcid"
                },
                "role": [
                    "ContactPerson",
                    "ContactPerson"
                ]
            }

Valid TTL:有效的 TTL:

madmp:contributor [ foaf:mbox "robert@bayern.de" ;
                    foaf:name "Robert Lewandowski" ;
                    madmp:contributor_id  [ terms:identifier "https://orcid.org/TEST" ;
                                           madmp:identifier_type  "orcid"
                                          ] ;
                    madmp:role ( "ContactPerson" "DataManager")
                  ] ;

Invalid TTL: TTL 无效:

madmp:contributor [ foaf:mbox "robert@bayern.de" ;
                    foaf:name "Robert Lewandowski" ;
                    madmp:contributor_id  [ terms:identifier "https://orcid.org/TEST" ;
                                           madmp:identifier_type  "orcid"
                                          ] ;
                    madmp:role ( "ContactPerson" "ContactPerson")
    

So this is a solution I came up with.所以这是我想出的解决方案。 It uses the dash vocabulary to first make sure the rdf:List structure is valid.它使用破折号词汇首先确保rdf:List结构有效。 The SPARQL constraint then makes sure that the values are unique. SPARQL 约束然后确保这些值是唯一的。

It might not be pretty, but it works for me.它可能不漂亮,但对我有用。

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)
            """
        ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM