Skip to content

Commit 4d4b720

Browse files
authored
Merge pull request #72 from casework/reduce_redundant_analysis
Reduce redundant case_validate analysis
2 parents 6a52cf2 + 4186363 commit 4d4b720

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

case_utils/case_validate/__init__.py

+20
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
ValidationResult,
5151
)
5252
from case_utils.case_validate.validate_utils import (
53+
disable_tbox_review,
5354
get_invalid_cdo_concepts,
5455
get_ontology_graph,
5556
)
@@ -65,6 +66,7 @@ def validate(
6566
input_file: Union[List[str], str],
6667
*args: Any,
6768
case_version: Optional[str] = None,
69+
review_tbox: bool = False,
6870
supplemental_graphs: Optional[List[str]] = None,
6971
**kwargs: Any,
7072
) -> ValidationResult:
@@ -74,6 +76,7 @@ def validate(
7476
:param *args: The positional arguments to pass to the underlying pyshacl.validate function.
7577
:param input_file: The path to the file containing the data graph to validate. This can also be a list of paths to files containing data graphs to pool together.
7678
:param case_version: The version of the CASE ontology to use (e.g. 1.2.0). If None, the most recent version will be used.
79+
:param review_tbox: If True, SHACL shapes that review OWL Classes, OWL Properties, and SHACL shapes that constrain those classes and properties will be used in the review. Otherwise, those shapes will be deactivated before running validation. Be aware that these shapes are known to significantly increase the validation run time.
7780
:param supplemental_graphs: File paths to supplemental graphs to use. If None, no supplemental graphs will be used.
7881
:param allow_warnings: In addition to affecting the conformance of SHACL validation, this will affect conformance based on unrecognized CDO concepts (likely, misspelled or miscapitalized) in the data graph. If allow_warnings is not True, any unrecognized concept using a CDO IRI prefix will cause conformance to be False.
7982
:param inference: The type of inference to use. If "none" (type str), no inference will be used. If None (type NoneType), pyshacl defaults will be used. Note that at the time of this writing (pySHACL 0.23.0), pyshacl defaults are no inferencing for the data graph, and RDFS inferencing for the SHACL graph, which for case_utils.validate includes the SHACL and OWL graphs.
@@ -94,6 +97,17 @@ def validate(
9497
# Get the ontology graph from the case_version and supplemental_graphs arguments
9598
ontology_graph: Graph = get_ontology_graph(case_version, supplemental_graphs)
9699

100+
if not review_tbox:
101+
# This is done because, at the time of pyshacl 0.20.0, the
102+
# entirety of the ontology graph is mixed into the data graph.
103+
# UCO 1.0.0 includes some mechanisms to cross-check SHACL
104+
# PropertyShapes versus OWL property definitions. Because of
105+
# the mix-in, all of the ontology graph (.validate ont_graph
106+
# kwarg) is reviewed by the SHACL graph (.validate shacl_graph
107+
# kwarg), so for UCO 1.0.0 that adds around 30 seconds to each
108+
# case_validate call, redundantly reviewing UCO.
109+
disable_tbox_review(ontology_graph)
110+
97111
# Get the undefined CDO concepts.
98112
undefined_cdo_concepts = get_invalid_cdo_concepts(data_graph, ontology_graph)
99113

@@ -225,6 +239,11 @@ def main() -> None:
225239
help='(ALMOST as with pyshacl CLI) Send output to a file. If absent, output will be written to stdout. Difference: If specified, file is expected not to exist. Clarification: Does NOT influence --format flag\'s default value of "human". (I.e., any machine-readable serialization format must be specified with --format.)',
226240
default=sys.stdout,
227241
)
242+
parser.add_argument(
243+
"--review-tbox",
244+
action="store_true",
245+
help='Enable rules for reviewing OWL Classes, Properties, and SHACL shapes that constrain them (i.e. the "TBox", or "Theorem box", of the data graph and ontology graph; in contrast, the "ABox", or "Axiom box", contains the declarations of members of those classes, and users of those properties). This should be used when adding extension classes or properties not adopted by UCO or its downstream ontologies, e.g. when using a drafting namespace. Be aware that these rules are known to significantly increase the validation run time.',
246+
)
228247

229248
parser.add_argument("in_graph", nargs="+")
230249

@@ -250,6 +269,7 @@ def main() -> None:
250269
do_owl_imports=True if args.imports else False,
251270
inference=args.inference,
252271
meta_shacl=args.metashacl,
272+
review_tbox=True if args.review_tbox else False,
253273
supplemental_graphs=args.ontology_graph,
254274
**validator_kwargs,
255275
)

case_utils/case_validate/validate_utils.py

+19
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,22 @@ def get_ontology_graph(
182182
ontology_graph.parse(arg_ontology_graph)
183183

184184
return ontology_graph
185+
186+
187+
def disable_tbox_review(graph: rdflib.Graph) -> None:
188+
l_true = rdflib.Literal(True)
189+
ns_uco_owl = rdflib.Namespace("https://ontology.unifiedcyberontology.org/owl/")
190+
191+
for tbox_shape_basename in {
192+
"DataOneOf-shape",
193+
"DatatypeProperty-shacl-constraints-shape",
194+
"Disjointedness-AP-DP-shape",
195+
"Disjointedness-AP-OP-shape",
196+
"Disjointedness-C-DT-shape",
197+
"Disjointedness-DP-OP-shape",
198+
"ObjectProperty-shacl-constraints-shape",
199+
"ontologyIRI-versionIRI-prerequisite-shape",
200+
"versionIRI-nodeKind-shape",
201+
}:
202+
n_tbox_shape = ns_uco_owl[tbox_shape_basename]
203+
graph.add((n_tbox_shape, NS_SH.deactivated, l_true))

tests/case_utils/case_validate/uco_test_examples/Makefile

+31
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,37 @@ all: \
102102
rm __$@
103103
mv _$@ $@
104104

105+
# NOTE - this more-specific recipe enables "tbox" review, but otherwise
106+
# matches the wildcarded recipe.
107+
owl_properties_XFAIL_validation.ttl: \
108+
$(examples_srcdir)/owl_properties_XFAIL.json \
109+
$(tests_srcdir)/.venv.done.log \
110+
$(top_srcdir)/.ontology.done.log \
111+
$(top_srcdir)/case_utils/case_validate/__init__.py \
112+
$(top_srcdir)/case_utils/case_validate/validate_types.py \
113+
$(top_srcdir)/case_utils/case_validate/validate_utils.py \
114+
$(top_srcdir)/case_utils/ontology/__init__.py
115+
source $(tests_srcdir)/venv/bin/activate \
116+
&& case_validate \
117+
--allow-warnings \
118+
--debug \
119+
--format turtle \
120+
--review-tbox \
121+
$< \
122+
> __$@ \
123+
; rc=$$? ; test 0 -eq $$rc -o 1 -eq $$rc
124+
@#Fail if output is empty.
125+
@test -s __$@ \
126+
|| exit 1
127+
java -jar $(RDF_TOOLKIT_JAR) \
128+
--inline-blank-nodes \
129+
--source __$@ \
130+
--source-format turtle \
131+
--target _$@ \
132+
--target-format turtle
133+
rm __$@
134+
mv _$@ $@
135+
105136
check: \
106137
$(validation_ttls)
107138
source $(tests_srcdir)/venv/bin/activate \

0 commit comments

Comments
 (0)