Apache Jena Shacl

jena-shacl is an implementation of the W3C Shapes Constraint Language (SHACL). It implements SHACL Core and SHACL SPARQL Constraints.

Command line

The command shacl introduces shacl operations; it takes a sub-command argument.

To validate:

shacl validate --shapes SHAPES.ttl --data DATA.ttl
shacl v -s SHAPES.ttl -d DATA.ttl

The shapes and data files can be the same; the --shapes is optional and default to the same as --data. This includes running individual W3C Working Group tests.

To parse a file

shacl parse FILE
shacl p FILE

which writes out a text format.

Integration with Apache Jena Fuseki

Fuseki has a new service operation fuseki:serviceShacl:

<#serviceInMemoryShacl> rdf:type fuseki:Service ;
    rdfs:label                   "Dataset with SHACL validation" ;
    fuseki:name                  "ds" ;
    fuseki:serviceReadWriteGraphStore "" ;
    fuseki:endpoint [ fuseki:operation fuseki:shacl ; fuseki:name "shacl" ] ;
    fuseki:dataset <#dataset> ;
    .

This requires a "new style" endpoint declaration: see "Fuseki Endpoint Configuration".

This is not installed into a dataset setup by default; a configuration file using fuseki:serviceShacl is necessary (or programmatic setup for Fuskei Main).

The service accepts a shapes graph posted as RDF to /ds/shacl with content negotiation.

There is an graph argument, ?graph=, that specifies the graph to validate. It is the URI of a named graph, default for the unnamed, default graph (and this is the assumed value of ?graph if not present), or union for union of all named graphs in the dataset.

Upload data in file fu-data.ttl:

curl -XPOST --data-binary @fu-data.ttl    \  
     --header 'Content-type: text/turtle' \  
     'http://localhost:3030/ds?default'

Validate with shapes in fu-shapes.ttl and get back a validation report:

curl -XPOST --data-binary @fu-shapes.ttl  \  
     --header 'Content-type: text/turtle' \  
     'http://localhost:3030/ds/shacl?graph-default'

API

The package org.apache.jena.shacl has the main classes.

  • ShaclValidator for parsing and validation
  • GraphValidation for updating graphs with validation

API Examples

https://github.com/apache/jena/tree/master/jena-shacl/src/main/java/org/apache/jena/shacl/examples

Example Shacl01_validateGraph shows validation and printing of the validation report in a text form and in RDF:

public static void main(String ...args) {
    String SHAPES = "shapes.ttl";
    String DATA = "data1.ttl";

    Graph shapesGraph = RDFDataMgr.loadGraph(SHAPES);
    Graph dataGraph = RDFDataMgr.loadGraph(DATA);

    Shapes shapes = Shapes.parse(shapesGraph);

    ValidationReport report = ShaclValidator.get().validate(shapes, dataGraph);
    ShLib.printReport(report);
    System.out.println();
    RDFDataMgr.write(System.out, report.getModel(), Lang.TTL);
}

Example Shacl02_validateTransaction shows how to update a graph only if, after the changes, the graph is validate according to the shapes provided.