Using SPARQL Constraints for Custom Resource Validation¤
Introduction¤
SHACL (Shapes Constraint Language) is a language for validating RDF data based on predefined shapes or constraints. 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.
Key advantages:
-
Complex logic: You can implement any logic supported by SPARQL, such as aggregation, advanced pattern matching, or value ranges.
-
Reusable queries: SPARQL constraints allow the reuse of sophisticated query logic across different shapes and data types.
Setup¤
SPARQL Constraints are resources which need to be managed in a Shape Catalog.
This catalog graph needs to be imported (virtually, with owl:imports
) into the main CMEM Shapes Catalog.
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.
Step-by-Step Example¤
In our running example, we create a Node Shape for foaf:Person
resources.
Then we add a SPARQL Constrain shape, which checks if the label of these resources contains the term eccenca
.
Install Vocabularies¤
Go to the Vocabulary catalog and install the RDF Schema and FOAF Vocabulary. Your vocabulary catalog should look like this after installation
Create Shape Catalog¤
Go to Knowledge graphs and create a New Shape Catalog. Name the graph e.g. My Shapes. 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.
Create Shapes¤
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. Fill in a label and a SPARQL SELECT Query.
For our example, we use the following values:
- Label:
The term eccenca is not allowed in a Person label
- SPARQL Select:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT $this (rdfs:label as ?path) (?label as ?value)
WHERE {
$this a foaf:Person ;
rdfs:label ?label .
FILTER( REGEX(STR(?label), "eccenca" ))
}
Note that the projection variables of SPARQL Constraint queries need to be named according to the SHACL Specificaton ($this
, ?path
, ?value
).
After you clicked on Create you should see your new resource in a list.
As a next step 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.
Enter the following values:
- Name:
Person Shape
- Target class: Select
Person
from the FOAF vocabulary - SPARQL Constraints: Select your new constraint from the list (your have to use the Add data menu to extend the form with this non-default input field)
- Property Shapes: Create a new property shape with
- Name:
Name
- Show always:
true
- Path:
rdfs:label
- Node Kind:
Literal
- Name:
Before you create your new Node Shape (and new Property Shape) your screen should look like this:
Validate Data¤
Now we can validate the shape by creating a Person with a label which contains the term eccenca
.
- Go to Knowledge graphs and create a New Knowledge Graph.
- Name:
My Data
- Imports:
foaf: Friend of a Friend vocabulary
- Name:
- Select Person in the Navigation component, then use the Create a new “Person” button in the top right corner to create a new resource.
- Name:
The term eccenca is not allowed
- Name:
To validate a single resource, click the validation button. You will see a screen like this:
When you open the validation report, you will see a error message such as: The value does not conform to SPARQL constraint.
In addition to manual validation of a single resource, you have the following other options:
- use the Validation Knowledge Graph task in a workflow
- use the cmemc command graph validation execute