Created
June 30, 2022 00:42
-
-
Save ashleysommer/ee6e4ee48f15c4244cbc79963aae82a5 to your computer and use it in GitHub Desktop.
Using ont_graph to mix-in ontology definitions at validation runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rdflib | |
from pyshacl import validate | |
shacl_file = """\ | |
@prefix owl: <http://www.w3.org/2002/07/owl#> . | |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
@prefix sh: <http://www.w3.org/ns/shacl#> . | |
@prefix ex: <http://example.com/ns#> . | |
ex:PersonShape | |
a sh:NodeShape ; | |
sh:targetClass ex:Person ; | |
sh:property [ | |
sh:path ex:hasPet ; | |
sh:class ex:Animal ; | |
sh:minCount 1 ; | |
sh:maxCount 1 ; | |
] ; | |
. | |
""" | |
ont_file = """\ | |
@prefix owl: <http://www.w3.org/2002/07/owl#> . | |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
@prefix ex: <http://example.com/ns#> . | |
ex:Person a owl:Class . | |
ex:Animal a owl:Class . | |
ex:Dog a owl:Class ; rdfs:subClassOf ex:Animal . | |
""" | |
data_file = """\ | |
@prefix ex: <http://example.com/ns#> . | |
ex:Brutus a ex:Dog . | |
ex:Jane a ex:Person ; ex:hasPet ex:Brutus . | |
""" | |
data = rdflib.Graph() | |
data.parse(data=data_file, format="turtle") | |
shapes = rdflib.Graph() | |
shapes.parse(data=shacl_file, format="turtle") | |
ont = rdflib.Graph() | |
ont.parse(data=ont_file, format="turtle") | |
res = validate(data, shacl_graph=shapes, ont_graph=ont) | |
conforms, graph, string = res | |
assert conforms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment