courses:semint:lab_rules

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

courses:semint:lab_rules [2022/09/08 22:24] – created kktcourses:semint:lab_rules [2022/11/10 02:09] (current) kkt
Line 55: Line 55:
   - Take a look at inferred knowledge   - Take a look at inferred knowledge
  
-=== 2. SWRL in use [20 minutes] ===+=== 2. SWRL in use [25 minutes] ===
  
   - Open your own ontology in the Protege Desktop.   - Open your own ontology in the Protege Desktop.
Line 62: Line 62:
   - //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!//   - //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 [20 minutes] === +=== 3. Introduction to SHACL [15 minutes] ===
- +
-  * SHACL stands for **Shapes Constraint Language** (no, these are not geometric shapes ;-) ). It's all about **constraints** (a shape is a collection of constraints that shapes the data). +
-    * Unlike ontologies (which model a selected part of real-world), SHACL is used to describe (low-level) requirements to specific RDF triples. +
-    * The starting point is our knowledge graph. Alongside it, we create a second graph (with SHACL triples) that describes the constraints. +
-  * Whatever you can do with SHACL you can also do with OWL or SWRL. __So why do we need SHACL?__ \\ (based on [[https://spinrdf.org/shacl-and-owl.html|SHACL and OWL Compared]]): +
-    * //OWL has been designed to support inferencing, but it is only practically applicable for certain kinds of inference. As a result, there is a need for SWRL rules (above) or a set of [[lab_sparql|SPARQL CONSTRUCT]] queries (e.g., if you want to __change__ the wrong triples, you need to use SPARQL; OWL and SWRL cannot do this).// \\ **SHACL is used to actually verify constraints (instead of performing inference). SHACL can also be used to define rules.** +
-    * //Confusion about the meaning of restrictions -- in particular that OWL does not constrain anything but rather describes inferences.// \\ **SHACL generates a list of exceeded constraints instead of stating that there are contradictions and the entire graph is false.** +
-    * //Reasoning under Open World Assumption is tricky. Especially if you want to determine if all records meet the constraints.// \\ **SHACL verifies constraints under Closed World Assumption.**+
  
   - Open the [[https://www.w3.org/TR/shacl/|Shapes Constraint Language (SHACL)]]. It may be useful as a reference.   - Open the [[https://www.w3.org/TR/shacl/|Shapes Constraint Language (SHACL)]]. It may be useful as a reference.
-  - Open [[https://shacl-playground.zazuko.com/|Zazuko SHACL Playground]]. +  - 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
-    - Click on the Shapes Graph icon (top left). Select Format: ''text/turtle'' (at the top of the left panel) +    * [[https://shacl-playground.zazuko.com/|Zazuko SHACL Playground]] 
-    - Click on the Data Graph icon (next to Shapes Graph icon). Select Format: ''text/turtle''+    * [[https://rdfshape.weso.es/shaclValidate|SHACL validate data]] at [[https://rdfshape.weso.es/|RDFShape]]
-    - Now, you can see graphs in Turtle syntax in both windows: Shapes Graph and Data Graph. +
-  - Let's start with a simple example. +
-    - Copy the following Shapes Graph to SHACL Playground: <code turtle>@prefix :       <http://example.org/>+
-@prefix sh:     <http://www.w3.org/ns/shacl#>+
-@prefix xsd:    <http://www.w3.org/2001/XMLSchema#>+
-@prefix schema: <http://schema.org/>+
- +
-:UserShape a sh:NodeShape ; +
-   sh:targetNode :alice, :bob, :carol ; +
-   sh:property [ +
-      sh:path schema:name ;  +
-      sh:minCount 1;  +
-      sh:maxCount 1; +
-      sh:datatype xsd:string +
-   ] ; +
-   sh:property [ +
-      sh:path schema:email ;  +
-      sh:minCount 1;  +
-      sh:maxCount 1;  +
-      sh:nodeKind sh:IRI +
-   ] . +
-</code> and the following Data Graph: <code turtle>@prefix :       <http://example.org/>+
-@prefix sh:     <http://www.w3.org/ns/shacl#>+
-@prefix xsd:    <http://www.w3.org/2001/XMLSchema#>+
-@prefix schema: <http://schema.org/>+
- +
-:alice schema:name "Alice Cooper"+
-       schema:email <mailto:alice@mail.org>+
-     +
-:bob   schema:firstName "Bob"+
-       schema:email <mailto:bob@mail.org>+
-     +
-:carol schema:name "Carol"+
-       schema:email "carol@mail.org" .</code> +
-    - Data Graph should be clear for you ;-) \\ Shapes Graph has a '':UserShape'' instance of a ''sh:NodeShape'' class. It defines the constraints to check against the Data Graph: +
-      * First, we provide information about nodes in Data Graph that should follow these constraints: ''sh:targetNode'' +
-      * Then, we provide information about constraints on two outgoing relations (i.e., properties in which targetNodes are the subject). ''sh:path'' defines the actual relation (it can be more sophisticated -> it can be a property chain) +
-      * For each property, we define the cardinality (min and max count; so, one user must have exactly one name and one email) and the expected object type (to be more concrete -> all restrictions on relations, i.e., everything in blank nodes in this example, are __PropertyShapes__) +
-    - The Validation Report is presented below. It is formatted in nice HTML, but the SHACL standard defines the exact report structure in RDF. If you want to see the RAW reportselect the Validation Report icon (third icon in the top left corner) and select Display errors as ''Raw RDF'' +
-  - There are some errors in the Data Graph. Fix them to check if you understand the defined shapes correctly! +
-  - SHACL is for humans, so make it more human-friendly! Play with all three relations: +
-    - You can define your own message in a Shape, using ''sh:message'' relation. +
-    - You can also define the severity of each constraint, using ''sh:severity'' relation -> possible values are ''sh:Info'', ''sh:Warning'', ''sh:Violation'' (by default, all are Violations; note that the UI shows only Violations! If you want to see other severity levels, take a look at raw report) +
-    - You can also deactivate the specific constraint by setting ''sh:deactivated'' to ''true'' (it is not needed to comment / remove them; you can simply add one statement, and they will not be checked) +
-  - Constraints applied to specific nodes listed by URIs (''sh:targetNode'' as above) are not so useful. But, there is a possibility to constraint **all** instances of specific class (''sh:targetClass'') or all instances connected with specific relation (''sh:targetSubjectsOf'' and ''sh:targetObjectsOf''). +
-    - Load the new Data Graph (with specification of instances of '':User'' class): <code turtle>@prefix :       <http://example.org/>+
-@prefix sh:     <http://www.w3.org/ns/shacl#> . +
-@prefix xsd:    <http://www.w3.org/2001/XMLSchema#>+
-@prefix schema: <http://schema.org/>+
-  +
-:alice a           :User ; +
-       schema:name "Alice Cooper"+
-       schema:email <mailto:alice@mail.org>+
-  +
-:bob             :User ; +
-       schema:firstName "Bob"+
-       schema:email <mailto:bob@mail.org>+
-  +
-:carol a           :User ; +
-       schema:name "Carol"+
-       schema:email "carol@mail.org" .</code> +
-    - Change the definition of the '':UserShape'' so that it applies to: +
-      - All instances of '':User'' class +
-      - All subjects of ''schema:email'' relation +
-  - SHACL provides logical operators to combine constraints. +
-    - Before you dive into this topic, it may be useful to separate PropertyShapes. As a result, it will be easier to control the constraints, as below: <code turtle>@prefix :       <http://example.org/> . +
-@prefix sh:     <http://www.w3.org/ns/shacl#>+
-@prefix xsd:    <http://www.w3.org/2001/XMLSchema#>+
-@prefix schema: <http://schema.org/>+
-  +
-:UserShape a sh:NodeShape ; +
-   sh:targetClass :User ; +
-   sh:and ( :CheckName :CheckEmail ) . +
- +
-:CheckName +
-   sh:path schema:name ;  +
-   sh:minCount 1;  +
-   sh:maxCount 1; +
-   sh:datatype xsd:string . +
- +
-:CheckEmail +
-   sh:path schema:email ;  +
-   sh:minCount 1;  +
-   sh:maxCount 1;  +
-   sh:nodeKind sh:IRI .</code> +
-    - Now, let's back to the Data Graph provided at the beginning, where some users have ''schema:firstName'' while the others have ''schema:name''. Provide the shapes that allow both of them (but not in the same time). [[https://www.w3.org/TR/shacl/#core-components-logical|4.6 Logical Constraint Components]] may be useful (take a look at ''sh:xone''). +
- +
- +
-=== 4. SHACL and Protégé [10 minutes] === +
- +
-  - Protégé has dedicated plugin for SHACL. We will try it now (it should be already installed; as the first task during this lab).+
   - 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...'')   - 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''.   - Open dedicated tab by selecting: ''Window'' -> ''Tabs'' -> ''SHACL Editor''.
Line 196: Line 97:
   - There are 6 violations. Fix them! (fix the knowledge base and/or change the constraints)   - There are 6 violations. Fix them! (fix the knowledge base and/or change the constraints)
  
-=== 5. SHACL in use [20 minutes] ===+=== 4. SHACL in use [25 minutes] ===
  
   - Open your own ontology (with SWRL rules developed during this lab) in the Protégé.   - Open your own ontology (with SWRL rules developed during this lab) in the Protégé.
  • courses/semint/lab_rules.1662675866.txt.gz
  • Last modified: 3 years ago
  • by kkt