这些使用 sh:or 的形状图有什么区别?

What is the difference between these shape graphs which use sh:or?

我有以下数据图。

@prefix hr: <http://learningsparql.com/ns/humanResources#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sch: <http://schema.org/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

hr:Another a rdfs:Class .

hr:Employee a rdfs:Class ;
    rdfs:label "model" ;
    rdfs:comment "a good employee" .

hr:Longer a hr:Employee ;
    rdfs:label "model" ;
    rdfs:comment "a good employee" .

hr:freestanding a rdf:Property ;
    sch:rangeIncludes sch:Text .

hr:missing rdfs:comment "some comment about missing" .

hr:name a rdf:Property ;
    sch:domainIncludes hr:Employee .

hr:nosuper a rdf:Property ;
    sch:domainIncludes hr:Uncreated ;
    sch:rangeIncludes sch:Text .

hr:randomtype a hr:invalidtype ;
    rdfs:label "some label about randomtype" ;
    rdfs:comment "some comment about randomtype" .

hr:typo a rdfs:Classs ;
    rdfs:label "some label about typo" ;
    rdfs:comment "some comment about typo" .

我试图理解这两个形状图之间的区别,我认为(错误地!)应该 return 相同的结果...hr:typo、[=40= 的验证错误], 和 hr:missing 因为没有 rdf:type 属性 到 rdfs:Class 或 rdf:Property.

的路径

以下形状图产生了预期的验证错误。

(A) -- 好结果

            @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
            @prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
            @prefix sch:  <http://schema.org/> .
            @prefix sh:   <http://www.w3.org/ns/shacl#> .
            @prefix ex:   <http://example.org/> .

            ex:ClassShape
                a sh:NodeShape ;

                sh:property [
                    sh:path [ sh:zeroOrMorePath rdf:type ];
                    sh:nodeKind sh:IRI ;
                    sh:hasValue rdfs:Class;
                    sh:message "class" ;
                ] .

            ex:PropertyShape
                a sh:NodeShape ;

                sh:property [
                    sh:path [ sh:zeroOrMorePath rdf:type ];
                    sh:nodeKind sh:IRI ;
                    sh:hasValue rdf:Property;
                    sh:message "property" ;
                ] .

            ex:ClassOrProperty
                a sh:NodeShape ;
                sh:target [
                    a sh:SPARQLTarget ;
                    sh:select   """
                                SELECT ?this
                                WHERE {
                                    ?this ?p ?o .
                                }
                                """ ;
                ] ;

                sh:or (
                    ex:ClassShape
                    ex:PropertyShape
                );
            .            

(A) 产生的良好和预期的验证错误是:

Validation Report
Conforms: False
Results (3):
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: ex:ClassOrProperty
    Focus Node: hr:randomtype
    Value Node: hr:randomtype
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: ex:ClassOrProperty
    Focus Node: hr:typo
    Value Node: hr:typo
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: ex:ClassOrProperty
    Focus Node: hr:missing
    Value Node: hr:missing

但是,这个形状图:

(B) -- 不好的结果

                    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
                    @prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
                    @prefix sch:  <http://schema.org/> .
                    @prefix sh:   <http://www.w3.org/ns/shacl#> .
                    @prefix ex:  <http://example.org/> .

                    ex:ClassOrProperty
                        a sh:NodeShape ;
                        sh:target [
                            a sh:SPARQLTarget ;
                            sh:select   """
                                        SELECT ?this
                                        WHERE {
                                            ?this ?p ?o .
                                        }
                                        """ ;
                        ] ;

                        sh:property [
                            sh:path     [sh:zeroOrMorePath rdf:type] ;
                            sh:nodeKind sh:IRI ;
                            sh:or (
                                [ sh:hasValue rdfs:Class;   ]
                                [ sh:hasValue rdf:Property; ]
                            )
                        ];
                    .

不会只产生预期的验证错误。我得到:

