rulebased_sem.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ### Operational Semantics - defined by rule-based model transformation ###
  2. from concrete_syntax.textual_od.parser import parse_od
  3. from transformation.rule import Rule, RuleMatcherRewriter, PriorityActionGenerator
  4. from transformation.matcher import match_od
  5. from util import loader
  6. import os
  7. THIS_DIR = os.path.dirname(__file__)
  8. # kind: lhs, rhs, nac
  9. get_filename = lambda rule_name, kind: f"{THIS_DIR}/rules/r_{rule_name}_{kind}.od"
  10. def get_action_generator(state, rt_mm, rt_mm_ramified):
  11. matcher_rewriter = RuleMatcherRewriter(state, rt_mm, rt_mm_ramified)
  12. #############################################################################
  13. # TO IMPLEMENT: Full semantics as a set of rule-based model transformations #
  14. rules0_dict = loader.load_rules(state, get_filename, rt_mm_ramified,
  15. ["ship_sinks"] # <- list of rule_name of equal priority
  16. )
  17. rules1_dict = loader.load_rules(state, get_filename, rt_mm_ramified,
  18. ["ship_appears_in_berth"]
  19. )
  20. # rules2_dict = ...
  21. generator = PriorityActionGenerator(matcher_rewriter, [
  22. rules0_dict, # highest priority
  23. rules1_dict, # lower priority
  24. # rules2_dict, # lowest priority
  25. ])
  26. # TO IMPLEMENT: Full semantics as a set of rule-based model transformations #
  27. #############################################################################
  28. return generator
  29. # The termination condition can also be specified as a pattern:
  30. class TerminationCondition:
  31. def __init__(self, state, rt_mm_ramified):
  32. self.state = state
  33. self.rt_mm_ramified = rt_mm_ramified
  34. # TO IMPLEMENT: terminate simulation when the place 'served' contains 2 ships.
  35. ########################################
  36. # You should only edit the pattern below
  37. pattern_cs = """
  38. # Placeholder to make the termination condition never hold:
  39. :GlobalCondition {
  40. condition = `False`;
  41. }
  42. """
  43. # You should only edit the pattern above
  44. ########################################
  45. self.pattern = parse_od(state, pattern_cs, rt_mm_ramified)
  46. def __call__(self, od):
  47. for match in match_od(self.state, od.m, od.mm, self.pattern, self.rt_mm_ramified):
  48. # stop after the first match (no need to look for more matches):
  49. return "There are 2 ships served." # Termination condition statisfied