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

Upload data in file fu-data.ttl:

curl -XPOST --data-binary @fu-data.ttl    \  
     --header 'Content-type: text/turtle' \  

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' \  


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

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

API 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);
    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

Jena can read SHACL Compact Syntax. The file extensions are .shc and .shaclc and there is a registered language constant Lang.SHACLC.


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