SHACL: Difference between revisions
(Created page with "https://github.com/rudof-project/rudof") |
|||
| (6 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
https://github.com/rudof-project/rudof | = SHACL (Shapes Constraint Language) = | ||
'''SHACL''' (Shapes Constraint Language) is a W3C standard for validating and describing the structure of RDF (Resource Description Framework) data. SHACL enables users to define constraints (called "shapes") on RDF graphs, ensuring that data conforms to expected schemas and business rules. | |||
== Overview == | |||
SHACL provides a way to: | |||
* Describe the expected structure of RDF data using shapes and constraints. | |||
* Validate RDF graphs against these shapes. | |||
* Provide human- and machine-readable documentation of data models. | |||
SHACL is written in RDF itself, making it compatible with existing RDF tools and infrastructures. | |||
== UML Class diagram == | |||
Open in new tab to be able to click the links | |||
<uml format='svg'> | |||
hide circle | |||
package SHACL { | |||
class Shape { | |||
+[[https://www.w3.org/TR/shacl/#targetClass targetClass]] : rdfs:Class | |||
+[[https://www.w3.org/TR/shacl/#targetNode targetNode]] : any IRI or literal | |||
+[[https://www.w3.org/TR/shacl/#targetObjectsOf targetObjectsOf]] : rdf:Property | |||
+[[https://www.w3.org/TR/shacl/#targetSubjectsOf targetSubjectsOf]] : rdf:Property | |||
-- | |||
+[[https://www.w3.org/TR/shacl/#deactivated deactivated]] : xsd:boolean | |||
+[[https://www.w3.org/TR/shacl/#message message]] : xsd:string or rdf:langString | |||
+[[https://www.w3.org/TR/shacl/#severity severity]] : sh:Severity | |||
} | |||
class NodeShape { | |||
==Constraint parameters== | |||
+[[https://www.w3.org/TR/shacl/#ClosedConstraintComponent closed]] : xsd:boolean | |||
+[[https://www.w3.org/TR/shacl/#OrConstraintComponent or]] : rdf:List | |||
+[[https://www.w3.org/TR/shacl/#NotConstraintComponent not]] : sh:Shape | |||
+[[https://www.w3.org/TR/shacl/#PropertyConstraintComponent property]] : sh:PropertyShape | |||
} | |||
class PropertyShape { | |||
==Constraint parameters== | |||
+[[https://www.w3.org/TR/shacl/#MinCountConstraintComponent minCount]], [[https://www.w3.org/TR/shacl/#MaxCountConstraintComponent maxCount]] : xsd:integer | |||
+[[https://www.w3.org/TR/shacl/#ClassConstraintComponent class]] or [[https://www.w3.org/TR/shacl/#DatatypeConstraintComponent datatype]] : rdfs:Resource | |||
+[[https://www.w3.org/TR/shacl/#NodeConstraintComponent node]] : sh:NodeShape | |||
-- | |||
+[[https://www.w3.org/TR/shacl/#name name]] : xsd:string or rdf:langString | |||
+[[https://www.w3.org/TR/shacl/#description description]] : xsd:string or rdf:langString | |||
+[[https://www.w3.org/TR/shacl/#defaultValue defaultValue]] : any | |||
+[[https://www.w3.org/TR/shacl/#group group]] : sh:PropertyGroup | |||
-- | |||
+[[https://www.w3.org/TR/shacl/#property-shapes path]] : rdfs:Resource | |||
} | |||
Shape <|-- NodeShape | |||
Shape <|-- PropertyShape | |||
note top of Shape : [[https://www.w3.org/TR/shacl/#shapes sh:Shape]] | |||
note top of NodeShape : [[https://www.w3.org/TR/shacl/#node-shapes sh:NodeShape]] | |||
note top of PropertyShape : [[https://www.w3.org/TR/shacl/#property-shapes sh:PropertyShape]] | |||
} | |||
</uml> | |||
== Core Concepts == | |||
=== Shapes === | |||
A '''shape''' is a node in an RDF graph that specifies constraints about the data. Shapes can be used to describe: | |||
* The types of nodes (e.g., Person, Address). | |||
* The required properties for nodes. | |||
* Value types, ranges, and cardinalities. | |||
=== Targets === | |||
Shapes are associated with target nodes in the data graph using target declarations (e.g., by node type, specific node, or all nodes). | |||
=== Constraints === | |||
SHACL defines a variety of constraint components, including: | |||
* Property constraints (e.g., property must exist, value type). | |||
* Cardinality constraints (e.g., minCount, maxCount). | |||
* Pattern and datatype constraints. | |||
* Logical constraints (AND, OR, NOT). | |||
* Complex expressions via SPARQL-based constraints. | |||
== Example == | |||
=== Defining a Person Shape === | |||
<syntaxhighlight lang="turtle"> | |||
@prefix sh: <http://www.w3.org/ns/shacl#> . | |||
@prefix ex: <http://example.org/> . | |||
ex:PersonShape | |||
a sh:NodeShape ; | |||
sh:targetClass ex:Person ; | |||
sh:property [ | |||
sh:path ex:firstName ; | |||
sh:datatype xsd:string ; | |||
sh:minCount 1 ; | |||
] ; | |||
sh:property [ | |||
sh:path ex:age ; | |||
sh:datatype xsd:integer ; | |||
sh:minInclusive 0 ; | |||
] . | |||
</syntaxhighlight> | |||
This example defines a shape for <code>ex:Person</code> that requires a <code>firstName</code> (string, at least one) and an optional <code>age</code> (integer, must be ≥ 0). | |||
== Tools and Libraries == | |||
* [https://github.com/TopQuadrant/shacl SHACL API (Java)] | |||
* [https://www.w3.org/2017/SHACL/ SHACL at W3C] | |||
* [https://github.com/RDFLib/pySHACL pySHACL (Python)] | |||
* [https://jena.apache.org/documentation/shacl/ Apache Jena SHACL] | |||
== Alternatives == | |||
* [[ShEx]] (Shape Expressions) – another RDF validation language | |||
* [[OWL]] (Web Ontology Language) – can express some constraints, but less focused on data validation | |||
== References == | |||
* [https://www.w3.org/TR/shacl/ W3C Recommendation: SHACL] | |||
* [https://www.w3.org/TR/shacl-ucr/ SHACL Use Cases and Requirements] | |||
= Links = | |||
* https://github.com/rudof-project/rudof | |||
* https://www.w3.org/TR/shacl/#shacl-example | |||
{{LLMHint}} | |||
Latest revision as of 05:48, 13 July 2025
SHACL (Shapes Constraint Language)
SHACL (Shapes Constraint Language) is a W3C standard for validating and describing the structure of RDF (Resource Description Framework) data. SHACL enables users to define constraints (called "shapes") on RDF graphs, ensuring that data conforms to expected schemas and business rules.
Overview
SHACL provides a way to:
- Describe the expected structure of RDF data using shapes and constraints.
- Validate RDF graphs against these shapes.
- Provide human- and machine-readable documentation of data models.
SHACL is written in RDF itself, making it compatible with existing RDF tools and infrastructures.
UML Class diagram
Open in new tab to be able to click the links
Core Concepts
Shapes
A shape is a node in an RDF graph that specifies constraints about the data. Shapes can be used to describe:
- The types of nodes (e.g., Person, Address).
- The required properties for nodes.
- Value types, ranges, and cardinalities.
Targets
Shapes are associated with target nodes in the data graph using target declarations (e.g., by node type, specific node, or all nodes).
Constraints
SHACL defines a variety of constraint components, including:
- Property constraints (e.g., property must exist, value type).
- Cardinality constraints (e.g., minCount, maxCount).
- Pattern and datatype constraints.
- Logical constraints (AND, OR, NOT).
- Complex expressions via SPARQL-based constraints.
Example
Defining a Person Shape
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <http://example.org/> .
ex:PersonShape
a sh:NodeShape ;
sh:targetClass ex:Person ;
sh:property [
sh:path ex:firstName ;
sh:datatype xsd:string ;
sh:minCount 1 ;
] ;
sh:property [
sh:path ex:age ;
sh:datatype xsd:integer ;
sh:minInclusive 0 ;
] .
This example defines a shape for ex:Person that requires a firstName (string, at least one) and an optional age (integer, must be ≥ 0).
Tools and Libraries
Alternatives
- ShEx (Shape Expressions) – another RDF validation language
- OWL (Web Ontology Language) – can express some constraints, but less focused on data validation
References
Links
⚠️ LLM-generated content notice: Parts of this page may have been created or edited with the assistance of a large language model (LLM). The prompts that have been used might be on the page itself, the discussion page or in straight forward cases the prompt was just "Write a mediawiki page on X" with X being the page name. While the content has been reviewed it might still not be accurate or error-free.