Skip to content

Commit e997b4d

Browse files
committed
add SPARQL Constraints Tutorial
1 parent 1654d17 commit e997b4d

7 files changed

Lines changed: 109 additions & 0 deletions

File tree

docs/explore-and-author/graph-exploration/building-a-customized-user-interface/.pages

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ nav:
44
- Node Shapes: node-shapes
55
- Property Shapes: property-shapes
66
- Datatypes: datatype-reference
7+
- SPARQL Constraints: sparql-constraints
78

385 KB
Loading
285 KB
Loading
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
icon: eccenca/application-queries
3+
tags:
4+
- SPARQL
5+
- Validation
6+
- AdvancedTutorial
7+
---
8+
# Using SPARQL Constraints for Custom Resource Validation
9+
10+
## Introduction
11+
12+
SHACL (Shapes Constraint Language) is a language for validating RDF data based on predefined shapes or constraints.
13+
SHACL SPARQL Constraints are a set of rules defined using SPARQL queries within SHACL shapes to express more complex validation logic that cannot be easily represented by other SHACL constructs.
14+
15+
Key advantages:
16+
17+
- **Complex logic**: You can implement any logic supported by SPARQL, such as aggregation, advanced pattern matching, or value ranges.
18+
19+
- **Reusable queries**: SPARQL constraints allow the reuse of sophisticated query logic across different shapes and data types.
20+
21+
## Setup
22+
23+
SPARQL Constraints are resources which need to be managed in a Shape Catalog.
24+
This catalog graph needs to be imported (virtually, with `owl:imports`) into the main CMEM Shapes Catalog.
25+
Similar to property shapes, SPARQL Constraints needed to be linked from a Node Shape in order to select the set of shapes which are used for a given selection of resources.
26+
27+
## Example
28+
29+
In our running example, we create a Node Shape for `foaf:Person` resources.
30+
Then we add a SPARQL Constrain shape, which ...
31+
32+
### Install needed Vocabularies
33+
34+
Go to the **:eccenca-application-vocabularies: [Vocabulary catalog](../../../vocabulary-catalog)** and install the RDF Schema and FOAF Vocabulary.
35+
Your vocabulary catalog should look like this after installation
36+
37+
![Vocabulary Catalog with Installed Vocabularies](vocabulary-catalog.png){ class="bordered" }
38+
39+
### Create a Shape Catalog
40+
41+
Go to **:eccenca-application-explore: [Knowledge graphs](../../../graph-exploration)** and create a **New Shape Catalog**.
42+
Name the graph e.g. *My Shapes*.
43+
After this step, the browser shows you a nearly empty graph which shows SHACL classes such as **SHACL Node Shape** and **SHACL SPARQL Constraint** in the navigation.
44+
45+
![Empty Shapes Graph](empty-shapes-graph.png){ class="bordered" }
46+
47+
### Create a SPARQL Constraint
48+
49+
Select **SHACL SPARQL Constraint** in the Navigation component, then use the **Create a new "SHACL SPARQL Constraint"** button in the top right corner to create a new resource.
50+
Fill in a label and a SPARQL SELECT Query.
51+
52+
For our example, we use the following values:
53+
54+
- Label: `The term eccenca is not allowed in a Person label`
55+
- SPARQL Select:
56+
57+
``` sparql
58+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
59+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
60+
61+
SELECT $this (rdfs:label as ?path) (?label as ?value)
62+
WHERE {
63+
$this a foaf:Person ;
64+
rdfs:label ?label .
65+
FILTER( REGEX(STR(?label), "eccenca" ))
66+
}
67+
```
68+
69+
Note that the projection variables of SPARQL Constraint queries need to be named according to the [SHACL Specificaton](https://www.w3.org/TR/shacl/#sparql-constraints-example) (`$this`, `?path`, `?value`).
70+
After you clicked on **Create** you should see your new resource in a list.
71+
72+
![New SPARQL Constraint](new-sparql-constraint.png){ class="bordered" }
73+
74+
### Create a Node Shape
75+
76+
Select **SHACL Node Shape** in the Navigation component, then use the **Create a new "SHACL Node Shape"** button in the top right corner to create a new resource.
77+
78+
Enter the following values:
79+
80+
- Name: `Person Shape`
81+
- Target class: Select `Person` from the FOAF vocabulary
82+
- SPARQL Constraints: Select your new constraint from the list (your have to use the **:material-plus-circle-outline: Add data** menu to extend the form with this non-default input field)
83+
- Property Shapes: Create a new property shape with
84+
- Name: `Name`
85+
- Show always: `true`
86+
- Path: `rdfs:label`
87+
- Node Kind: `Literal`
88+
89+
Before you create your new Node Shape (and new Property Shape) your screen should look like this:
90+
91+
![Create New Node Shape](create-node-shape.png){ class="bordered" }
92+
93+
### Validate the Shape
94+
95+
Now we can validate the shape by creating a Person with a label which contains the term `eccenca`.
96+
97+
- Go to **:eccenca-application-explore: [Knowledge graphs](../../../graph-exploration)** and create a **New Knowledge Graph**.
98+
- Name: `My Person Data`
99+
- Imports: `foaf: Friend of a Friend vocabulary`
100+
- Select **Person** in the Navigation component, then use the **Create a new "Person"** button in the top right corner to create a new resource.
101+
- Name: `The term eccenca is not allowed`
102+
103+
The validate the resource with the validation button. You will see a screen like this:
104+
105+
![Invalid Resource](invalid-resource.png){ class="bordered" }
106+
107+
When you open the validation report, you will see a error message such as: **The value does not conform to SPARQL constraint**.
108+
255 KB
Loading
271 KB
Loading
543 KB
Loading

0 commit comments

Comments
 (0)