Difference between revisions of "SHACL"

From BITPlan cr Wiki
Jump to navigation Jump to search
 
Line 13: Line 13:
 
Open in new tab to be able to click the links
 
Open in new tab to be able to click the links
 
<uml format='svg'>
 
<uml format='svg'>
 +
hide circle
 +
package SHACL {
 +
 
class Shape {
 
class Shape {
   +targetClass : rdfs:Class
+
   +[[https://www.w3.org/TR/shacl/#targetClass targetClass]] : rdfs:Class
   +targetNode : any IRI or literal
+
   +[[https://www.w3.org/TR/shacl/#targetNode targetNode]] : any IRI or literal
   +targetObjectsOf : rdf:Property
+
   +[[https://www.w3.org/TR/shacl/#targetObjectsOf targetObjectsOf]] : rdf:Property
   +targetSubjectsOf : rdf:Property
+
   +[[https://www.w3.org/TR/shacl/#targetSubjectsOf targetSubjectsOf]] : rdf:Property
 
   --
 
   --
   +deactivated : xsd:boolean
+
   +[[https://www.w3.org/TR/shacl/#deactivated deactivated]] : xsd:boolean
   +message : xsd:string or rdf:langString
+
   +[[https://www.w3.org/TR/shacl/#message message]] : xsd:string or rdf:langString
   +severity : sh:Severity
+
   +[[https://www.w3.org/TR/shacl/#severity severity]] : sh:Severity
 
}
 
}
  
 
class NodeShape {
 
class NodeShape {
 
   ==Constraint parameters==
 
   ==Constraint parameters==
   +closed : xsd:boolean
+
   +[[https://www.w3.org/TR/shacl/#ClosedConstraintComponent closed]] : xsd:boolean
   +or : rdf:List
+
   +[[https://www.w3.org/TR/shacl/#OrConstraintComponent or]] : rdf:List
   +not : sh:Shape
+
   +[[https://www.w3.org/TR/shacl/#NotConstraintComponent not]] : sh:Shape
   +property : sh:PropertyShape
+
   +[[https://www.w3.org/TR/shacl/#PropertyConstraintComponent property]] : sh:PropertyShape
 
}
 
}
  
 
class PropertyShape {
 
class PropertyShape {
 
   ==Constraint parameters==
 
   ==Constraint parameters==
   +minCount, maxCount : xsd:integer
+
   +[[https://www.w3.org/TR/shacl/#MinCountConstraintComponent minCount]], [[https://www.w3.org/TR/shacl/#MaxCountConstraintComponent maxCount]] : xsd:integer
   +class or datatype : rdfs:Resource
+
   +[[https://www.w3.org/TR/shacl/#ClassConstraintComponent class]] or [[https://www.w3.org/TR/shacl/#DatatypeConstraintComponent datatype]] : rdfs:Resource
   +node : sh:NodeShape
+
   +[[https://www.w3.org/TR/shacl/#NodeConstraintComponent node]] : sh:NodeShape
 
   --
 
   --
   +name : xsd:string or rdf:langString
+
   +[[https://www.w3.org/TR/shacl/#name name]] : xsd:string or rdf:langString
   +description : xsd:string or rdf:langString
+
   +[[https://www.w3.org/TR/shacl/#description description]] : xsd:string or rdf:langString
   +defaultValue : any
+
   +[[https://www.w3.org/TR/shacl/#defaultValue defaultValue]] : any
   +group : sh:PropertyGroup
+
   +[[https://www.w3.org/TR/shacl/#group group]] : sh:PropertyGroup
 
   --
 
   --
   +path : rdfs:Resource
+
   +[[https://www.w3.org/TR/shacl/#property-shapes path]] : rdfs:Resource
 
}
 
}
  
Line 49: Line 52:
 
Shape <|-- PropertyShape
 
Shape <|-- PropertyShape
  
note top of Shape : [[http://www.w3.org/ns/shacl#Shape]]
+
note top of Shape : [[https://www.w3.org/TR/shacl/#shapes sh:Shape]]
note top of NodeShape : [[http://www.w3.org/ns/shacl#NodeShape]]
+
note top of NodeShape : [[https://www.w3.org/TR/shacl/#node-shapes sh:NodeShape]]
note top of PropertyShape : [[http://www.w3.org/ns/shacl#PropertyShape]]
+
note top of PropertyShape : [[https://www.w3.org/TR/shacl/#property-shapes sh:PropertyShape]]
 +
}
  
 
</uml>
 
</uml>

Latest revision as of 06: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.