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:

rectangles.ttl
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 rectangleRules.ttl
):

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

SHACL rule execution using Jena
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:

inference.ttl
Note that ex:InvalidRectangle
has been ignored because it does not adhere to sh:condition ex:Rectangle
, since it does not have ex:height
and ex:width
properties. Also, ex:NonSquareRectangle
is a rectangle, not a square.
Conclusion
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.