Search code examples
validationrdfshacl

How to resolve Predicate is not allowed (closed shape) validation error


I am using https://shacl.org/playground/

I have the following Shape Graph:

@prefix hr: <http://learningsparql.com/ns/humanResources#> .
@prefix d:  <http://learningsparql.com/ns/data#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

hr:ClassShape
    a sh:NodeShape ;
    sh:targetSubjectsOf rdf:type;

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

    sh:closed true ;
.

I have the following Data Graph

@prefix hr: <http://learningsparql.com/ns/humanResources#> .
@prefix d:  <http://learningsparql.com/ns/data#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

#### Regular RDFS modeling ####

hr:Employee a rdfs:Class .

hr:Another a rdfs:Class .

hr:name
   rdf:type rdf:Property ; .

hr:hireDate
   rdf:type rdf:Property ; .

hr:jobGrade
   rdf:type rdf:Property ; .

I want to verify that every node which declares a rdf:type has a value of either rdfs:Class or rdf:Property.

I am getting the following validation errors:

[
    a sh:ValidationResult ;
    sh:resultSeverity sh:Violation ;
    sh:sourceConstraintComponent sh:ClosedConstraintComponent ;
    sh:sourceShape hr:ClassShape ;
    sh:focusNode hr:Employee ;
    sh:resultPath rdf:type ;
    sh:value rdfs:Class ;
    sh:resultMessage "Predicate is not allowed (closed shape)" ;
] .
[
    a sh:ValidationResult ;
    sh:resultSeverity sh:Violation ;
    sh:sourceConstraintComponent sh:ClosedConstraintComponent ;
    sh:sourceShape hr:ClassShape ;
    sh:focusNode hr:Another ;
    sh:resultPath rdf:type ;
    sh:value rdfs:Class ;
    sh:resultMessage "Predicate is not allowed (closed shape)" ;
] .
[
    a sh:ValidationResult ;
    sh:resultSeverity sh:Violation ;
    sh:sourceConstraintComponent sh:ClosedConstraintComponent ;
    sh:sourceShape hr:ClassShape ;
    sh:focusNode hr:name ;
    sh:resultPath rdf:type ;
    sh:value rdf:Property ;
    sh:resultMessage "Predicate is not allowed (closed shape)" ;
] .
[
    a sh:ValidationResult ;
    sh:resultSeverity sh:Violation ;
    sh:sourceConstraintComponent sh:ClosedConstraintComponent ;
    sh:sourceShape hr:ClassShape ;
    sh:focusNode hr:hireDate ;
    sh:resultPath rdf:type ;
    sh:value rdf:Property ;
    sh:resultMessage "Predicate is not allowed (closed shape)" ;
] .
[
    a sh:ValidationResult ;
    sh:resultSeverity sh:Violation ;
    sh:sourceConstraintComponent sh:ClosedConstraintComponent ;
    sh:sourceShape hr:ClassShape ;
    sh:focusNode hr:jobGrade ;
    sh:resultPath rdf:type ;
    sh:value rdf:Property ;
    sh:resultMessage "Predicate is not allowed (closed shape)" ;
] .

I am not sure why or what I need to do to resolve them. I believe all of the validation errors are related, so the solution to one should provide the solution to the rest.

What should my Shape file look like?


Solution

  • You confused the OR-statement, here is a working example following the SHACL docs on sh:or

    @prefix hr: <http://learningsparql.com/ns/humanResources#> .
    @prefix d:  <http://learningsparql.com/ns/data#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    
    hr:ClassShape
      a sh:NodeShape ;
      sh:targetSubjectsOf rdf:type;
    
      sh:property [
        sh:path rdf:type ;
        sh:nodeKind sh:IRI ;
        sh:or (
          [sh:hasValue rdfs:Class;]
          [sh:hasValue rdf:Property;]
        )
      ]; 
    
      sh:closed true ;
    .