In my previous post, Rule Execution with SHACL, we have looked at how SHACL rules can be utilized to make inferences. In this post we consider a more complex situation where SHACL rules are used to classify baked goods as vegan friendly or gluten free based on their ingredients.
Why use SHACL and not RDF/RDFS/OWL?
In my discussion I will only concentrate on the definition of vegan friendly baked goods since the translation to gluten free baked goods is similar. Gluten free baked goods are included to give a more representative example.
Essentially what we need to do is look at a baked good and determine whether it includes non-vegan friendly ingredients. If it includes no non-vegan friendly ingredients, we want to assume that it is a vegan friendly baked good. This kind of reasoning uses what is called closed world reasoning, i.e. when a fact does not follow from the data, it is assumed to be false. SHACL uses closed world reasoning and hence the reason for why it is a good fit for this problem.
RDF/RDFS/OWL uses open world reasoning, which means when a fact does not follow from data or schema, it cannot derive that the fact is necessarily false. Rather, it is both possible (1) that the fact holds but it is not captured in data (or schema), or (2) the fact does not hold. For this reason RDF/RDFS/OWL will only infer that a fact holds (or does not hold) if it explicitly stated in the data or can be derived from a combination of data and schema information. Hence, for this reason RDF/RDFS/OWL are not a good fit for this problem.
Baked Goods Data
Below are example baked goods RDF data:
A couple of points are important w.r.t. the RDF data:
- Note that we define both
NonVeganFriendlyingredients to be able to identify ingredients completely. Importantly we state that
NonVeganFriendlyare disjoint so that we cannot inadvertently state that an ingredient is both
- We state that
AppleTartDare of type
BakedGoodso that when we specify our rules, we can state that the rules are applicable only to instances of type
- We enforce the domain and range for
bakery:hasIngredientwhich results in whenever we say
bakery:a bakery:hasIngredient bakery:b, the reasoner can infer that
bakery:ais of type
bakery:bis of type
Baked Good Rules
Now we define the shape of a baked good:
We state that
bakery:BakedGood a rdfs:Class which is important to be able to apply rules to instances of
bakery:BakedGood. We also state that
bakery:BakedGood a sh:NodeShape which allows us to add shape and rule information to
bakery:BakedGood. Note that our
bakery:BakedGood shape state that a baked good has at least one property called
bakery:hasIngredient with range
We now add a
which we will use in the rule definition of
We add two rules, one for identifying a
bakery:VeganBakedGood and one for a
bakery:NonVeganBakedGood. Note that these rules are of type
sh:TripleRule, which will infer the existence of a new triple if the rule is triggered. The first rule states that the subject of this triple is sh:this, which refers to instances of our
bakery:BakedGood class. The predicate is
rdf:type and the object is
bakery:VeganBakedGood. So if this rule is triggered it will infer that an instance of
bakery:BakedGood is also an instance of type
Both rules have two conditions which instances must adhere to before these rules will trigger. These rules will only apply to instances of
bakery:BakedGood according to the first condition. The second condition of the rule for
bakery:VeganBakedGood checks for
bakery:hasIngredient properties of the shape
bakery:NonVeganFriendly. This ensures that the range of
bakery:hasIngredient is of type
bakery:hasIngredient has a maximum count of 0, it will infer that this instance of
bakery:BakedGood is of type
bakery:VeganBakedGood. The rule for
bakery:NonVeganBakedGood will also check for
bakery:hasIngredient properties of the shape
bakery:NonVeganFriendly, but with minimum count of 1 for which it will then infer that this instance is of type
Jena SHACL Rule Execution Code
The Jena SHACL implementation provides command line scripts (
/bin/shaclinfer.bat) which takes as arguments a data file and a shape file which can be used to do rule execution. However, for this specific example you have to write your own Java code. The reason being that the scripts creates a default model that has no reasoning support. In this section I provide the SHACL Jena code needed to do the classification of baked goods.
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:
In this post I gave a brief overview of how SHACL can be used to do classification based on some property. This code example is available at shacl tutorial. This post was inspired by a question on Stack Overflow.
If you have any questions regarding SHACL or the semantic web, please leave a comment and I will try to help where I can.
Hey Henriette! I’ve been modelling a client domain in OWL/SWRL and I think I am at the practical limit of open-world reasoning, or at least I am not willing to have my brain hurt anymore. Lol. A question: can SHACL/SPARQL do everything OWL/SWRL can? With SHACL/SPARQL, I can still use RDFS subclasses, I can do property path queries, I can assert rules for data validation… I guess what I want to ask is: when is OWL/SWRL preferable over SHACL/SPARQL (and vice versa) for an RDFS ontology?
I ask because posts like these:
indicate to me that SemWeb professionals have shifted away from OWL because of its complexity. To me, it seems OWL is only really prevalent for the biomed domain, where absolute perfection is needed. For other domains (like the one I am working in right now), OWL is too complex and the reasoning is too hard to determine. SHACL just seems preferable for smaller projects.
I think it is rather the case that the semantic web has realized that closed world reasoning has a place (as seen in SHACL). When dealing with the situation where you never have complete knowledge, the open world assumption makes sense (as seen in OWL). Personally I see SHACL and OWL as complementary rather than being in competition with each other. I.e., OWL is incredibly useful to infer sophisticated classification hierarchies that improves maintenance of these hierarchies (see
https://oboacademy.github.io/obook/explanation/logical-axiomatization/#let-the-reasoner-do-the-work). Moreover, the inference of these classification hierarchies can be done in the absence of data using an OWL reasoner. When you want to upload data, that data is often assumed to be complete wrt some context. Hence, in that case using SHACL/Shex makes sense.