SHACL#
This document contains a short introduction to RDF using rudof.
Preliminaries: Install and configure rudof#
The library is available as pyrudof.
# @title
!pip install pyrudof
from pyrudof import Rudof, RudofConfig, RudofError
rudof = Rudof(RudofConfig())
Requirement already satisfied: pyrudof in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (0.2.9)
Validate using SHACL#
# @title
rudof = Rudof(RudofConfig())
Let’s read some RDF data.
rudof.read_data("""
prefix : <http://example.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix schema: <http://schema.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
:timbl rdf:type :Human ;
:name "Tim Berners-Lee" ;
:birthPlace :london ;
:birthDate "1955-06-08"^^xsd:date ;
:employer :CERN ;
:knows _:1 .
:london rdf:type :city, :metropolis ;
:country :UK .
:CERN rdf:type :Organization .
_:1 :birthPlace :Spain .
""")
rudof.read_shacl("""
prefix : <http://example.org/>
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#>
prefix schema: <http://schema.org/>
:Person a sh:NodeShape ;
sh:targetClass :Human ;
sh:property [ sh:path :name ;
sh:datatype xsd:string;
sh:minCount 1 ; sh:maxCount 1 ] ;
sh:property [ sh:path :birthDate ; sh:datatype xsd:date;
sh:maxCount 1 ] ;
sh:property [ sh:path :birthPlace ;
sh:node :Place; sh:maxCount 1 ] ;
sh:property [
sh:path :employer ;
sh:node :Organization
] .
:Place a sh:NodeShape .
:Organization a sh:NodeShape .
""")
rudof.validate_shacl()
result = rudof.serialize_shacl_validation_results()
print(result)
No Errors found
The previous data didn’t have errors. We can add some data with errors like the following:
rudof.read_data("""
prefix : <http://example.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix schema: <http://schema.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
:wrong1 rdf:type :Human ;
:name "John" ;
:birthDate "1955-06-08"^^xsd:date, "1956-06-08"^^xsd:date .
:wrong2 rdf:type :Human ;
:name 23 ;
:birthDate "1955-06-08"^^xsd:date .
""", merge = True)
rudof.validate_shacl()
result = rudof.serialize_shacl_validation_results()
print(result)
╭────────────────────────────────────────┬─────────┬───────────────────────────────────────┬────────────┬───────┬────────────────────────────────────┬───────────────────────────────╮
│ Severity │ Node │ Component │ Path │ Value │ Source shape │ Details │
├────────────────────────────────────────┼─────────┼───────────────────────────────────────┼────────────┼───────┼────────────────────────────────────┼───────────────────────────────┤
│ <http://www.w3.org/ns/shacl#Violation> │ :wrong2 │ <http://www.w3.org/ns/shacl#datatype> │ :name │ 23 │ _:dc37a82a8924decfbf01954a654842a7 │ Expected datatype: xsd:string │
├────────────────────────────────────────┼─────────┼───────────────────────────────────────┼────────────┼───────┼────────────────────────────────────┼───────────────────────────────┤
│ <http://www.w3.org/ns/shacl#Violation> │ :wrong1 │ <http://www.w3.org/ns/shacl#maxCount> │ :birthDate │ │ _:cd7d3e93b6ab85fac64dbbefac4c8c8f │ MaxCount(1) not satisfied │
╰────────────────────────────────────────┴─────────┴───────────────────────────────────────┴────────────┴───────┴────────────────────────────────────┴───────────────────────────────╯