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 have a file
person.ttl that contains the following data:
Example RDF data
To validate this data we create a shape definition in
Person shape definition
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.
SHACL Maven dependency
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:
Java code using Jena implementation of SHACL
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:ssn property does not conform to the pattern defined in the shape.
ex:Bob has 2
ex:Calvin works for a company that is not of type
ex:Calvin has a property
ex:birthDate that is not allowed by
ex:PersonShape since it is close by
A corrected version of our person data may look as follows:
Person data that conforms to our person shape
In this post I have 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.