Skip to content

Commit

Permalink
build templates off of imports closure
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed May 22, 2024
1 parent fb45770 commit f83ce19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
19 changes: 18 additions & 1 deletion buildingmotif/dataclasses/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from buildingmotif.schemas import validate_libraries_yaml
from buildingmotif.template_compilation import compile_template_spec
from buildingmotif.utils import (
copy_graph,
get_ontology_files,
get_template_parts_from_shape,
shacl_inference,
Expand Down Expand Up @@ -299,6 +300,20 @@ def _infer_templates_from_graph(self, graph: rdflib.Graph):
:param graph: graph to infer templates from
:type graph: rdflib.Graph
"""
# add all imports to the same graph so we can resolve everything
imports_closure = copy_graph(graph)
# import dependencies into 'graph'
# get all imports from the graph
for dependency in graph.objects(predicate=rdflib.OWL.imports):
# attempt to load from BuildingMOTIF
try:
lib = Library.load(name=str(dependency))
imports_closure += lib.get_shape_collection().graph
except Exception as e: # TODO: replace with a more specific exception
logging.warning(
f"An ontology could not resolve a dependency on {dependency} ({e}). Check this is loaded into BuildingMOTIF"
)
continue
class_candidates = set(graph.subjects(rdflib.RDF.type, rdflib.OWL.Class))
shape_candidates = set(graph.subjects(rdflib.RDF.type, rdflib.SH.NodeShape))
candidates = class_candidates.intersection(shape_candidates)
Expand All @@ -307,7 +322,9 @@ def _infer_templates_from_graph(self, graph: rdflib.Graph):
for candidate in candidates:
assert isinstance(candidate, rdflib.URIRef)
# TODO: mincount 0 (or unspecified) should be optional args on the generated template
partial_body, deps = get_template_parts_from_shape(candidate, graph)
partial_body, deps = get_template_parts_from_shape(
candidate, imports_closure
)
templ = self.create_template(str(candidate), partial_body)
dependency_cache[templ.id] = deps
template_id_lookup[str(candidate)] = templ.id
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_library_validity.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def test_brick_template(bm, library_path_brick, template_brick, shacl_engine):
Library.load(
ontology_graph=dep, infer_templates=False, run_shacl_inference=False
)
ont_brick = Library.load(ontology_graph="libraries/brick/Brick.ttl")
ont_brick = Library.load(
ontology_graph="libraries/brick/Brick.ttl", run_shacl_inference=False
)

lib = Library.load(directory=str(library_path_brick))

Expand All @@ -143,7 +145,5 @@ def test_brick_template(bm, library_path_brick, template_brick, shacl_engine):
assert isinstance(g, Graph), "was not a graph"
bind_prefixes(g)
m.add_graph(g)
m.graph.serialize("/tmp/model.ttl")
ctx = m.validate([ont_brick.get_shape_collection()], error_on_missing_imports=False)
ctx.report.serialize("/tmp/report.ttl")
assert ctx.valid, ctx.report_string

0 comments on commit f83ce19

Please sign in to comment.