How can we import shapes (dash) for SHACL validation in Apache Jena?

108 views Asked by At

How can we import shapes for SHACL validation in Apache Jena?

I am trying to use the DASH (https://datashapes.org/dash.html) shapes vocabulary to validate an ontology in Jena. The code is quite simple:

        String SHAPES = """
                @prefix : <https://example.com/> .
                @prefix sh: <http://www.w3.org/ns/shacl#> .
                @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
                @prefix owl: <http://www.w3.org/2002/07/owl#> .
                @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
                @prefix dash: <http://datashapes.org/dash#> .
                                
                <https://example.com/> a owl:Ontology ;
                  owl:imports <http://datashapes.org/dash> .
                                
                :person a sh:NodeShape ; a rdfs:Class ;
                  dash:closedByTypes true ;
                  sh:ignoredProperties (
                    rdf:type
                  ) ;
                  sh:property [
                    sh:path :name ;
                  ]
                .
                """;

        String DATA = """
                @prefix : <https://example.com/> .
                @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
                :person-1 a :person ;
                  :name "John"^^xsd:string ;
                .  
                """;

RDFParser shapesTurtleParser = RDFParser.source(new ByteArrayInputStream(SHAPES.toString().getBytes(StandardCharsets.UTF_8)))
       .forceLang(Lang.TTL)
       .build();

Graph shapesGraph = shapesTurtleParser.toGraph();

// If I don't put this line, the validation pass. 
shapesGraph = Imports.withImports(shapesGraph);

RDFParser dataTurtleParser = RDFParser.source(new ByteArrayInputStream(DATA.toString().getBytes(StandardCharsets.UTF_8)))
       .forceLang(Lang.TTL)
       .build();

Graph dataGraph = dataTurtleParser.toGraph();

ValidationReport report = ShaclValidator.get().validate(shapesGraph, dataGraph);
RDFDataMgr.write(System.out, report.getModel(), Lang.TTL);

Here is the result from the validation:

@prefix :     <https://example.com/> .
@prefix dash: <http://datashapes.org/dash#> .
@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 tosh: <http://topbraid.org/tosh#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .

[ rdf:type     sh:ValidationReport;
  sh:conforms  false;
  sh:result    [ rdf:type                      sh:ValidationResult;
                 sh:focusNode                  :person-1;
                 sh:resultMessage              "Property <https://example.com/name> is not among those permitted for any of the types";
                 sh:resultPath                 :name;
                 sh:resultSeverity             sh:Violation;
                 sh:sourceConstraintComponent  dash:ClosedByTypesConstraintComponent;
                 sh:sourceShape                :person;
                 sh:value                      "John"
               ]
] .

I get un unexpected validation error: sh:ClosedConstraintComponent which I should not have since the property (:name) is declared in the shape itself.

What do I miss or do incorrectly in my SHACL validation?

0

There are 0 answers