๐Ÿ›ก๏ธ Shape Constraints & Expressions

Lecture 7 Interactive Learning Companion ยท SHACL ยท Shape Graphs ยท Validation ยท ShEx

๐Ÿ” Chapter 7 Topic Explorer

Explore each core topic from Chapter 7. Select a category to see syntax, semantics, and annotated examples.

// Select a topic above to explore Chapter 7 features
๐Ÿ“š Chapter 7 in context: SPARQL (Ch. 6) lets you query RDF graphs. SHACL and ShEx (Ch. 7) let you validate them โ€” enforcing structural constraints that the open-world assumption of OWL/RDFS cannot provide.

๐Ÿงช Shape Authoring Sandbox

Explore how SHACL shapes are written. Load an example, edit it, and see a live parse of the shape graph structure.

โš ๏ธ Key Rule: A SHACL shape graph is itself valid RDF. Shapes can be stored in a named graph, queried with SPARQL, and versioned alongside your ontology. Validation is always graph vs shapes graph.

โœ… Validation Simulator

Step through how a SHACL processor evaluates a data graph against a shapes graph. See which nodes conform and which violate.

Select a scenario above to begin the validation walkthrough

๐Ÿ’ก Key Insight: Validation is closed-world โ€” if an expected property is absent, it is a violation. This is the opposite of OWL's open-world assumption. A node conforms only when ALL constraints in all applicable shapes evaluate to TRUE.

๐Ÿงฑ Constraint Component Lab

Explore the full set of SHACL constraint components and the SHACL vs ShEx comparison. Click a tip to expand it.

Core SHACL Constraint Components

ConstraintCategoryWhat it checksExample
sh:minCount / sh:maxCountCardinalityMin/max number of values for the pathsh:minCount 1
sh:datatypeValue TypeAll values must be a given XSD datatypesh:datatype xsd:string
sh:classValue TypeAll values must be instances of a given classsh:class ex:Person
sh:nodeKindValue TypeIRI, BlankNode, or Literalsh:nodeKind sh:IRI
sh:minExclusive / sh:maxInclusiveRangeNumeric value range boundssh:minInclusive 0
sh:minLength / sh:maxLengthStringLiteral string length boundssh:minLength 1
sh:patternStringValue must match a regular expressionsh:pattern "^\\d{4}-"
sh:inEnumerationValue must be in a given listsh:in (ex:A ex:B)
sh:hasValueEnumerationAt least one value must equal a given nodesh:hasValue ex:X
sh:nodeShapeAll values must conform to another shapesh:node ex:PersonShape
sh:and / sh:or / sh:notLogicalBoolean combinations of shapessh:or ( [...] [...] )
sh:sparqlSPARQLSPARQL SELECT โ€” any row = violationsh:sparql [ sh:select ... ]

โš–๏ธ SHACL vs ShEx โ€” Click to expand

๐Ÿ“ Syntax: SHACL uses Turtle/RDF; ShEx has its own compact syntax

๐ŸŽฏ Targets: sh:targetClass vs Shape Map

๐Ÿ”„ Recursion: ShEx supports it natively; SHACL Core does not

โšก Extensibility: SHACL-SPARQL vs Semantic Actions

๐Ÿฅ Real-world adoption: enterprise vs biomedical

๐Ÿ† W3C Status: SHACL is a full Recommendation; ShEx is a Submission

๐Ÿ”— Property Path Types in SHACL

SHACL sh:path supports rich SPARQL-style property paths:

Predicate Path

sh:path ex:name โ€” validates values of a single predicate at the focus node.

Inverse Path

sh:path [ sh:inversePath ex:knows ] โ€” validates nodes that point TO the focus node.

Sequence Path

sh:path ( ex:worksFor ex:name ) โ€” chain two predicates; validates the employer's name.

Alternative / OneOrMore

sh:alternativePath, sh:oneOrMorePath, sh:zeroOrMorePath โ€” mirrors SPARQL property paths.

๐ŸŽฎ SHACL Challenge

Test your knowledge of SHACL, ShEx, and shape-based validation. 10 points per correct answer!

Score: 0 / 100 Question - of 10

Press "Start" to begin the SHACL Challenge!

