ScheduledActionGenerator.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import importlib.util
  2. import io
  3. import os
  4. from jinja2 import FileSystemLoader, Environment
  5. from concrete_syntax.textual_od import parser as parser_od
  6. from concrete_syntax.textual_cd import parser as parser_cd
  7. from api.od import ODAPI
  8. from bootstrap.scd import bootstrap_scd
  9. from examples.schedule.generator import schedule_generator
  10. from examples.schedule.schedule_lib import End, NullNode
  11. from framework.conformance import Conformance, render_conformance_check_result
  12. from state.devstate import DevState
  13. class ScheduleActionGenerator:
  14. def __init__(self, rule_executer, schedulefile:str):
  15. self.rule_executer = rule_executer
  16. self.rule_dict = {}
  17. self.schedule: "Schedule"
  18. self.state = DevState()
  19. self.load_schedule(schedulefile)
  20. def load_schedule(self, filename):
  21. print("Loading schedule ...")
  22. scd_mmm = bootstrap_scd(self.state)
  23. with open("../schedule/models/scheduling_MM.od", "r") as f_MM:
  24. mm_cs = f_MM.read()
  25. with open(f"{filename}", "r") as f_M:
  26. m_cs = f_M.read()
  27. print("OK")
  28. print("\nParsing models")
  29. print(f"\tParsing meta model")
  30. scheduling_mm = parser_cd.parse_cd(
  31. self.state,
  32. m_text=mm_cs,
  33. )
  34. print(f"\tParsing '{filename}_M.od' model")
  35. scheduling_m = parser_od.parse_od(
  36. self.state,
  37. m_text=m_cs,
  38. mm=scheduling_mm
  39. )
  40. print(f"OK")
  41. print("\tmeta-meta-model a valid class diagram")
  42. conf = Conformance(self.state, scd_mmm, scd_mmm)
  43. print(render_conformance_check_result(conf.check_nominal()))
  44. print(f"Is our '{filename}_M.od' model a valid '{filename}_MM.od' diagram?")
  45. conf = Conformance(self.state, scheduling_m, scheduling_mm)
  46. print(render_conformance_check_result(conf.check_nominal()))
  47. print("OK")
  48. od = ODAPI(self.state, scheduling_m, scheduling_mm)
  49. g = schedule_generator(od)
  50. output_buffer = io.StringIO()
  51. g.generate_schedule(output_buffer)
  52. open(f"schedule.py", "w").write(output_buffer.getvalue())
  53. spec = importlib.util.spec_from_file_location("schedule", "schedule.py")
  54. scedule_module = importlib.util.module_from_spec(spec)
  55. spec.loader.exec_module(scedule_module)
  56. self.schedule = scedule_module.Schedule(self.rule_executer)
  57. self.load_matchers()
  58. def load_matchers(self):
  59. matchers = dict()
  60. for file in self.schedule.get_matchers():
  61. matchers[file] = self.rule_executer.load_match(file)
  62. self.schedule.init_schedule(matchers)
  63. def __call__(self, api: ODAPI):
  64. exec_op = self.schedule(api)
  65. yield from exec_op
  66. def termination_condition(self, api: ODAPI):
  67. if type(self.schedule.cur) == End:
  68. return "jay"
  69. if type(self.schedule.cur) == NullNode:
  70. return "RRRR"
  71. return None
  72. def generate_dot(self):
  73. env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
  74. env.trim_blocks = True
  75. env.lstrip_blocks = True
  76. template_dot = env.get_template('schedule_dot.j2')
  77. nodes = []
  78. edges = []
  79. visit = set()
  80. self.schedule.generate_dot(nodes, edges, visit)
  81. print("Nodes:")
  82. print(nodes)
  83. print("\nEdges:")
  84. print(edges)
  85. with open("test.dot", "w") as f_dot:
  86. f_dot.write(template_dot.render({"nodes": nodes, "edges": edges}))