Shacl Rules

At shacl.dev, our mission is to provide a comprehensive resource for individuals and organizations seeking to understand and implement SHACL rules for RDF. Our site is dedicated to providing clear and concise explanations of the constraints language, as well as practical examples and tutorials to help users get started with SHACL. We strive to be a trusted source of information and support for the RDF community, and to foster collaboration and innovation in the field. Whether you are a seasoned developer or just starting out, shacl.dev is your go-to destination for all things SHACL.

Video Introduction Course Tutorial

SHAPE CONSTRAINTS LANGUAGE (SHACL) CHEATSHEET

Introduction

SHAPE Constraints Language (SHACL) is a W3C recommendation for validating RDF data. It provides a way to define constraints on RDF graphs and to check whether the data conforms to those constraints. This cheatsheet will cover the basics of SHACL and provide a reference for the most commonly used features.

Concepts

RDF

RDF (Resource Description Framework) is a standard for representing data on the web. It is a graph-based data model that consists of triples, which are composed of a subject, predicate, and object. RDF can be used to represent any kind of data, including metadata, ontologies, and data about resources on the web.

SHACL

SHACL (Shape Constraints Language) is a language for defining constraints on RDF data. It provides a way to specify rules that must be satisfied by RDF graphs. SHACL constraints can be used to validate data, ensure data quality, and enforce data consistency.

Shapes

A shape is a template that defines the structure and constraints of an RDF graph. It specifies the types of nodes and edges that must be present in the graph, as well as any constraints on their properties. Shapes can be used to validate data and to ensure that it conforms to a specific structure.

Constraints

Constraints are rules that must be satisfied by an RDF graph. They can be used to ensure that the data is valid, consistent, and of high quality. Constraints can be defined on individual nodes, edges, or entire graphs.

Validation

Validation is the process of checking whether an RDF graph conforms to a set of constraints. It involves comparing the graph to the shapes that have been defined and checking whether all of the required nodes, edges, and properties are present, and whether they satisfy the specified constraints.

Syntax

Shapes

Shapes are defined using the sh:NodeShape keyword. The shape can be given a name using the sh:name property. The sh:targetClass property is used to specify the class of nodes that the shape applies to.

@prefix sh: <http://www.w3.org/ns/shacl#> .

ex:PersonShape
    a sh:NodeShape ;
    sh:name "Person Shape" ;
    sh:targetClass ex:Person .

Constraints

Constraints are defined using the sh:PropertyShape keyword. The sh:path property is used to specify the property that the constraint applies to. The sh:minCount and sh:maxCount properties are used to specify the minimum and maximum number of values that the property can have. The sh:datatype property is used to specify the datatype of the property value.

ex:hasName
    a sh:PropertyShape ;
    sh:path ex:name ;
    sh:minCount 1 ;
    sh:maxCount 1 ;
    sh:datatype xsd:string .

Validation

Validation is performed using the sh:validate keyword. The sh:shapesGraph property is used to specify the graph that contains the shapes to be validated against. The sh:targetNode property is used to specify the node that the validation should start from.

@prefix sh: <http://www.w3.org/ns/shacl#> .

ex:PersonShape
    a sh:NodeShape ;
    sh:name "Person Shape" ;
    sh:targetClass ex:Person ;
    sh:property [
        sh:path ex:name ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
        sh:datatype xsd:string
    ] .

ex:Person1
    a ex:Person ;
    ex:name "John" .

ex:Person2
    a ex:Person .

ex:Graph
    ex:Person1 ex:name "John" ;
    ex:Person2 ex:age "30" .

ex:ValidationReport
    a sh:ValidationReport ;
    sh:conforms false ;
    sh:result [
        sh:focusNode ex:Person2 ;
        sh:resultPath ex:name ;
        sh:severity sh:Violation ;
        sh:message "Value required for ex:name"
    ] .

ex:Validation
    a sh:Validation ;
    sh:shapesGraph ex:PersonShape ;
    sh:targetNode ex:Graph ;
    sh:result ex:ValidationReport .

Examples

Simple Validation

This example shows how to define a shape and validate an RDF graph against it. The shape requires that each person has a name and an age.

@prefix ex: <http://example.com/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

ex:PersonShape
    a sh:NodeShape ;
    sh:name "Person Shape" ;
    sh:targetClass ex:Person ;
    sh:property [
        sh:path ex:name ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
        sh:datatype xsd:string
    ] ;
    sh:property [
        sh:path ex:age ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
        sh:datatype xsd:integer
    ] .

