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
rudof = Rudof(RudofConfig())
Requirement already satisfied: pyrudof in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (0.1.135)
Validate using SHACL#
# @title
rudof = Rudof(RudofConfig())
Let’s read some RDF data.
rudof.read_data_str("""
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_str("""
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 .
""")
result = rudof.validate_shacl()
print(result.show_as_table())
No Errors found
The previous data didn’t have errors. We can add some data with errors like the following:
rudof.read_data_str("""
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)
result = rudof.validate_shacl()
print(result.show_as_table())
╭──────────────┬─────────┬─────────────┬─────────────┬───────┬────────────────────────────────────╮
│ Severity │ node │ Component │ Path │ value │ Source shape │
├──────────────┼─────────┼─────────────┼─────────────┼───────┼────────────────────────────────────┤
│ sh:Violation │ :wrong2 │ sh:datatype │ :name, │ 23 │ _:edd5fe5ea01baa808ca2e15c5cbbdef3 │
├──────────────┼─────────┼─────────────┼─────────────┼───────┼────────────────────────────────────┤
│ sh:Violation │ :wrong1 │ sh:maxCount │ :birthDate, │ │ _:ffc0ada4954df415be29ca5f72a803c7 │
╰──────────────┴─────────┴─────────────┴─────────────┴───────┴────────────────────────────────────╯