runner.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import functools
  2. import pprint
  3. from state.devstate import DevState
  4. from bootstrap.scd import bootstrap_scd
  5. from api.od import ODAPI
  6. from concrete_syntax.common import indent
  7. from concrete_syntax.textual_od import renderer as od_renderer
  8. from concrete_syntax.plantuml import renderer as plantuml
  9. from concrete_syntax.plantuml.make_url import make_url as make_plantuml_url
  10. from concrete_syntax.graphviz.make_url import make_url as make_graphviz_url
  11. from concrete_syntax.graphviz import renderer as graphviz
  12. from transformation.matcher.mvs_adapter import match_od
  13. from transformation.rewriter import rewrite
  14. from transformation.cloner import clone_od
  15. from examples.semantics.operational import simulator
  16. import models
  17. def match_rule(rule_name, od: ODAPI, lhs, nac):
  18. lhs_matcher = match_od(state,
  19. host_m=od.m,
  20. host_mm=od.mm,
  21. pattern_m=lhs,
  22. pattern_mm=mm_rt_ram)
  23. try:
  24. for i, lhs_match in enumerate(lhs_matcher):
  25. nac_matcher = match_od(state,
  26. host_m=od.m,
  27. host_mm=od.mm,
  28. pattern_m=nac,
  29. pattern_mm=mm_rt_ram,
  30. pivot=lhs_match)
  31. try:
  32. for j, nac_match in enumerate(nac_matcher):
  33. break # there may be more NAC-matches, but we already now enough -> proceed to next lhs_match
  34. else:
  35. yield lhs_match # got match
  36. except Exception as e:
  37. # Make exceptions raised in eval'ed code easier to trace:
  38. e.add_note(f"while matching NAC of '{rule_name}'")
  39. raise
  40. except Exception as e:
  41. # Make exceptions raised in eval'ed code easier to trace:
  42. e.add_note(f"while matching LHS of '{rule_name}'")
  43. raise
  44. def exec_action(rule_name, od: ODAPI, lhs, rhs, lhs_match):
  45. # copy these, will be overwritten in-place
  46. cloned_m = clone_od(state, od.m, od.mm)
  47. rhs_match = dict(lhs_match)
  48. try:
  49. rewrite(state,
  50. lhs_m=lhs,
  51. rhs_m=rhs,
  52. pattern_mm=mm_rt_ram,
  53. lhs_name_mapping=rhs_match,
  54. host_m=cloned_m,
  55. host_mm=od.mm)
  56. except Exception as e:
  57. # Make exceptions raised in eval'ed code easier to trace:
  58. e.add_note(f"while executing RHS of '{rule_name}'")
  59. raise
  60. print("Updated match:\n" + indent(pp.pformat(rhs_match), 6))
  61. return (ODAPI(state, cloned_m, od.mm), [f"executed rule '{rule_name}'"])
  62. pp = pprint.PrettyPrinter(depth=4)
  63. def attempt_rules(od: ODAPI, rule_dict):
  64. at_least_one_match = False
  65. for rule_name, rule in rule_dict.items():
  66. for lhs_match in match_rule(rule_name, od, rule["lhs"], rule["nac"]):
  67. # We got a match!
  68. yield (rule_name + '\n' + indent(pp.pformat(lhs_match), 6),
  69. functools.partial(exec_action,
  70. rule_name, od, rule["lhs"], rule["rhs"], lhs_match))
  71. at_least_one_match = True
  72. return at_least_one_match
  73. def get_actions(od: ODAPI):
  74. # transformation schedule
  75. rule_advance_time = rules["advance_time"]
  76. rules_not_advancing_time = { rule_name: rule for rule_name, rule in rules.items() if rule_name != "advance_time" }
  77. at_least_one_match = yield from attempt_rules(od, rules_not_advancing_time)
  78. if not at_least_one_match:
  79. yield from attempt_rules(od, {"advance_time": rule_advance_time})
  80. state = DevState()
  81. scd_mmm = bootstrap_scd(state)
  82. print("Parsing models...")
  83. mm, mm_rt, m, m_rt_initial = models.get_fibonacci(state, scd_mmm)
  84. mm_rt_ram, rules = models.get_rules(state, mm_rt)
  85. # print("RT-MM")
  86. # print(make_plantuml_url(plantuml.render_class_diagram(state, mm_rt)))
  87. # print("RAMIFIED RT-MM")
  88. # print(make_plantuml_url(plantuml.render_class_diagram(state, mm_rt_ram)))
  89. sim = simulator.Simulator(
  90. action_generator=get_actions,
  91. # decision_maker=simulator.InteractiveDecisionMaker(auto_proceed=False),
  92. decision_maker=simulator.RandomDecisionMaker(seed=0),
  93. termination_condition=lambda od: "Time is up" if od.get_slot_value(od.get_all_instances("Clock")[0][1], "time") >= 10 else None,
  94. check_conformance=True,
  95. verbose=True,
  96. renderer=lambda od: od_renderer.render_od(state, od.m, od.mm, hide_names=False),
  97. )
  98. sim.run(ODAPI(state, m_rt_initial, mm_rt))