Apache Jena SHACL

jena-shacl is an implementation of the W3C Shapes Constraint Language (SHACL). It implements SHACL Core and SHACL SPARQL Constraints. It also provides a reader and writer for SHACL Compact Syntax.

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 defaults 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.

shacl p --out=FMT FILE

writes out in text(t), compact(c), rdf(r) formats. Multiple formats can be given, separated by “,” and format all outputs all 3 formats.

Integration with Apache Jena Fuseki

Fuseki has a new service operation fuseki:shacl:

<#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:endpoint [ fuseki:operation fuseki:shacl ;
                  fuseki:name "shacl" ];

is necessary (or programmatic setup for Fuseki Main).

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

There is a 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.

Further, an argument target=uri validates a specific node in the data.

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 validated according to the shapes provided.

SHACL Compact Syntax

Apache Jena supports SHACL Compact Syntax (SHACL-C) for both reading and writing.

The file extensions for SHACL-C are .shc and .shaclc and there is a registered language constant Lang.SHACLC.

RDFDataMgr.load("shapes.shc");

RDFDataMgr.read("file:compactShapes", Lang.SHACLC);

RDFDataMgr.write(System.out, shapesGraph, Lang.SHACLC);

SHACL-C is managed by the SHACL Community Group. It does not cover all possible shapes. When outputting SHACL-C, SHACL shapes not expressible in SHACL-C will cause an exception and data in the RDF graph that is not relevant will not be output. In other words, SHACL-C is a lossy format for RDF.

The Jena SHACL-C writer will output any valid SHACL-C document.