๐Ÿ“ Chapter 7 Quiz

12 questions covering SHACL, Shape Graphs, Validation, and ShEx. You can review answers before finishing.

1. Which W3C document defines SHACL?
SPARQL 1.1 Overview
Shapes Constraint Language (SHACL) โ€” W3C Recommendation 2017
OWL 2 Web Ontology Language โ€” W3C Recommendation
RDF Schema โ€” W3C Recommendation
1 / 12

๐Ÿ“„ Chapter 7 Cheat Sheet

Key syntax, rules, and patterns for SHACL and ShEx. Keep this open during revision.

๐Ÿ›ก๏ธ SHACL Shape Declaration

Node shape targeting a class: ex:MyShape a sh:NodeShape ; sh:targetClass ex:Person ; sh:property [ sh:path ex:name ; sh:minCount 1 ; sh:datatype xsd:string ] .
Other target types: sh:targetNode ex:alice # specific node sh:targetSubjectsOf ex:p # all subjects of p sh:targetObjectsOf ex:p # all objects of p

๐Ÿ“Š Cardinality & Value Type

Cardinality constraints: sh:minCount 1 # at least 1 value sh:maxCount 1 # at most 1 value sh:minCount 1 ; sh:maxCount 1 # exactly 1
Value type constraints: sh:datatype xsd:string sh:datatype xsd:integer sh:class ex:Person sh:nodeKind sh:IRI sh:nodeKind sh:Literal sh:nodeKind sh:BlankNode

๐Ÿ”€ Logical Constraints

Boolean combinations: sh:and ( ex:ShapeA ex:ShapeB ) sh:or ( ex:ShapeA ex:ShapeB ) sh:not ex:ForbiddenShape sh:xone ( ex:A ex:B ) # exactly one
Closed shape (no extra properties): sh:closed true ; sh:ignoredProperties ( rdf:type ) ;

โœ… Validation Report Structure

sh:ValidationReport: [ a sh:ValidationReport ; sh:conforms false ; sh:result [ sh:focusNode ex:bob ; sh:resultPath ex:name ; sh:sourceConstraintComponent sh:MinCountConstraintComponent ; sh:resultMessage "Missing name" ] ]
Severity levels (set on any shape): sh:severity sh:Violation # default sh:severity sh:Warning sh:severity sh:Info

๐Ÿ“ ShEx Compact Syntax (ShExC)

Basic shape: <PersonShape> { ex:name xsd:string MinLength 1 ; ex:email IRI + ; ex:age xsd:integer MinInclusive 0 ? } # ? = optional + = 1+ * = 0+
Recursive shape & shape map: <OrgShape> { ex:name xsd:string ; ex:subOrg @<OrgShape> * # recursive! } # Shape map (node โ†’ shape binding): <http://ex.org/alice> @ <PersonShape>

โš–๏ธ SHACL vs ShEx Quick Reference

Key differences at a glance: Feature SHACL ShEx โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Syntax Turtle/RDF ShExC / ShExJ Targets sh:target* Shape Map Recursion โœ— Core โœ“ Native W3C Status Rec (2017) Sub (2019) Extensible SHACL-SPARQL Semantic Actions Tooling Jena, RDF4J shex.js, PyShEx Best for Enterprise KG Biomedical/Wiki

โšก SHACL-SPARQL Constraint

Embed a SPARQL SELECT โ€” any row = violation: ex:AgeShape a sh:NodeShape ; sh:targetClass ex:Person ; sh:sparql [ sh:message "Age inconsistent" ; sh:prefixes ex: ; sh:select """ SELECT $this WHERE { $this ex:birthYear ?by ; ex:age ?age . FILTER(?age != (2024 - ?by)) }""" ] .

๐Ÿ† Best Practices

Author & validate effectively: โœ“ Give shapes URIs (not blank nodes) โœ“ Add sh:message to every constraint โœ“ Use sh:severity to layer criticality โœ“ Keep shapes & data graphs separate โœ“ Validate early in your CI/CD pipeline โœ— Avoid sh:closed in open-world data โœ— Don't skip sh:ignoredProperties with sh:closed (rdf:type needed!) โœ“ Run OWL reasoner FIRST, then SHACL