Why SPARQLTarget doesn't work with Apache Jena?

121 Views Asked by At

I am having trouble creating a NodeShape with Apache Jena. I would like to define a shape targeting all the named individuals belonging to owl:Class (so excluding blank nodes). This shape would be helpful, for example, for assessing that all the classes of an ontology have at least a label.

I defined this shape

@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix os: <https://w3id.org/OWLunit/shapes/ontology.ttl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .


os:ClassShape
    a sh:NodeShape ;
    sh:target [
        a sh:SPARQLTarget ;
        sh:select """
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            SELECT ?this
            WHERE {
                ?this a owl:Class .
                FILTER(!isBlank(?this))
            }
        """ ;
    ] ;
    sh:property [
        sh:path rdfs:label ;
        sh:minCount 1 ;
    ] .

And, following the instructions provided here I wrote the following code for assessing the shape over the DUL ontology.

    public static void test() {
        String SHAPES = "test_shape.ttl";
        String DATA = "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl";

        String q = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                + "PREFIX owl: <http://www.w3.org/2002/07/owl#> " 
                + "SELECT ?this  WHERE {"
                + " ?this a owl:Class . FILTER NOT EXISTS {?this rdfs:label ?l}" 
                + "FILTER (!isBlank(?this))}";

        Graph shapesGraph = RDFDataMgr.loadGraph(SHAPES);

        Graph dataGraph = RDFDataMgr.loadGraph(DATA, Lang.RDFXML);

        System.out.println(ResultSetFormatter
                .asText(QueryExecutionFactory.create(q, ModelFactory.createModelForGraph(dataGraph)).execSelect()));

        Shapes shapes = Shapes.parse(shapesGraph);

        System.out.println("Number of shapes " + shapes.getTargetShapes().size());

        ValidationReport report = ShaclValidator.get().validate(shapes, dataGraph);
        System.out.println(report.conforms());
        ShLib.printReport(report);
    }

The execution of the method test() produces the following results:

--------------------------------------------------------------------------------
| this                                                                         |
================================================================================
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#DesignedSubstance>    |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#TimeIndexedRelation>  |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#ObjectAggregate>      |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#SpatioTemporalRegion> |
| <http://www.w3.org/2002/07/owl#Thing>                                        |
| <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#InformationEntity>    |
--------------------------------------------------------------------------------

Number of shapes 0
true
Conforms

Notice that 6 classes defined in the ontology that don't have a label (query q print them), therefore the ontology don't comply with the shape. No shapes are loaded, therefore DUL results compliant with shape.

There is something wrong going on here. Is due to the shape or the code?

PS: I'm using apache-jena 4.0.0

0

There are 0 best solutions below