===== Rules and constraints =====
* Last verification: **20220909**
* Tools required for this lab:
* [[http://protege.stanford.edu/products.php#desktop-protege|Protégé Desktop 5]]
==== Prepare yourself for the lab ====
* {{https://www.ida.liu.se/~robke04/SHACLTutorial/Introduction%20to%20SHACL.pdf|Introduction to SHACL.pdf}}
==== Lab instructions ====
At the end of the lab, each group should email their second project to the teacher. It consist of:
- the final ''*.ttl'' file with the ontology (started on [[.:lab_onto101|Ontology 101]], refined during the [[.:lab_onto2|Advanced ontology engineering]] and extended with SWRL rules during today's lab),
- the second ''*.ttl'' file with SHACL shapes (developed during the second part of today's lab).
=== 0. Prepare the Protégé Desktop [5 minutes] ===
- Run the Protégé Desktop.
- Open ''File'' -> ''Check for plugins...''
- Select tickbox on the left side of two plugins (if they are not listed, they are already installed in the newest version):
- ''SWRLTab Protege 5.0+ Plugin''
- ''SHACL4Protege Constraint Validator''
- Click ''Install''
- After a while, you will see message: "Updates will take effect when you next start Protege."
- Close Protégé and run it again (to load the new plugins).
=== 1. Introduction to SWRL [15 minutes] ===
* The OWL 2 language (known from previous labs on [[lab_onto2|ontologies]] and [[lab_reasoning|reasoning]]) is not able to express all relations. E.g., it cannot express the relation //child of married parents//, because there is no way in OWL 2 to express the relation between individuals with which an individual has relations: \\ {{.:swrl-ivan.png?direct&650|}} \\ To address this gap, and to allow this and other kind of inferences, the **Semantic Web Rule Language (SWRL)** was introduced: Person(?x) ^ hasParent(?x, ?y) ^ hasParent(?x, ?z) ^ hasSpouse(?y, ?z) -> ChildOfMarriedParents(?x)
Also, sometimes you can describe something using OWL, but it can be more intuitive when defined as a SWRL rule.
* As you can see, rules in SWRL has a simple structure:
* There is a left-hand side (called the antecedent) and a right-hand side (called the consequent). They are separated by an arrow (''->'').
* Each expression in a SWRL rule is separated by a ''^'' sign (logical //and//).
* All parameters (variables that are wildcards and get bound dynamically as the rule fires) are preceded by a ''?''.
* There are three types of expressions in SWRL:
* __Class expressions__. This is the name of a class followed by parentheses with a parameter inside, e.g., ''Person(?x)'' will bind ''?x'' to an instance of the class Person and will iterate over each instance of the Person class.
* __Property expressions__. This is the name of a property followed by parentheses and two parameters: the first is the subject, and the second is the object of a relation, e.g., ''hasParent(?x, ?y)'' will bind ''?y'' to each parent of ''?x'' (note that the ''?x'' will be bound in the previous expression).
* __Built-in functions__. SWRL has a number of built-in functions for doing mathematical tests, string tests, etc. All of them are prefaced by the ''swrlb:'' prefix, e.g., the math built-in ''swrlb:greaterThan(?age, 18)'' succeeds if the value of ''?age'' is greater than 18. See [[https://www.w3.org/Submission/SWRL/#8|8. Built-Ins]] for full list and documentation.
* The consequent of the rule fires if and only if every expression in the antecedent is satisfied.
* The Protégé supports SWRL
* They are parsed and interpreted by both HermiT and Pellet reasoners
* There are two dedicated interfaces to view and edit SWRL rules (there are slight differences between them, e.g., ''^'' are changed to commans in the Rule view):
* The SWRLTab: if it is not visible, select ''Window'' -> ''Tabs'' -> ''SWRLTab'' (there is no need to use the Drools engine in the bottom panel; we will use the HermiT/Pellet reasoners)
* The Rule view: select ''Window'' -> ''Views'' -> ''Ontology views'' -> ''Rules'' and click somewhere in the Protege, where this view should be placed.
- Download the {{http://swrl.stanford.edu/ontologies/examples/family.swrl.owl|family.swrl.owl}} ontology (mirror: {{https://krzysztof.kutt.pl/didactics/semweb/family.swrl.owl|family.swrl.owl}}) and load it into Protege.
- Take a look at the existing classes, properties, and instances (in the Entities tab).
- Open one of the dedicated interfaces for the SWRL (as described above).
- Take a look at the SWRL rules. Do you understand them?
- Run the rules:
- Under ''Reasoner'' menu select HermiT or Pellet reasoner
- ''Reasoner'' -> ''Start Reasoner'' (or ''Synchronize reasoner'' if it was started before)
- Take a look at inferred knowledge
=== 2. SWRL in use [25 minutes] ===
- Open your own ontology in the Protege Desktop.
- Prepare 3-5 rules for your ontology.
- Run them to check if they work correctly.
- //After adding rules, don't forget to save the new version of the ontology so you don't lose your work if something will go wrong!//
=== 3. Introduction to SHACL [15 minutes] ===
- Open the [[https://www.w3.org/TR/shacl/|Shapes Constraint Language (SHACL)]]. It may be useful as a reference.
- Protégé has dedicated plugin for SHACL. We will try it now (it should be already installed as the first task during this lab). \\ If you do not like the Protégé plugin, you can also work at:
* [[https://shacl-playground.zazuko.com/|Zazuko SHACL Playground]]
* [[https://rdfshape.weso.es/shaclValidate|SHACL validate data]] at [[https://rdfshape.weso.es/|RDFShape]]
- Download the [[https://raw.githubusercontent.com/fekaputra/shacl-plugin/master/src/main/resources/example3-data.owl|example OWL file]] ({{https://krzysztof.kutt.pl/didactics/semweb/example3-data.owl|mirror}}) and load it into Protege (simply ''File'' -> ''Open...'')
- Open dedicated tab by selecting: ''Window'' -> ''Tabs'' -> ''SHACL Editor''.
- By default, it will show sample SHACL graph with ''ex:PersonShape''. If it is not there, simply copy the following file to SHACL Editor: @prefix rdf: .
@prefix sh: .
@prefix xsd: .
@prefix rdfs: .
@prefix ex: .
@prefix owl: .
ex:PersonShape
a sh:NodeShape ;
sh:targetClass ex:Person ; # Applies to all persons
sh:property [ # _:b0
sh:path ex:ssn ; # constrains the values of ex:ssn
sh:maxCount 1 ;
] ;
sh:property [ # _:b1
sh:path ex:ssn ; # constrains the values of ex:ssn
sh:datatype xsd:string ;
sh:pattern "^\\d{3}-\\d{2}-\\d{4}$" ;
sh:severity sh:Warning ;
] ;
sh:closed true ;
sh:ignoredProperties ( rdf:type owl:topDataProperty owl:topObjectProperty ) ;
.
- Click ''Validate'' to see the Validation Report (in the table, at the bottom). \\ You can also start reasoner before running the validation -> then the shapes will be also validated against inferred knowledge. \\ There is also a possibility to filter the report by classes and instances (simply click on the class/instance in the left panel of the SHACL Editor tab).
- There are new type of constraints named ''sh:pattern'' and ''sh:closed'' (combined with ''sh:ignoredProperties''). Do you understand them? \\ For a list of available (core) constraints, see the table below and the [[https://www.w3.org/TR/shacl/#core-components|4. Core Constraint Components]] section of SHACL recommendation. \\ {{.:shacl-core.png?direct&650|}}
- There are 6 violations. Fix them! (fix the knowledge base and/or change the constraints)
=== 4. SHACL in use [25 minutes] ===
- Open your own ontology (with SWRL rules developed during this lab) in the Protégé.
- Prepare some SHACL shapes for your ontology (at least 2 PropertyShapes).
- Validate the knowledge base and observe the Validation Report to check if they work correctly.
- Save the SHACL shapes in a separate file (**they are not saved with the ontology**; you need to save them using ''Save'' button in the ''SHACL Editor'' tab; the file should have ''.ttl'' extension, as it is a regular Turtle file)
==== Learn more! ====
//These instructions are based on [[https://www.michaeldebellis.com/post/new-protege-pizza-tutorial|New Protégé Pizza Tutorial]], [[https://dior.ics.muni.cz/~makub/owl/|OWL 2 and SWRL Tutorial]]// and //[[http://www.validatingrdf.com/tutorial/iswc2020/|Shapes applications and tools tutorial]]//
SWRL:
* Standard:
* W3C Submission: [[https://www.w3.org/Submission/SWRL/|SWRL: A Semantic Web Rule Language Combining OWL and RuleML]]
* Tutorials:
* [[https://www.michaeldebellis.com/post/new-protege-pizza-tutorial|New Protégé Pizza Tutorial]] (2021)
* [[https://www.michaeldebellis.com/post/swrl_tutorial|SWRL Process Modeling Tutorial]] (2020)
* [[https://dior.ics.muni.cz/~makub/owl/|OWL 2 and SWRL Tutorial]] (2012)
* Excellent slides about SWRL: {{https://protege.stanford.edu/conference/2009/slides/SWRL2009ProtegeConference.pdf|SWRL2009ProtegeConference.pdf}}
* Tools:
* [[https://github.com/protegeproject/swrlapi|SWRLAPI]] -- API for Java; used as a base for Protege plugin
SHACL:
* Standard:
* [[https://www.w3.org/TR/shacl/|Shapes Constraint Language (SHACL)]]
* [[https://www.w3.org/TR/shacl-af/|SHACL Advanced Features]] (e.g., SHACL Functions and SHACL Rules)
* Readings:
* [[http://book.validatingrdf.com/|Validating RDF Data (2018)]] -- free HTML book on SHACL (and ShEx)
* [[https://spinrdf.org/shacl-and-owl.html|SHACL and OWL Compared]]
* Tutorials:
* [[http://www.validatingrdf.com/|Tutorials at ValidatingRDF]]
* {{https://www.ida.liu.se/~robke04/SHACLTutorial/Introduction%20to%20SHACL.pdf|Introduction to SHACL.pdf}}
* Tools:
* [[https://shacl-playground.zazuko.com/|SHACL Playground]]
* [[https://rdfshape.weso.es/shaclValidate|SHACL Validator]] at [[https://rdfshape.weso.es/|RDFShape]]
* [[https://github.com/fekaputra/shacl-plugin|SHACL4Protege]]
* List of open source tools is provided, e.g., at [[wp>SHACL|Wikipedia]]
* //ShEx (Shape Expressions)// -- although similar to SHACL and with a similar purpose (specifying the structure of RDF graphs), it is an independent language:
* SHACL defines constraints for specific classes (and validates them), while ShEx defines the schema (and tries to best match the definitions with instances)
* Standard: [[http://shex.io/shex-semantics/|Shape Expressions Language]]
* A chapter in Validating RDF Data: [[https://book.validatingrdf.com/bookHtml013.html|Chapter 7. Comparing ShEx and SHACL]]