Table of Contents

Rules and constraints

Prepare yourself for the lab

Lab instructions

At the end of the lab, each group should email their second project to the teacher. It consist of:

  1. the final *.ttl file with the ontology (started on Ontology 101, refined during the Advanced ontology engineering and extended with SWRL rules during today's lab),
  2. the second *.ttl file with SHACL shapes (developed during the second part of today's lab).

0. Prepare the Protégé Desktop [5 minutes]

  1. Run the Protégé Desktop.
  2. Open FileCheck for plugins…
  3. Select tickbox on the left side of two plugins (if they are not listed, they are already installed in the newest version):
    1. SWRLTab Protege 5.0+ Plugin
    2. SHACL4Protege Constraint Validator
  4. Click Install
  5. After a while, you will see message: “Updates will take effect when you next start Protege.”
  6. Close Protégé and run it again (to load the new plugins).

1. Introduction to SWRL [15 minutes]

  1. Download the family.swrl.owl ontology (mirror: family.swrl.owl) and load it into Protege.
  2. Take a look at the existing classes, properties, and instances (in the Entities tab).
  3. Open one of the dedicated interfaces for the SWRL (as described above).
  4. Take a look at the SWRL rules. Do you understand them?
  5. Run the rules:
    1. Under Reasoner menu select HermiT or Pellet reasoner
    2. ReasonerStart Reasoner (or Synchronize reasoner if it was started before)
  6. Take a look at inferred knowledge

2. SWRL in use [25 minutes]

  1. Open your own ontology in the Protege Desktop.
  2. Prepare 3-5 rules for your ontology.
  3. Run them to check if they work correctly.
  4. 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]

  1. Open the Shapes Constraint Language (SHACL). It may be useful as a reference.
  2. 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:
  3. Download the example OWL file (mirror) and load it into Protege (simply FileOpen…)
  4. Open dedicated tab by selecting: WindowTabsSHACL Editor.
  5. 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:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix sh:    <http://www.w3.org/ns/shacl#> .
    @prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
    @prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix ex:    <http://www.example.org/#> .
    @prefix owl:   <http://www.w3.org/2002/07/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 ) ;
        .
  6. 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).
  7. 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 4. Core Constraint Components section of SHACL recommendation.
  8. There are 6 violations. Fix them! (fix the knowledge base and/or change the constraints)

4. SHACL in use [25 minutes]

  1. Open your own ontology (with SWRL rules developed during this lab) in the Protégé.
  2. Prepare some SHACL shapes for your ontology (at least 2 PropertyShapes).
  3. Validate the knowledge base and observe the Validation Report to check if they work correctly.
  4. 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 New Protégé Pizza Tutorial, OWL 2 and SWRL Tutorial and Shapes applications and tools tutorial

SWRL:

SHACL: