Skip to content

Instantly share code, notes, and snippets.

@ashleysommer
Created April 7, 2022 21:45
Show Gist options
  • Save ashleysommer/155b94b36eaa3f5786f52d57d9d42e7b to your computer and use it in GitHub Desktop.
Save ashleysommer/155b94b36eaa3f5786f52d57d9d42e7b to your computer and use it in GitHub Desktop.
pyshacl issue 141 alternative represenation
# -*- coding: utf-8 -*-
"""
https://github.com/RDFLib/pySHACL/issues/141
"""
import rdflib
from pyshacl import validate
shacl_file = r"""@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ex: <http://example.org/ontology/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
ex:Dog
a owl:Class , sh:NodeShape ;
sh:property [
a sh:PropertyShape ;
sh:minCount 1 ;
sh:nodeKind sh:BlankNodeOrIRI ;
sh:path ex:hasIdentifier
] ;
sh:targetClass ex:Dog ;
.
ex:Cat
a owl:Class , sh:NodeShape ;
sh:property [
a sh:PropertyShape ;
sh:minCount 1 ;
sh:nodeKind sh:BlankNodeOrIRI ;
sh:path ex:hasCharacteristic
] ;
sh:targetClass ex:Cat ;
.
ex:hasIdentifier a owl:ObjectProperty ;
.
ex:hasCharacteristic a owl:ObjectProperty ;
.
ex:hasName a owl:ObjectProperty ;
rdfs:subPropertyOf ex:hasIdentifier , ex:hasCharacteristic ;
.
ex:hasLabel a owl:ObjectProperty ;
rdfs:subPropertyOf ex:hasIdentifier ;
.
"""
data_file = """@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ex: <http://example.org/ontology/> .
@prefix kb: <http://example.org/kb/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
kb:dog_with_identifier
a ex:Dog ;
rdfs:comment "PASS" ;
ex:hasIdentifier kb:dogidentifier1 ;
.
kb:dog_with_name
a ex:Dog ;
rdfs:comment "PASS" ;
ex:hasName [ rdf:value "Rover" ] ;
.
kb:dog_with_characteristic
a ex:Dog ;
rdfs:comment "XFAIL" ;
ex:hasCharacteristic [ rdf:value "Brown" ] ;
.
kb:cat_with_identifier
a ex:Cat ;
rdfs:comment "XFAIL" ;
ex:hasIdentifier kb:catidentifier1 ;
.
kb:cat_with_name
a ex:Cat ;
rdfs:comment "PASS" ;
ex:hasName [ rdf:value "Mittens" ] ;
.
kb:cat_with_characteristic
a ex:Cat ;
rdfs:comment "PASS" ;
ex:hasCharacteristic [ rdf:value "Green" ] ;
.
kb:dog_with_label
a ex:Dog ;
rdfs:comment "PASS" ;
ex:hasLabel [ rdf:value "Dopey" ] ;
.
"""
def test_141() -> None:
data = rdflib.Graph()
data.parse(data=data_file, format="turtle")
shapes = rdflib.Graph()
shapes.parse(data=shacl_file, format="turtle")
res = validate(
data,
shacl_graph=shapes,
data_graph_format='turtle',
shacl_graph_format='turtle',
debug=True,
inference="rdfs",
)
conforms, graph, string = res
if __name__ == "__main__":
test_141()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment