rule_executor.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from typing import Any
  2. from uuid import UUID
  3. from api.od import ODAPI
  4. from transformation.matcher import match_od
  5. from transformation.rewriter import rewrite
  6. from util.loader import parse_and_check
  7. class RuleExecutor:
  8. def __init__(self, state, mm: UUID, mm_ramified: UUID, eval_context={}):
  9. self.state = state
  10. self.mm = mm
  11. self.mm_ramified = mm_ramified
  12. self.eval_context = eval_context
  13. # Generates matches.
  14. # Every match is a dictionary with entries LHS_element_name -> model_element_name
  15. def match_rule(self, m: UUID, lhs: UUID, *, pivot: dict[Any, Any]):
  16. lhs_matcher = match_od(
  17. self.state,
  18. host_m=m,
  19. host_mm=self.mm,
  20. pattern_m=lhs,
  21. pattern_mm=self.mm_ramified,
  22. eval_context=self.eval_context,
  23. pivot=pivot,
  24. )
  25. return lhs_matcher
  26. def rewrite_rule(self, od: ODAPI, rhs: UUID, *, pivot: dict[Any, Any]):
  27. rhs = rewrite(
  28. self.state,
  29. rhs_m=rhs,
  30. pattern_mm=self.mm_ramified,
  31. lhs_match=pivot,
  32. host_m=od.m,
  33. host_mm=od.mm,
  34. eval_context=self.eval_context,
  35. )
  36. od.recompute_mappings()
  37. yield rhs
  38. def load_match(self, file: str):
  39. with open(file, "r") as f:
  40. return parse_and_check(self.state, f.read(), self.mm_ramified, file)