model_visitor.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from visitor import Visitor
  2. from compiler import main as do_compile
  3. import os
  4. def jsonstr(s):
  5. return '"%s"' % s
  6. def empty(s):
  7. return None
  8. class ModelVisitor(Visitor):
  9. def __init__(self, args):
  10. Visitor.__init__(self, args)
  11. self.constructors = []
  12. self.free_id = 0
  13. self.name_maps = {}
  14. self.current_model = None
  15. self.current_element = None
  16. self.includes = []
  17. def dump(self):
  18. print(self.constructors)
  19. return self.constructors
  20. def __getattr__(self, attr):
  21. if attr.startswith("visit_"):
  22. return empty
  23. else:
  24. raise AttributeError()
  25. def visit_start(self, tree):
  26. self.constructors.append('"model"')
  27. for t in tree.get_tail():
  28. self.visit(t)
  29. self.constructors.append('"exit"')
  30. def visit_include_files(self, tree):
  31. self.includes.append(tree.get_children("STRVALUE")[0].get_text())
  32. def visit_import(self, tree):
  33. url = tree.get_children("MV_URL")[0]
  34. target = tree.get_children("MODEL_ID")[0]
  35. self.constructors.extend(['"import_node"', jsonstr(url.get_text()), jsonstr(target.get_text())])
  36. def visit_export(self, tree):
  37. url = tree.get_children("MV_URL")[0]
  38. target = tree.get_children("MODEL_ID")[0]
  39. self.constructors.extend(['"export_node"', jsonstr(target.get_text()), jsonstr(url.get_text())])
  40. def visit_model(self, tree):
  41. children = tree.get_children("MODEL_ID")
  42. model_type = children[0].get_text()
  43. model_name = children[1].get_text()
  44. self.constructors.extend(['"instantiate_model"', jsonstr(model_type), jsonstr(model_name)])
  45. self.constructors.extend(['"define_inheritance"', jsonstr(model_name), jsonstr("Inheritance")])
  46. self.current_model = model_name
  47. for element in tree.get_children("model_element"):
  48. self.visit(element)
  49. def visit_model_element(self, tree):
  50. children = tree.get_children("MODEL_ID")
  51. element_type = children[0].get_text()
  52. if len(children) == 2 or len(children) == 4:
  53. element_name = children[1].get_text()
  54. else:
  55. element_name = "__%s" % self.free_id
  56. self.free_id += 1
  57. if len(children) > 2:
  58. # So we have a source and target; but aren't sure which is which, because the name is optional!
  59. source_name = children[-2].get_text()
  60. target_name = children[-1].get_text()
  61. self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name), jsonstr(source_name), jsonstr(target_name)])
  62. else:
  63. self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
  64. self.current_element = element_name
  65. for attr in tree.get_children("model_attribute"):
  66. self.visit(attr)
  67. def visit_model_attribute(self, tree):
  68. children = tree.get_children("MODEL_ID")
  69. is_definition = bool(tree.get_children("COLON"))
  70. is_constraint = bool(tree.get_children("DOLLAR"))
  71. is_assign = bool(tree.get_children("ASSIGN"))
  72. if is_definition:
  73. attr_name = children[0].get_text()
  74. attr_type = children[1].get_text()
  75. self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr("Association"), jsonstr(self.current_element + "_" + attr_name), jsonstr(self.current_element), jsonstr(attr_type)])
  76. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_model), jsonstr(self.current_element + "_" + attr_name), jsonstr("name"), jsonstr(attr_name)])
  77. elif is_assign:
  78. attr_name = children[0].get_text()
  79. attr_value = tree.get_children("value")[0]
  80. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_model), jsonstr(self.current_element), jsonstr(attr_name), jsonstr(attr_value.get_text()) if attr_value.head == "STRVALUE" else attr_value.get_text()])
  81. elif is_constraint:
  82. constraint = tree.get_children("ANYTHING_EXCEPT_DOLLAR")[0].get_text()
  83. whitespaces = len(constraint) - len(constraint.lstrip())
  84. constraint = "\n".join(["\t" + line[whitespaces-1:].replace(" ", "\t") for line in constraint.split("\n") if len(line.strip()) != 0])
  85. constraint = "".join(["include %s\n" % i for i in self.includes]) + \
  86. "String function constraint(model : Element, element : String):\n" + \
  87. "\tElement self\n" + \
  88. '\tself = model["model"][element]\n' + \
  89. constraint + "\n"
  90. with open(".constraint.alc", 'w') as f:
  91. f.write(constraint)
  92. f.flush()
  93. directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
  94. self.constructors.extend(['"add_constraint"', jsonstr(self.current_model), jsonstr(self.current_element)] + do_compile(".constraint.alc", directory + "/../grammars/actionlanguage.g", "CS"))