In my previous post, Using Jena and SHACL to validate RDF Data, I have looked at how RDF data can be validated using SHACL. A closely related concern to that of constraints checking, is rule execution, for which SHACL can also be used.
A SHACL Rule Example
We will again use an example from the SHACL specification. Assume we have the a
file rectangles.ttl that contains the following data:
Assuming we want to infer that when the height and width of a rectangle are equal, the rectangle represents a square, the following SHACL rule specification can be used (which we will store in
A Code Example using Jena
Naturally you will need to add SHACL to your Maven pom dependencies. Then the following code will execute your SHACL rules:
Running the Code
Running the code will cause an
inferences.ttl file to be written out to
$Project/src/main/resources/. It contains the following output:
ex:InvalidRectangle has been ignored because it does not adhere to
sh:condition ex:Rectangle, since it does not have
ex:width properties. Also,
ex:NonSquareRectangle is a rectangle, not a square.
In this post I gave a brief overview of how SHACL can be used to implement rules on RDF data. This code example is available at shacl tutorial.