Skip to content

Instantly share code, notes, and snippets.

@ashleysommer
Created June 30, 2022 00:54
Show Gist options
  • Save ashleysommer/2d718b2456442da3353da86072caf8fc to your computer and use it in GitHub Desktop.
Save ashleysommer/2d718b2456442da3353da86072caf8fc to your computer and use it in GitHub Desktop.
Example of using ont_graph and "inplace=True"
import rdflib
from pyshacl import validate
shacl_file = """\
@prefix ex: <urn:ex#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@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#> .
ex:Class a owl:Class .
ex:SubClass a owl:Class ;
rdfs:subClassOf ex:Class .
ex:SubSubClass a owl:Class ;
rdfs:subClassOf ex:SubClass .
ex:FailedRule a sh:NodeShape ;
sh:targetClass ex:Class ;
sh:rule [
a sh:TripleRule ;
sh:object ex:Inferred ;
sh:predicate ex:hasProperty ;
sh:subject sh:this ;
] .
"""
ont_file = """\
@prefix ex: <urn:ex#> .
@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#> .
ex:Class a owl:Class .
ex:SubClass a owl:Class ;
rdfs:subClassOf ex:Class .
ex:SubSubClass a owl:Class ;
rdfs:subClassOf ex:SubClass .
"""
data_file = """\
@prefix ex: <urn:ex#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ex:A a ex:SubSubClass .
"""
data_g = rdflib.Graph()
data_g.parse(data=data_file, format="turtle")
shapes_g = rdflib.Graph()
shapes_g.parse(data=shacl_file, format="turtle")
ont_g = rdflib.Graph()
ont_g.parse(data=ont_file, format="turtle")
res = validate(data_g, shacl_graph=shapes_g, ont_graph=ont_g, inplace=True, advanced=True)
conforms, graph, string = res
assert (rdflib.URIRef("urn:ex#A"), rdflib.URIRef("urn:ex#hasProperty"), rdflib.URIRef("urn:ex#Inferred")) in data_g
assert conforms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment