model_visitor.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from visitor import Visitor
  2. from compiler import main as do_compile
  3. import os
  4. def empty(s):
  5. return None
  6. class ModelVisitor(Visitor):
  7. def __init__(self, args):
  8. Visitor.__init__(self, args)
  9. self.constructors = []
  10. self.free_id = 0
  11. self.name_maps = {}
  12. self.current_model = None
  13. self.current_element = []
  14. self.includes = []
  15. def dump(self):
  16. return self.constructors
  17. def __getattr__(self, attr):
  18. if attr.startswith("visit_"):
  19. return empty
  20. else:
  21. raise AttributeError()
  22. def visit_start(self, tree):
  23. for t in tree.get_tail():
  24. self.visit(t)
  25. def visit_include_files(self, tree):
  26. self.includes.append(tree.get_children("STRVALUE")[0].get_text())
  27. def visit_import(self, tree):
  28. url = tree.get_children("MV_URL")[0]
  29. target = tree.get_children("MODEL_ID")[0]
  30. self.constructors.extend(["import_node", url.get_text(), target.get_text()])
  31. def visit_export(self, tree):
  32. url = tree.get_children("MV_URL")[0]
  33. target = tree.get_children("MODEL_ID")[0]
  34. self.constructors.extend(["export_node", target.get_text(), url.get_text()])
  35. def visit_model(self, tree):
  36. children = tree.get_children("MODEL_ID")
  37. model_type = children[0].get_text()
  38. model_name = children[-1].get_text()
  39. self.constructors.extend(["instantiate_model", model_type, model_name])
  40. print("CHECK for inheritance")
  41. if tree.get_children("LPAR"):
  42. print("ADD")
  43. self.constructors.extend(["define_inheritance", model_name, tree.get_children("MODEL_ID")[1].get_text()])
  44. else:
  45. print("NO!")
  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", self.current_model, element_type, element_name, source_name, target_name])
  62. else:
  63. self.constructors.extend(["instantiate_node", self.current_model, element_type, element_name])
  64. self.current_element.append(element_name)
  65. if tree.get_children("inheritance"):
  66. self.visit(tree.get_children("inheritance")[0])
  67. for attr in tree.get_children("model_attribute"):
  68. self.visit(attr)
  69. self.current_element.pop()
  70. return element_name
  71. def visit_inheritance(self, tree):
  72. for token in tree.get_children("MODEL_ID"):
  73. superclass = token.get_text()
  74. self.constructors.extend(["instantiate_link", self.current_model, "Inheritance", "%s_inherits_from_%s" % (self.current_element[-1], superclass), self.current_element[-1], superclass])
  75. def visit_model_attribute(self, tree):
  76. children = tree.get_children("MODEL_ID")
  77. is_definition = bool(tree.get_children("COLON"))
  78. is_constraint = bool(tree.get_children("DOLLAR"))
  79. is_assign = bool(tree.get_children("model_attr_instance"))
  80. is_nested = bool(tree.get_children("model_element"))
  81. if is_definition:
  82. attr_name = children[0].get_text()
  83. attr_type = children[1].get_text()
  84. self.constructors.extend(["instantiate_link", self.current_model, "Association", self.current_element[-1] + "_" + attr_name, self.current_element[-1], attr_type])
  85. full_attribute_name = self.current_element[-1] + "_" + attr_name
  86. self.constructors.extend(["instantiate_attribute", self.current_model, full_attribute_name, "name", attr_name])
  87. if is_assign:
  88. # There are also some attributes to set!
  89. self.current_element.append(full_attribute_name)
  90. for f in tree.get_children("model_attr_instance"):
  91. self.visit(f)
  92. self.current_element.pop()
  93. elif is_assign:
  94. self.visit(tree.get_children("model_attr_instance")[0])
  95. elif is_constraint:
  96. constraint = tree.get_children("ANYTHING_EXCEPT_DOLLAR")[0].get_text()
  97. whitespaces = len(constraint) - len(constraint.lstrip())
  98. constraint = "\n".join(["\t" + line[whitespaces-1:].replace(" ", "\t") for line in constraint.split("\n") if len(line.strip()) != 0])
  99. constraint = "".join(["include %s\n" % i for i in self.includes]) + \
  100. "String function constraint(model : Element, name : String):\n" + \
  101. "\tElement self\n" + \
  102. '\tself = model["model"][name]\n' + \
  103. constraint + "\n"
  104. with open(".constraint.alc", 'w') as f:
  105. f.write(constraint)
  106. f.flush()
  107. directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
  108. compiled = do_compile(".constraint.alc", directory + "/../grammars/actionlanguage.g", "CS")
  109. self.constructors.extend(["add_constraint", self.current_model, self.current_element[-1]] + compiled)
  110. elif is_nested:
  111. if tree.get_children("MODEL_ID"):
  112. contains_link = tree.get_children("MODEL_ID")[0].get_text()
  113. else:
  114. contains_link = ""
  115. entry = self.visit(tree.get_children("model_element")[0])
  116. self.constructors.extend(["instantiate_link", self.current_model, contains_link, "__%s" % self.free_id, self.current_element[-1], entry])
  117. self.free_id += 1
  118. def visit_model_attr_instance(self, tree):
  119. def constructors_compile(code):
  120. code_fragments = code.split("\n")
  121. code_fragments = [i for i in code_fragments if i.strip() != ""]
  122. code_fragments = [i.replace(" ", "\t") for i in code_fragments]
  123. initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
  124. code_fragments = [i[initial_tabs:] for i in code_fragments]
  125. code = "\n".join(code_fragments)
  126. code += "\n"
  127. with open(".code.alc", 'w') as f:
  128. f.write(code)
  129. f.flush()
  130. directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
  131. compiled = do_compile(".code.alc", directory + "/../grammars/actionlanguage.g", "CS")
  132. return compiled
  133. children = tree.get_children("MODEL_ID")
  134. attr_name = children[0].get_text()
  135. if tree.get_children("value"):
  136. # Value attribute
  137. attr_value = tree.get_children("value")[0].get_tail()[0]
  138. if attr_value.head == "STRVALUE":
  139. attr_value = attr_value.get_text()[1:-1]
  140. elif attr_value.head == "TRUE":
  141. attr_value = True
  142. elif attr_value.head == "FALSE":
  143. attr_value = False
  144. elif attr_value.head == "DEC_NUMBER":
  145. attr_value = int(attr_value.get_text())
  146. elif attr_value.head == "FLOAT_NUMBER":
  147. attr_value = float(attr_value.get_text())
  148. else:
  149. raise Exception(attr_value.head)
  150. self.constructors.extend(["instantiate_attribute", self.current_model, self.current_element[-1], attr_name, attr_value])
  151. elif tree.get_children("DOLLAR"):
  152. # Coded attribute
  153. self.constructors.extend(["instantiate_attribute_code", self.current_model, self.current_element[-1], attr_name])
  154. self.constructors.extend(constructors_compile(tree.get_children("ANYTHING_EXCEPT_DOLLAR")[0].get_text()))
  155. else:
  156. # Assign direct reference
  157. self.constructors.extend(["instantiate_attribute_ref", self.current_model, self.current_element[-1], attr_name, tree.get_children("MODEL_ID")[1].get_text()])