Skip to content

Commit

Permalink
Merge pull request #383 from nanglo123/speed_up_model_composition
Browse files Browse the repository at this point in the history
Speed up model composition by referring a singleton refinement closure
  • Loading branch information
bgyori authored Oct 4, 2024
2 parents 5335739 + 6e8ec45 commit 69f2718
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
29 changes: 24 additions & 5 deletions mira/metamodel/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

__all__ = ["ModelComparisonGraphdata", "TemplateModelComparison",
"TemplateModelDelta", "RefinementClosure",
"get_dkg_refinement_closure"]
"get_dkg_refinement_closure", "default_dkg_refinement_closure"]

from collections import defaultdict
from itertools import combinations, count, product
Expand Down Expand Up @@ -788,6 +788,28 @@ def is_ontological_child(self, child_curie: str, parent_curie: str) -> bool:
return (child_curie, parent_curie) in self.transitive_closure


class DefaultDkgRefinementClosure(RefinementClosure):
def __init__(self, transitive_closure: Set[Tuple[str, str]] = None):
self.transitive_closure = transitive_closure
if self.transitive_closure:
self.initialized = True
else:
self.initialized = False

def initialize(self):
from mira.dkg.web_client import get_transitive_closure_web
self.transitive_closure = get_transitive_closure_web()
self.initialized = True

def is_ontological_child(self, child_curie: str, parent_curie: str) -> bool:
if not self.initialized:
self.initialize()
return (child_curie, parent_curie) in self.transitive_closure


default_dkg_refinement_closure = DefaultDkgRefinementClosure()


def get_dkg_refinement_closure() -> RefinementClosure:
"""Return a refinement closure from the DKG
Expand All @@ -796,7 +818,4 @@ def get_dkg_refinement_closure() -> RefinementClosure:
:
The refinement closure
"""
# Import here to avoid dependency upon module import
from mira.dkg.web_client import get_transitive_closure_web
rc = RefinementClosure(get_transitive_closure_web())
return rc
return default_dkg_refinement_closure
4 changes: 2 additions & 2 deletions mira/metamodel/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from copy import deepcopy
import sympy

from .comparison import TemplateModelComparison, get_dkg_refinement_closure
from .comparison import TemplateModelComparison, default_dkg_refinement_closure
from .template_model import Annotations, TemplateModel


Expand Down Expand Up @@ -59,7 +59,7 @@ def compose_two_models(tm0, tm1):
The composed template model
"""
model_list = [tm0, tm1]
rf_func = get_dkg_refinement_closure().is_ontological_child
rf_func = default_dkg_refinement_closure.is_ontological_child
compare = TemplateModelComparison(model_list,
refinement_func=rf_func)
compare_graph = compare.model_comparison
Expand Down

0 comments on commit 69f2718

Please sign in to comment.