Validation Report
Conforms: False
Results (12):
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:freestanding
    Value Node: hr:freestanding
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:name
    Value Node: hr:name
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:Another
    Value Node: hr:Another
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:nosuper
    Value Node: hr:nosuper
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:Employee
    Value Node: hr:Employee
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:randomtype
    Value Node: hr:randomtype
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:randomtype
    Value Node: hr:invalidtype
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:typo
    Value Node: hr:typo
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:typo
    Value Node: rdfs:Classs
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:missing
    Value Node: hr:missing
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:Longer
    Value Node: hr:Employee
    Result Path: [ sh:zeroOrMorePath rdf:type ]
Constraint Violation in OrConstraintComponent (http://www.w3.org/ns/shacl#OrConstraintComponent):
    Severity: sh:Violation
    Source Shape: [ sh:nodeKind sh:IRI ; sh:or ( [ sh:hasValue rdfs:Class ] [ sh:hasValue rdf:Property ] ) ; sh:path [ sh:zeroOrMorePath rdf:type ] ]
    Focus Node: hr:Longer
    Value Node: hr:Longer
    Result Path: [ sh:zeroOrMorePath rdf:type ]

为什么结果不同?

我喜欢 (B) 而不是 (A) 的原因是,如果它有效的话,它会更简洁。

双方 pySHACL and TopBraid SHACL API shaclvalidate.sh 都同意结果。

了解 属性 路径的工作原理很重要。路径用于到达值。当使用 sh:path [sh:zeroOrMorePath rdf:path] 并考虑节点 hr:Longer 时,它将达到三个值 -- (0) hr:Longer、(1) hr:Employee 和 (2) rdfs:Class.

牢记这个概念,(B) 中发生的事情以及为什么它不起作用就可以得到充分的解释。

(A) 和 (B) 具有相同的目标定义,并且将 return 相同的焦点节点。它们是:

hr:Another 
hr:Employee
hr:Longer   
hr:freestanding
hr:missing
hr:name      
hr:nosuper
hr:randomtype
hr:typo      

此外,(A) 和 (B) 的共同点是 sh:path [sh:zeroOrMorePath rdf:type] ;。例如,当考虑节点 hr:Longer 时,它将发出三个值,每个值都可能需要检查。这三个值是 (0) hr:Longer、(1) hr:Employee 和 (2) rdfs:Class.

对于 (B),当它考虑 hr:Longer 并将发射值 hr:Longer 传递给 sh:or 时,它发现它既不是 rdfs:Class 也不是 rdf:Property。由于 sh:or 的两个子句都不满足,因此发出验证错误。

要使 (B) 起作用,sh:or 中的两个子句需要更改为 [ sh:path [sh:zeroOrMorePath rdf:type] ; sh:hasValue rdfs:Class; ][ sh:path [sh:zeroOrMorePath rdf:type] ; sh:hasValue rdf:Property; ]。在这种情况下,当 hr:Longer 传递到 sh:or 时,每个子句都会检查整个路径,而 sh:hasValue 只需要匹配路径发出的三个值之一。

(B) - 工作

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sch:  <http://schema.org/> .
@prefix sh:   <http://www.w3.org/ns/shacl#> .
@prefix ex:  <http://example.org/> .

ex:ClassOrProperty
    a sh:NodeShape ;
    sh:target [
        a sh:SPARQLTarget ;
        sh:select   """
                    SELECT ?this
                    WHERE {
                        ?this ?p ?o .
                    }
                    """ ;
    ] ;

    sh:property [
        sh:path     [sh:zeroOrMorePath rdf:type] ;
        sh:nodeKind sh:IRI ;
        sh:or (
            [ sh:path [sh:zeroOrMorePath rdf:type] ; sh:hasValue rdfs:Class;   ]
            [ sh:path [sh:zeroOrMorePath rdf:type] ; sh:hasValue rdf:Property; ]
        )
    ];
.

现在考虑(A),每个焦点节点都传递给ex:PropertyShapeex:ClassShape。如果它针对其中一种形状进行验证,它将进行验证。两种形状的相似之处在于它们都使用路径 sh:path [ sh:zeroOrMorePath rdf:type ];。因为它们使用 sh:hasValue,所以路径的发射值中只有一个需要匹配。再次考虑 hr:Longer,因为路径将发出值 rdfs:Class,它根据 ex:ClassShape 进行验证并且不会生成验证错误。