Skip to content
rossbowen edited this page Mar 13, 2023 · 2 revisions

SHACL stands for Shapes Constraint Language and is a language used for describing and validating the structure and content of RDF data.

SHACL is used to define constraints on RDF data, specifying the allowed shapes or patterns of data, and the constraints that data must satisfy in order to be considered valid. This allows data to be validated against a set of rules, helping to ensure that it conforms to a particular structure or format.

Resources

  • The SHACL specification has a complete specfication of the language with examples.
  • SHACL shapes defined in turtle format can be visualised using the SHACL Play! tool.

Tools

The examples in this wiki are included as files which can be retrieved by cloning the wiki's repo.

git clone https://github.com/GSS-Cogs/knowledge.wiki.git

Apache Jena

You can use the Apache Jena shacl command to validate SHACL shapes in turtle format against RDF data.

docker run -it -v $PWD:/workspace -w /workspace stain/jena \
shacl validate --shapes shapes.ttl --data data.ttl 

Shapes

The SHACL spec defines sh:NodeShapes and sh:PropertyShapes. A node shape defines constraints on a node within an RDF graph (subjects and objects of triples) and a property shape defines constraints on a property within an RDF graph.

Consider the following RDF data about two people who work for the same company.

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

ex:John a schema:Person ;
    schema:name "John Smith"@en ;
    schema:age 42 ;
    schema:worksFor ex:AcmeCorp ;
    .

ex:Mary a schema:Person ;
    schema:name "Mary Jones"@en ;
    schema:worksFor ex:AcmeCorp ;
    .

ex:AcmeCorp a schema:Organization ;
    schema:name "Acme Corporation"@en ;
    .

Targeting constraints

Related section in SHACL spec

We can define a constraint against a specific node by using the sh:targetNode property and passing the IRI of the node. This targets ex:John.

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

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

# Targets ex:John

More generally, we can define a constraint against all nodes of a specific class by using the sh:targetClass property and passing the IRI of the class. This targets all nodes of class schema:Person.

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

ex:PersonShape a sh:NodeShape ;
    sh:targetClass schema:Person ;
    .

# Targets ex:John, ex:Mary

We can target the subjects of all triples which have a particular property value by using the sh:targetSubjectsOf property and passing the IRI of the property. This targets all subjects which have the property schema:name.

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

ex:PersonShape a sh:NodeShape ;
    sh:targetSubjectsOf schema:name ;
    .

# Targets ex:John, ex:Mary, ex:AcmeCorp

Similarly, we can target objects by using the sh:targetObjectsOf property and passing the IRI of the property. This targets all objects which have the property schema:worksFor.

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

ex:PersonShape a sh:NodeShape ;
    sh:targetObjsectsOf schema:worksFor ;
    .

# Targets ex:AcmeCorp

Property constraints

Related section in SHACL spec

Property constraints are attached to node shapes by the sh:property property. The sh:path property of the property shape defines the IRI of the property to be constrained. Property shapes have sh:names and sh:descriptions to provide information about the constraints.

The following example defines a constraint on the schema:name property for all schema:Person nodes.

ex:PersonShape a sh:NodeShape ;
    sh:targetClass schema:Person ;
    sh:property [
        a sh:PropertyShape ;
        sh:path schema:name ;
        sh:name "Name" ;
        sh:description "All people need a name" ;
        sh:minCount 1 ;
    ] ;
    .

A full listing of constraints can be found in the SHACL spec.

Validating with Jena returns a sh:ValidationReport.

docker run -it -v $PWD:/workspace -w /workspace stain/jena \
shacl validate --shapes files/shacl-shapes.ttl --data files/shacl-data.ttl
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh:    <http://www.w3.org/ns/shacl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

[ rdf:type     sh:ValidationReport ;
  sh:conforms  true
] .

Exercise

Imagine creating a data catalogue app based on the DCAT standard. To make sure your app works correctly, you want to validate the data you receive. Write a SHACL shape which ensures:

All dcat:Datasets must have a:

  • dcterms:title
  • dcterms:description
  • dcterms:publisher
  • dcterms:license
  • dcat:distribution

The following properties, if present, should have a datatype of xsd:date or xsd:dateTime:

  • dcterms:issued
  • dcterms:modified

The following properties, if present, should point to a blank node or an IRI:

  • dcat:contactPoint
  • dcterms:publisher
  • dcterms:creator

The following properties, if present, should point to a literal:

  • dcat:keyword

Clone this wiki locally