Skip to content

Commit 64bd95c

Browse files
committed
Merge branch 'develop' into library-usage
2 parents 97d7fbb + 82461b3 commit 64bd95c

File tree

2 files changed

+67
-37
lines changed

2 files changed

+67
-37
lines changed

case_utils/case_validate/__init__.py

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,44 +92,40 @@ def concept_is_cdo_concept(n_concept: rdflib.URIRef) -> bool:
9292
) or concept_iri.startswith("https://ontology.caseontology.org/")
9393

9494

95-
def get_ontology_graph(
96-
case_version: Optional[str] = None, supplemental_graphs: Optional[List[str]] = None
97-
) -> rdflib.Graph:
98-
"""
99-
Get the ontology graph for the given case_version and any supplemental graphs.
100-
101-
:param case_version: the version of the CASE ontology to use. If None (i.e. null), the most recent version will be used. If "none" (the string), no pre-built version of CASE will be used.
102-
:param supplemental_graphs: a list of supplemental graphs to use. If None, no supplemental graphs will be used.
103-
:return: the ontology graph against which to validate the data graph.
104-
"""
105-
ontology_graph = rdflib.Graph()
106-
107-
if case_version != "none":
108-
# Load bundled CASE ontology at requested version.
109-
if case_version is None:
110-
case_version = CURRENT_CASE_VERSION
111-
ttl_filename = case_version + ".ttl"
112-
_logger.debug("ttl_filename = %r.", ttl_filename)
113-
ttl_data = importlib.resources.read_text(case_utils.ontology, ttl_filename)
114-
ontology_graph.parse(data=ttl_data, format="turtle")
115-
116-
if supplemental_graphs:
117-
for arg_ontology_graph in supplemental_graphs:
118-
_logger.debug("arg_ontology_graph = %r.", arg_ontology_graph)
119-
ontology_graph.parse(arg_ontology_graph)
120-
121-
return ontology_graph
122-
123-
12495
def get_invalid_cdo_concepts(
125-
data_graph: Graph, ontology_graph: Graph
96+
data_graph: rdflib.Graph, ontology_graph: rdflib.Graph
12697
) -> Set[rdflib.URIRef]:
12798
"""
128-
Get the set of concepts in the data graph that are not part of the CDO ontologies.
99+
Get the set of concepts in the data graph that are not part of the CDO ontologies as specified with the ontology_graph argument.
129100
130101
:param data_graph: The data graph to validate.
131102
:param ontology_graph: The ontology graph to use for validation.
132-
:return: The set of concepts in the data graph that are not part of the CDO ontologies.
103+
:return: The list of concepts in the data graph that are not part of the CDO ontology.
104+
105+
>>> from case_utils.namespace import NS_RDF, NS_OWL, NS_UCO_CORE
106+
>>> from rdflib import Graph, Literal, Namespace, URIRef
107+
>>> # Define a namespace for a knowledge base, and a namespace for custom extensions.
108+
>>> ns_kb = Namespace("http://example.org/kb/")
109+
>>> ns_ex = Namespace("http://example.org/ontology/")
110+
>>> dg = Graph()
111+
>>> og = Graph()
112+
>>> # Use an ontology graph in review that includes only a single class and a single property excerpted from UCO, but also a single custom property.
113+
>>> _ = og.add((NS_UCO_CORE.UcoObject, NS_RDF.type, NS_OWL.Class))
114+
>>> _ = og.add((NS_UCO_CORE.name, NS_RDF.type, NS_OWL.DatatypeProperty))
115+
>>> _ = og.add((ns_ex.ourCustomProperty, NS_RDF.type, NS_OWL.DatatypeProperty))
116+
>>> # Define an individual.
117+
>>> n_uco_object = ns_kb["UcoObject-f494d239-d9fd-48da-bc07-461ba86d8c6c"]
118+
>>> n_uco_object
119+
rdflib.term.URIRef('http://example.org/kb/UcoObject-f494d239-d9fd-48da-bc07-461ba86d8c6c')
120+
>>> # Review a data graph that includes only the single individual, class typo'd (capitalized incorrectly), but property OK.
121+
>>> _ = dg.add((n_uco_object, NS_RDF.type, NS_UCO_CORE.UCOObject))
122+
>>> _ = dg.add((n_uco_object, NS_UCO_CORE.name, Literal("Test")))
123+
>>> _ = dg.add((n_uco_object, ns_ex.customProperty, Literal("Custom Value")))
124+
>>> invalid_cdo_concepts = get_invalid_cdo_concepts(dg, og)
125+
>>> invalid_cdo_concepts
126+
{rdflib.term.URIRef('https://ontology.unifiedcyberontology.org/uco/core/UCOObject')}
127+
>>> # Note that the property "ourCustomProperty" was typo'd in the data graph, but this was not reported.
128+
>>> assert ns_ex.ourCustomProperty not in invalid_cdo_concepts
133129
"""
134130
# Construct set of CDO concepts for data graph concept-existence review.
135131
cdo_concepts: Set[rdflib.URIRef] = set()
@@ -194,6 +190,35 @@ def get_invalid_cdo_concepts(
194190
return data_cdo_concepts - cdo_concepts
195191

196192

193+
def get_ontology_graph(
194+
case_version: Optional[str] = None, supplemental_graphs: Optional[List[str]] = None
195+
) -> rdflib.Graph:
196+
"""
197+
Get the ontology graph for the given case_version and any supplemental graphs.
198+
199+
:param case_version: the version of the CASE ontology to use. If None (i.e. null), the most recent version will be used. If "none" (the string), no pre-built version of CASE will be used.
200+
:param supplemental_graphs: a list of supplemental graphs to use. If None, no supplemental graphs will be used.
201+
:return: the ontology graph against which to validate the data graph.
202+
"""
203+
ontology_graph = rdflib.Graph()
204+
205+
if case_version != "none":
206+
# Load bundled CASE ontology at requested version.
207+
if case_version is None:
208+
case_version = CURRENT_CASE_VERSION
209+
ttl_filename = case_version + ".ttl"
210+
_logger.debug("ttl_filename = %r.", ttl_filename)
211+
ttl_data = importlib.resources.read_text(case_utils.ontology, ttl_filename)
212+
ontology_graph.parse(data=ttl_data, format="turtle")
213+
214+
if supplemental_graphs:
215+
for arg_ontology_graph in supplemental_graphs:
216+
_logger.debug("arg_ontology_graph = %r.", arg_ontology_graph)
217+
ontology_graph.parse(arg_ontology_graph)
218+
219+
return ontology_graph
220+
221+
197222
def validate(
198223
input_file: str,
199224
case_version: Optional[str] = None,

tests/Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ all: \
2323
.PHONY: \
2424
all-case_utils \
2525
check-case_utils \
26+
check-doctest \
2627
check-isomorphic_diff \
2728
check-mypy \
2829
download
@@ -57,13 +58,9 @@ all-case_utils: \
5758
# These check calls are provided in preferred run-order.
5859
check: \
5960
check-mypy \
61+
check-doctest \
6062
check-isomorphic_diff \
6163
check-case_utils
62-
source venv/bin/activate \
63-
&& pytest \
64-
--doctest-modules \
65-
--log-level=DEBUG \
66-
$(top_srcdir)/case_utils
6764
source venv/bin/activate \
6865
&& pytest \
6966
--ignore case_utils \
@@ -76,6 +73,14 @@ check-case_utils: \
7673
--directory case_utils \
7774
check
7875

76+
check-doctest: \
77+
.venv.done.log
78+
source venv/bin/activate \
79+
&& pytest \
80+
--doctest-modules \
81+
--log-level=DEBUG \
82+
$(top_srcdir)/case_utils
83+
7984
check-isomorphic_diff: \
8085
.venv.done.log
8186
$(MAKE) \

0 commit comments

Comments
 (0)