Skip to content

Commit ce8da08

Browse files
committed
Filter SHACL statements from data graph's ontology mix-in
References: * RDFLib/pySHACL#89 Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
1 parent 9b78310 commit ce8da08

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

case_utils/case_validate/__init__.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,37 @@ def main() -> None:
142142
_logger.debug("arg_ontology_graph = %r.", arg_ontology_graph)
143143
ontology_graph.parse(arg_ontology_graph)
144144

145+
# Filter the graph pyshacl uses as its ontology mix-in to exclude
146+
# all SHACL-related triples.
147+
# This is done because, at the time of pyshacl 0.20.0, the entirety
148+
# of the ontology graph is mixed into the data graph. UCO 1.0.0
149+
# includes some mechanisms to cross-check SHACL PropertyShapes
150+
# versus OWL property definitions. Because of the mix-in, all of
151+
# the ontology graph (.validate ont_graph kwarg) is reviewed by the
152+
# SHACL graph (.validate shacl_graph kwarg), so for UCO 1.0.0 that
153+
# adds around 30 seconds to each case_validate call, redundantly
154+
# reviewing UCO.
155+
# The ontology graph (.validate ont_graph kwarg) is currently
156+
# believed to never need to know about SHACL concepts.
157+
ontology_graph_without_shacl = rdflib.Graph()
158+
SH_prefix = str(rdflib.SH)
159+
for triple in ontology_graph.triples((None, None, None)):
160+
skip_triple = False
161+
for triple_part in triple:
162+
if isinstance(triple_part, rdflib.URIRef):
163+
if str(triple_part).startswith(SH_prefix):
164+
skip_triple = True
165+
if skip_triple:
166+
break
167+
if skip_triple:
168+
continue
169+
ontology_graph_without_shacl.add(triple)
170+
# _logger.debug("len(ontology_graph) = %d.", len(ontology_graph))
171+
# _logger.debug("len(ontology_graph_without_shacl) = %d.", len(ontology_graph_without_shacl))
172+
# At the time of CASE 1.0.0, this was the debug output:
173+
# DEBUG:__init__.py:len(ontology_graph) = 13499.
174+
# DEBUG:__init__.py:len(ontology_graph_without_shacl) = 7639.
175+
145176
# Determine output format.
146177
# pySHACL's determination of output formatting is handled solely
147178
# through the -f flag. Other CASE CLI tools handle format
@@ -158,7 +189,7 @@ def main() -> None:
158189
validate_result = pyshacl.validate(
159190
data_graph,
160191
shacl_graph=ontology_graph,
161-
ont_graph=ontology_graph,
192+
ont_graph=ontology_graph_without_shacl,
162193
inference=args.inference,
163194
abort_on_first=args.abort,
164195
allow_warnings=True if args.allow_warnings else False,

0 commit comments

Comments
 (0)