1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
# -*- coding: utf-8 -*-
#
# Extra tests which are not part of the SHT or DASH test suites,
# nor the discrete issues tests or the cmdline_test file.
# The need for these tests are discovered by doing coverage checks and these
# are added as required.
import os
import re
from rdflib import Graph, Dataset
from pyshacl import validate
from pyshacl.errors import ReportableRuntimeError
ontology_graph_text = """
@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 xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix exOnt: <http://example.com/exOnt#> .
<http://example.com/exOnt> a owl:Ontology ;
rdfs:label "An example extra-ontology file."@en .
exOnt:Animal a rdfs:Class ;
rdfs:comment "The parent class for Humans and Pets"@en ;
rdfs:subClassOf owl:Thing .
exOnt:Human a rdfs:Class ;
rdfs:comment "A Human being"@en ;
rdfs:subClassOf exOnt:Animal .
exOnt:Pet a rdfs:Class ;
rdfs:comment "An animal owned by a human"@en ;
rdfs:subClassOf exOnt:Animal .
exOnt:hasPet a rdf:Property ;
rdfs:domain exOnt:Human ;
rdfs:range exOnt:Pet .
exOnt:nLegs a rdf:Property ;
rdfs:domain exOnt:Animal ;
rdfs:range xsd:integer .
exOnt:Teacher a rdfs:Class ;
rdfs:comment "A Human who is a teacher."@en ;
rdfs:subClassOf exOnt:Human .
exOnt:PreschoolTeacher a rdfs:Class ;
rdfs:comment "A Teacher who teaches preschool."@en ;
rdfs:subClassOf exOnt:Teacher .
exOnt:Lizard a rdfs:Class ;
rdfs:subClassOf exOnt:Pet .
exOnt:Goanna a rdfs:Class ;
rdfs:subClassOf exOnt:Lizard .
"""
ontology_ds_text = """
@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 xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix exOnt: <http://example.com/exOnt#> .
# This is a TRIG file.
<http://example.com/exOnt-graph> {
<http://example.com/exOnt> a owl:Ontology ;
rdfs:label "An example extra-ontology file."@en .
exOnt:Animal a rdfs:Class ;
rdfs:comment "The parent class for Humans and Pets"@en ;
rdfs:subClassOf owl:Thing .
exOnt:Human a rdfs:Class ;
rdfs:comment "A Human being"@en ;
rdfs:subClassOf exOnt:Animal .
exOnt:Pet a rdfs:Class ;
rdfs:comment "An animal owned by a human"@en ;
rdfs:subClassOf exOnt:Animal .
exOnt:hasPet a rdf:Property ;
rdfs:domain exOnt:Human ;
rdfs:range exOnt:Pet .
exOnt:nLegs a rdf:Property ;
rdfs:domain exOnt:Animal ;
rdfs:range xsd:integer .
exOnt:Teacher a rdfs:Class ;
rdfs:comment "A Human who is a teacher."@en ;
rdfs:subClassOf exOnt:Human .
exOnt:PreschoolTeacher a rdfs:Class ;
rdfs:comment "A Teacher who teaches preschool."@en ;
rdfs:subClassOf exOnt:Teacher .
exOnt:Lizard a rdfs:Class ;
rdfs:subClassOf exOnt:Pet .
exOnt:Goanna a rdfs:Class ;
rdfs:subClassOf exOnt:Lizard .
}
"""
shacl_file_text = """
@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 xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix exShape: <http://example.com/exShape#> .
@prefix exOnt: <http://example.com/exOnt#> .
<http://example.com/exShape> a owl:Ontology ;
rdfs:label "Example Shapes File"@en .
exShape:HumanShape a sh:NodeShape ;
sh:property [
sh:class exOnt:Pet ;
sh:path exOnt:hasPet ;
] ;
sh:property [
sh:datatype xsd:integer ;
sh:path exOnt:nLegs ;
sh:maxInclusive 2 ;
sh:minInclusive 2 ;
] ;
sh:targetClass exOnt:Human .
exShape:AnimalShape a sh:NodeShape ;
sh:property [
sh:datatype xsd:integer ;
sh:path exOnt:nLegs ;
sh:maxInclusive 4 ;
sh:minInclusive 1 ;
] ;
sh:targetClass exOnt:Animal .
"""
data_file_text = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix exOnt: <http://example.com/exOnt#> .
@prefix ex: <http://example.com/ex#> .
ex:Human1 rdf:type exOnt:PreschoolTeacher ;
rdf:label "Amy" ;
exOnt:nLegs "2"^^xsd:integer ;
exOnt:hasPet ex:Pet1 .
ex:Pet1 rdf:type exOnt:Goanna ;
rdf:label "Sebastian" ;
exOnt:nLegs "4"^^xsd:integer .
"""
data_file_text_bad = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix exOnt: <http://example.com/exOnt#> .
@prefix ex: <http://example.com/ex#> .
ex:Human1 rdf:type exOnt:PreschoolTeacher ;
rdf:label "Amy" ;
exOnt:nLegs "2"^^xsd:integer ;
exOnt:hasPet "Sebastian"^^xsd:string .
ex:Pet1 rdf:type exOnt:Goanna ;
rdf:label "Sebastian" ;
exOnt:nLegs "four"^^xsd:string .
"""
def test_validate_ds_with_graph_ontology():
ds = Dataset()
ds.parse(data=data_file_text_bad, format='turtle')
extra_g = Graph()
extra_g.parse(data=ontology_graph_text, format='turtle')
ds_len = len(ds)
res = validate(
ds, shacl_graph=shacl_file_text, shacl_graph_format='turtle', ont_graph=extra_g, inference='rdfs', debug=True
)
conforms, graph, string = res
assert not conforms
# Assert that the dataset is unchanged
ds_len2 = len(ds)
assert ds_len2 == ds_len
def test_validate_ds_with_ds_ontology():
ds = Dataset()
ds.parse(data=data_file_text_bad, format='turtle')
extra_ds = Dataset()
extra_ds.parse(data=ontology_ds_text, format='trig')
ds_len = len(ds)
res = validate(
ds, shacl_graph=shacl_file_text, shacl_graph_format='turtle', ont_graph=extra_ds, inference='rdfs', debug=True
)
conforms, graph, string = res
assert not conforms
# Assert that the dataset is unchanged
ds_len2 = len(ds)
assert ds_len2 == ds_len
def test_validate_ds_with_ds_ontology_inplace():
ds = Dataset()
ds.parse(data=data_file_text_bad, format='turtle')
extra_ds = Dataset()
extra_ds.parse(data=ontology_ds_text, format='trig')
ds_len = len(ds)
res = validate(
ds,
shacl_graph=shacl_file_text,
shacl_graph_format='turtle',
ont_graph=extra_ds,
inference='rdfs',
debug=True,
inplace=True
)
conforms, graph, string = res
assert not conforms
# Assert that the dataset is changed
ds_len2 = len(ds)
assert ds_len2 != ds_len
a = ds.serialize(format='trig')
print(a)
|