Last active
May 2, 2019 03:56
-
-
Save JoelBender/c62c109e2f1bcb5f3bb27cf7622257fe to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
import sys | |
from io import BytesIO | |
from rdflib import Graph | |
from pyshacl.validate import Validator | |
data = b""" | |
@prefix bakery: <http://bakery.com/ns#> . | |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
@prefix owl: <http://www.w3.org/2002/07/owl#> . | |
bakery:hasIngredient rdf:type owl:ObjectProperty ; | |
rdfs:domain bakery:BakeryGood ; | |
rdfs:range bakery:Ingredient . | |
bakery:VeganFriendly rdf:type owl:Class . | |
bakery:NonVeganFriendly rdf:type owl:Class ; | |
owl:disjointWith bakery:VeganFriendly . | |
bakery:GlutenFree rdf:type owl:Class . | |
bakery:NonGlutenFree rdf:type owl:Class ; | |
owl:disjointWith bakery:GlutenFree . | |
bakery:Apple a bakery:VeganFriendly, bakery:GlutenFree . | |
bakery:Egg a bakery:NonVeganFriendly, bakery:GlutenFree . | |
bakery:Flour a bakery:VeganFriendly, bakery:NonGlutenFree . | |
bakery:AlmondFlour a bakery:VeganFriendly, bakery:GlutenFree . | |
bakery:RiceMilk a bakery:VeganFriendly, bakery:GlutenFree . | |
bakery:AppleTartA | |
a bakery:BakedGood ; | |
bakery:hasIngredient bakery:Apple, bakery:Egg, bakery:Flour . | |
bakery:AppleTartB | |
a bakery:BakedGood ; | |
bakery:hasIngredient bakery:Apple, bakery:RiceMilk, bakery:AlmondFlour . | |
bakery:AppleTartC | |
a bakery:BakedGood ; | |
bakery:hasIngredient bakery:Apple, bakery:RiceMilk, bakery:Flour . | |
bakery:AppleTartD | |
a bakery:BakedGood ; | |
bakery:hasIngredient bakery:Apple, bakery:Egg, bakery:AlmondFlour . | |
""" | |
data_graph = Graph() | |
data_graph.parse(BytesIO(data), format="turtle") | |
shacl = b""" | |
@prefix bakery: <http://bakery.com/ns#> . | |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
@prefix dash: <http://datashapes.org/dash#> . | |
@prefix sh: <http://www.w3.org/ns/shacl#> . | |
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | |
bakery:NonVeganFriendly | |
a rdfs:Class, sh:NodeShape . | |
bakery:NonGlutenFree | |
a rdfs:Class, sh:NodeShape . | |
bakery:BakedGood | |
a rdfs:Class, sh:NodeShape ; | |
sh:property [ | |
sh:path bakery:hasIngredient ; | |
sh:class bakery:Ingredient ; | |
sh:nodeKind sh:IRI ; | |
sh:minCount 1 ; | |
] ; | |
sh:rule [ | |
a sh:TripleRule ; | |
sh:subject sh:this ; | |
sh:predicate rdf:type ; | |
sh:object bakery:VeganBakedGood ; | |
sh:condition bakery:BakedGood ; | |
sh:condition [ | |
sh:property [ | |
sh:path bakery:hasIngredient ; | |
sh:qualifiedValueShape [ sh:class bakery:NonVeganFriendly ] ; | |
sh:qualifiedMaxCount 0 ; | |
] ; | |
] ; | |
] ; | |
sh:rule [ | |
a sh:TripleRule ; | |
sh:subject sh:this ; | |
sh:predicate rdf:type ; | |
sh:object bakery:NonVeganBakedGood ; | |
sh:condition bakery:BakedGood ; | |
sh:condition [ | |
sh:property [ | |
sh:path bakery:hasIngredient ; | |
sh:qualifiedValueShape [ sh:class bakery:NonVeganFriendly ] ; | |
sh:qualifiedMinCount 1 ; | |
] ; | |
] ; | |
] ; | |
sh:rule [ | |
a sh:TripleRule ; | |
sh:subject sh:this ; | |
sh:predicate rdf:type ; | |
sh:object bakery:GlutenFreeBakedGood ; | |
sh:condition bakery:BakedGood ; | |
sh:condition [ | |
sh:property [ | |
sh:path bakery:hasIngredient ; | |
sh:qualifiedValueShape [ sh:class bakery:NonGlutenFree ] ; | |
sh:qualifiedMaxCount 0 ; | |
] ; | |
] ; | |
] ; | |
sh:rule [ | |
a sh:TripleRule ; | |
sh:subject sh:this ; | |
sh:predicate rdf:type ; | |
sh:object bakery:NonGlutenFreeBakedGood ; | |
sh:condition bakery:BakedGood ; | |
sh:condition [ | |
sh:property [ | |
sh:path bakery:hasIngredient ; | |
sh:qualifiedValueShape [ sh:class bakery:NonGlutenFree] ; | |
sh:qualifiedMinCount 1 ; | |
] ; | |
] ; | |
] . | |
""" | |
shacl_graph = Graph() | |
shacl_graph.parse(BytesIO(shacl), format="turtle") | |
# v = Validator(data_graph, shacl_graph=shacl_graph) | |
# v = Validator(data_graph, shacl_graph=shacl_graph, options={"inference": "rdfs"}) | |
v = Validator( | |
data_graph, | |
shacl_graph=shacl_graph, | |
ont_graph=Graph(), | |
options={"inference": "rdfs"}, | |
) | |
conforms, report_graph, report_text = v.run() | |
print("conforms: {}".format(conforms)) | |
if not conforms: | |
print(report_text) | |
else: | |
expanded_graph = v.target_graph | |
if data_graph is expanded_graph: | |
print("data_graph is expanded_graph") | |
else: | |
xor_graph = data_graph ^ expanded_graph | |
print("----- data_graph ^ expanded_graph -----") | |
print(xor_graph.serialize(format="turtle").decode("utf-8")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment