loader.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os.path
  2. from framework.conformance import Conformance, render_conformance_check_result
  3. from concrete_syntax.textual_od import parser
  4. from concrete_syntax.common import indent
  5. from transformation.rule import Rule
  6. # parse model and check conformance
  7. def parse_and_check(state, m_cs, mm, descr: str, check_conformance=True, type_transform=lambda type_name: type_name, name_generator=parser.DefaultNameGenerator()):
  8. try:
  9. m = parser.parse_od(
  10. state,
  11. m_text=m_cs,
  12. mm=mm,
  13. type_transform=type_transform,
  14. name_generator=name_generator,
  15. )
  16. except Exception as e:
  17. e.add_note("While parsing model " + descr)
  18. raise
  19. try:
  20. if check_conformance:
  21. conf = Conformance(state, m, mm)
  22. errors = conf.check_nominal()
  23. if len(errors) > 0:
  24. print(render_conformance_check_result(errors))
  25. print(" model: " + descr)
  26. except Exception as e:
  27. e.add_note("In model " + descr)
  28. raise
  29. return m
  30. # get file contents as string
  31. def read_file(filename):
  32. with open(filename) as file:
  33. return file.read()
  34. KINDS = ["nac", "lhs", "rhs"]
  35. # Phony name generator that raises an error if you try to use it :)
  36. class LHSNameGenerator:
  37. def __call__(self, type_name):
  38. raise Exception(f"Error: Object or link of type '{type_name}' does not have a name.\nAnonymous objects/links are not allowed in the LHS of a rule, because they can have unintended consequences. Please give all of the elements in the LHS explicit names.")
  39. # load model transformation rules
  40. def load_rules(state, get_filename, rt_mm_ramified, rule_names, check_conformance=True):
  41. rules = {}
  42. files_read = []
  43. for rule_name in rule_names:
  44. rule = {}
  45. def parse(kind):
  46. filename = get_filename(rule_name, kind)
  47. descr = "'"+filename+"'"
  48. if kind == "nac":
  49. suffix = ""
  50. nacs = []
  51. try:
  52. while True:
  53. base, ext = os.path.splitext(filename)
  54. processed_filename = base+suffix+ext
  55. nac = parse_and_check(state, read_file(processed_filename), rt_mm_ramified, descr, check_conformance)
  56. nacs.append(nac)
  57. suffix = "2" if suffix == "" else str(int(suffix)+1)
  58. files_read.append(processed_filename)
  59. except FileNotFoundError:
  60. if suffix == "":
  61. print(f"Warning: rule {rule_name} has no NAC ({filename} not found)")
  62. return nacs
  63. else:
  64. try:
  65. if kind == "lhs":
  66. m = parse_and_check(state, read_file(filename), rt_mm_ramified, descr, check_conformance, name_generator=LHSNameGenerator())
  67. elif kind == "rhs":
  68. m = parse_and_check(state, read_file(filename), rt_mm_ramified, descr, check_conformance)
  69. files_read.append(filename)
  70. return m
  71. except FileNotFoundError as e:
  72. print(f"Warning: using empty {kind} ({filename} not found)")
  73. # Use empty model as fill-in:
  74. return parse_and_check(
  75. state,
  76. "",
  77. rt_mm_ramified,
  78. descr="'"+filename+"'",
  79. check_conformance=check_conformance)
  80. rules[rule_name] = Rule(*(parse(kind) for kind in KINDS))
  81. print("Rules loaded:\n" + indent('\n'.join(files_read), 4))
  82. return rules