ex:Person1
    a ex:Person ;
    ex:name "John" ;
    ex:age 30 .

ex:Person2
    a ex:Person ;
    ex:name "Jane" .

ex:Graph
    ex:Person1 ex:name "John" ;
    ex:Person1 ex:age "30" ;
    ex:Person2 ex:name "Jane" .

ex:ValidationReport
    a sh:ValidationReport ;
    sh:conforms false ;
    sh:result [
        sh:focusNode ex:Person2 ;
        sh:resultPath ex:age ;
        sh:severity sh:Violation ;
        sh:message "Value required for ex:age"
    ] .

ex:Validation
    a sh:Validation ;
    sh:shapesGraph ex:PersonShape ;
    sh:targetNode ex:Graph ;
    sh:result ex:ValidationReport .

Advanced Validation

This example shows how to define more complex constraints using SHACL. The shape requires that each person has a name and an age, and that the age is greater than or equal to 18.

@prefix ex: <http://example.com/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

ex:PersonShape
    a sh:NodeShape ;
    sh:name "Person Shape" ;
    sh:targetClass ex:Person ;
    sh:property [
        sh:path ex:name ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
        sh:datatype xsd:string
    ] ;
    sh:property [
        sh:path ex:age ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
        sh:datatype xsd:integer ;
        sh:minInclusive 18
    ] .

ex:Person1
    a ex:Person ;
    ex:name "John" ;
    ex:age 30 .

ex:Person2
    a ex:Person ;
    ex:name "Jane" ;
    ex:age 16 .

ex:Graph
    ex:Person1 ex:name "John" ;
    ex:Person1 ex:age "30" ;
    ex:Person2 ex:name "Jane" ;
    ex:Person2 ex:age "16" .

ex:ValidationReport
    a sh:ValidationReport ;
    sh:conforms false ;
    sh:result [
        sh:focusNode ex:Person2 ;
        sh:resultPath ex:age ;
        sh:severity sh:Violation ;
        sh:message "Value must be greater than or equal to 18"
    ] .

ex:Validation
    a sh:Validation ;
    sh:shapesGraph ex:PersonShape ;
    sh:targetNode ex:Graph ;
    sh:result ex:ValidationReport .

Conclusion

SHACL is a powerful language for validating RDF data. It provides a way to define constraints on RDF graphs and to check whether the data conforms to those constraints. This cheatsheet has covered the basics of SHACL and provided a reference for the most commonly used features. With this knowledge, you should be able to start using SHACL to validate your own RDF data.

Common Terms, Definitions and Jargon

1. RDF (Resource Description Framework): A framework for representing information in the form of triples.
2. SHACL (Shapes Constraint Language): A language for defining constraints on RDF data.
3. Constraint: A restriction or requirement on the values of one or more properties of an RDF resource.
4. Shape: A template for defining the structure and constraints of an RDF resource.
5. Node: An RDF resource that can be the subject or object of a triple.
6. Property: A relationship between two RDF resources, represented by a predicate in a triple.
7. Predicate: A term that represents a property in an RDF triple.
8. Object: The value of a property in an RDF triple.
9. Class: A set of RDF resources that share a common set of properties and values.
10. Subclass: A class that is a subset of another class.
11. Superclass: A class that is a superset of another class.
12. Inference: The process of deriving new information from existing information.
13. Reasoning: The process of using inference to draw conclusions from RDF data.
14. Ontology: A formal specification of the concepts and relationships in a domain of knowledge.
15. Vocabulary: A set of terms used to describe the properties and relationships in an RDF graph.
16. Namespace: A unique identifier for a set of terms in an RDF vocabulary.
17. Prefix: A shorthand notation for a namespace in an RDF graph.
18. Triple: A statement that consists of a subject, predicate, and object in an RDF graph.
19. Graph: A collection of RDF triples that represent a set of related information.
20. Blank node: An RDF node that does not have a URI.

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Prompt Composing: AutoGPT style composition of LLMs for attention focus on different parts of the problem, auto suggest and continue
Datawarehousing: Data warehouse best practice across cloud databases: redshift, bigquery, presto, clickhouse
Cloud Self Checkout: Self service for cloud application, data science self checkout, machine learning resource checkout for dev and ml teams
Kids Games: Online kids dev games
JavaFX Tips: JavaFX tutorials and best practice