SHACL


Winter 2019
Instructor: Raghava Mutharaju
IIIT-Delhi
IIIT Delhi

Introduction

  • SHACL stands for Shapes Constraint Language
  • It is used for validating RDF graphs against a set of conditions
  • These conditions/constraints are provided in the form of shapes and are referred to as "shapes graph"
  • RDF graphs that are validated against the shapes graph are called as "data graphs"
  • SHACL follows closed world assumption
  • SHACL namespace is as follows
    • sh: http://www.w3.org/ns/shacl#
  • Focus node: An RDF term that is validated against a "shape" using the triples from the "data graph"
  • Two types of shapes
    • Node shapes: shapes about the focus node
    • Property shapes: shapes about the values of a property or path for the focus node

Shapes Graph

ex:PersonShape
	a sh:NodeShape ;
	sh:targetNode ex:Alice .

Data Graph

ex:Alice a ex:Person .
ex:Bob a ex:Person .

ex:Alice is the focus node

Severity of a Shape


Severity Description
sh:Info A non-critical constraint violation indicating an informative message
sh:Warning A non-critical constraint violation indicating a warning
sh:Violation A constraint violation

sh:Violation is the default if sh:severity is unspecified

Example

Shapes Graph

ex:MyShape
a sh:NodeShape ;
sh:targetNode ex:MyInstance ;
sh:property [    # _:b1
	# Violations of sh:minCount and sh:datatype are produced as warnings
	sh:path ex:myProperty ;
	sh:minCount 1 ;
	sh:datatype xsd:string ;
	sh:severity sh:Warning ;
] ;
sh:property [    # _:b2
	# The default severity here is sh:Violation
	sh:path ex:myProperty ;
	sh:maxLength 10 ;
	sh:message "Too many characters"@en ;
	sh:message "Zu viele Zeichen"@de ;
] .					

Data Graph

ex:MyInstance
	ex:myProperty "http://toomanycharacters"^^xsd:anyURI .

Validation Results

[	
a sh:ValidationReport ;
sh:conforms false ;
sh:result
[	a sh:ValidationResult ;
	sh:resultSeverity sh:Warning ;
	sh:focusNode ex:MyInstance ;
	sh:resultPath ex:myProperty ;
	sh:value "http://toomanycharacters"^^xsd:anyURI ;
	sh:sourceConstraintComponent sh:DatatypeConstraintComponent ;
	sh:sourceShape _:b1 ;
] ,
[	a sh:ValidationResult ;
	sh:resultSeverity sh:Violation ;
	sh:focusNode ex:MyInstance ;
	sh:resultPath ex:myProperty ;
	sh:value "http://toomanycharacters"^^xsd:anyURI ;
	sh:resultMessage "Too many characters"@en ;
	sh:resultMessage "Zu viele Zeichen"@de ;
	sh:sourceConstraintComponent sh:MaxLengthConstraintComponent ;
	sh:sourceShape _:b2 ;
]
] .

Shapes Graph

ex:PersonShape
	a sh:NodeShape ;
	sh:targetClass ex:Person ;    # Applies to all persons
	sh:property [                 # _:b1
		sh:path ex:ssn ;           # constrains the values of ex:ssn
		sh:maxCount 1 ;
		sh:datatype xsd:string ;
		sh:pattern "^\\d{3}-\\d{2}-\\d{4}$" ;
	] ;
	sh:property [                 # _:b2
		sh:path ex:worksFor ;
		sh:class ex:Company ;
		sh:nodeKind sh:IRI ;
	] ;
	sh:closed true ;
	sh:ignoredProperties ( rdf:type ) .

Data Graph

ex:Alice
	a ex:Person ;
	ex:ssn "987-65-432A" .
  
ex:Bob
	a ex:Person ;
	ex:ssn "123-45-6789" ;
	ex:ssn "124-35-6789" .
  
ex:Calvin
	a ex:Person ;
	ex:birthDate "1971-07-07"^^xsd:date ;
	ex:worksFor ex:UntypedCompany .

Validation Result

[	a sh:ValidationReport ;
	sh:conforms false ;
	sh:result
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Alice ;
		sh:resultPath ex:ssn ;
		sh:value "987-65-432A" ;
		sh:sourceConstraintComponent sh:RegexConstraintComponent ;
		sh:sourceShape ... blank node _:b1 on ex:ssn above ... ;
	] ,
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Bob ;
		sh:resultPath ex:ssn ;
		sh:sourceConstraintComponent sh:MaxCountConstraintComponent ;
		sh:sourceShape ... blank node _:b1 on ex:ssn above ... ;
	] ,
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Calvin ;
		sh:resultPath ex:worksFor ;
		sh:value ex:UntypedCompany ;
		sh:sourceConstraintComponent sh:ClassConstraintComponent ;
		sh:sourceShape ... blank node _:b2 on ex:worksFor above ... ;
	] ,
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Calvin ;
		sh:resultPath ex:birthDate ;
		sh:value "1971-07-07"^^xsd:date ;
		sh:sourceConstraintComponent sh:ClosedConstraintComponent ;
		sh:sourceShape sh:PersonShape ;
	] 
] .

SHACL Property Paths


SPARQL Property path: ex:parent
SHACL Property path: ex:parent

SPARQL Property path: ^ex:parent
SHACL Property path: [ sh:inversePath ex:parent ]

SPARQL Property path: ex:parent/ex:firstName
SHACL Property path: ( ex:parent ex:firstName )

SPARQL Property path: rdf:type/rdfs:subClassOf*
SHACL Property path: ( rdf:type [ sh:zeroOrMorePath rdfs:subClassOf ] )

SPARQL Property path: ex:father|ex:mother
SHACL Property path: [ sh:alternativePath ( ex:father ex:mother  ) ]

Demo


References


  1. Shapes Constraint Language (SHACL). Holger Knublauch, Dimitris Kontokostas. W3C Recommendation.
  2. SHACL Tutorial