RDF enables users to capture data in a way that is intuitive to them. This means that data is often captured without conforming to any schema. It is often useful to know that an RDF dataset conforms to some (potential partial) schema. This is where SHACL (SHApe Constraint Language), a W3C standard, comes into play. It is a language for describing and validating RDF graphs. In this post I will give a brief overview of how to use SHACL to validate RDF data using the Jena implementation of SHACL.
A SHACL Example
We will use an example from the SHACL specification. Assume we hav a file
person.ttl that contains the following data:
To validate this data we create a shape definition in
A Code Example using Jena
To validate our RDF data using our SHACL shape we will use the Jena implementation of SHACL. Start by adding the SHACL dependency to your Maven
pom.xml. Note that you do not need to add Jena as well as the SHACL pom already includes Jena.
In the code we will assume the
personShape.ttl files are in
$Project/src/main/resources/. The code for doing the validation is the following then:
Running the Code
Running the code will cause a
report.ttl file to be written out to
$Project/src/main/resources/. We can determine that our data does not conform by checking the
sh:conforms property. We have 4 violations of our
ex:ssnproperty does not conform to the pattern defined in the shape.
ex:Calvinworks for a company that is not of type
ex:Calvinhas a property
ex:birthDatethat is not allowed by
ex:PersonShapesince it is close by
A corrected version of our person data may look as follows:
In this post I given a brief overview of how SHACL can be used to validate RDF data using the SHACL implementation of Jena. This code example is available at shacl tutorial.