model_raw_visitor.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ModelRawVisitor(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_model(self, tree):
  28. children = tree.get_children("MODEL_ID")
  29. model_type = children[0].get_text()
  30. model_name = children[-1].get_text()
  31. self.current_model = model_name
  32. for element in tree.get_children("model_element"):
  33. self.visit(element)
  34. def visit_model_element(self, tree):
  35. children = tree.get_children("MODEL_ID")
  36. element_type = children[0].get_text()
  37. if len(children) == 2 or len(children) == 4:
  38. element_name = children[1].get_text()
  39. else:
  40. element_name = "__%s" % self.free_id
  41. self.free_id += 1
  42. if len(children) > 2:
  43. # So we have a source and target; but aren't sure which is which, because the name is optional!
  44. source_name = children[-2].get_text()
  45. target_name = children[-1].get_text()
  46. self.constructors.extend(["instantiate_link", element_type, element_name, source_name, target_name])
  47. else:
  48. self.constructors.extend(["instantiate_node", element_type, element_name])
  49. self.current_element.append(element_name)
  50. if tree.get_children("inheritance"):
  51. self.visit(tree.get_children("inheritance")[0])
  52. for attr in tree.get_children("model_attribute"):
  53. self.visit(attr)
  54. self.current_element.pop()
  55. return element_name
  56. def visit_inheritance(self, tree):
  57. for token in tree.get_children("MODEL_ID"):
  58. superclass = token.get_text()
  59. self.constructors.extend(["instantiate_link", "Inheritance", "%s_inherits_from_%s" % (self.current_element[-1], superclass), self.current_element[-1], superclass])
  60. def visit_model_attribute(self, tree):
  61. children = tree.get_children("MODEL_ID")
  62. is_definition = bool(tree.get_children("COLON"))
  63. is_constraint = bool(tree.get_children("DOLLAR"))
  64. is_assign = bool(tree.get_children("model_attr_instance"))
  65. is_nested = bool(tree.get_children("model_element"))
  66. if is_definition:
  67. attr_name = children[0].get_text()
  68. attr_type = children[1].get_text()
  69. self.constructors.extend(["instantiate_link", "Association", self.current_element[-1] + "_" + attr_name, self.current_element[-1], attr_type])
  70. full_attribute_name = self.current_element[-1] + "_" + attr_name
  71. self.constructors.extend(["instantiate_attribute", full_attribute_name, "name", attr_name])
  72. if is_assign:
  73. # There are also some attributes to set!
  74. self.current_element.append(full_attribute_name)
  75. for f in tree.get_children("model_attr_instance"):
  76. self.visit(f)
  77. self.current_element.pop()
  78. elif is_assign:
  79. self.visit(tree.get_children("model_attr_instance")[0])
  80. elif is_constraint:
  81. constraint = tree.get_children("ANYTHING_EXCEPT_DOLLAR")[0].get_text()
  82. whitespaces = len(constraint) - len(constraint.lstrip())
  83. constraint = "\n".join(["\t" + line[whitespaces-1:].replace(" ", "\t") for line in constraint.split("\n") if len(line.strip()) != 0])
  84. constraint = "".join(["include %s\n" % i for i in self.includes]) + \
  85. "String function constraint(model : Element, name : String):\n" + \
  86. "\tElement self\n" + \
  87. '\tself = model["model"][name]\n' + \
  88. constraint + "\n"
  89. with open(".constraint.alc", 'w') as f:
  90. f.write(constraint)
  91. f.flush()
  92. directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
  93. compiled = do_compile(".constraint.alc", directory + "/../grammars/actionlanguage.g", "CS")
  94. self.constructors.extend(["add_constraint", self.current_element[-1]] + compiled)
  95. elif is_nested:
  96. if tree.get_children("MODEL_ID"):
  97. contains_link = tree.get_children("MODEL_ID")[0].get_text()
  98. else:
  99. contains_link = ""
  100. entry = self.visit(tree.get_children("model_element")[0])
  101. self.constructors.extend(["instantiate_link", contains_link, "__%s" % self.free_id, self.current_element[-1], entry])
  102. self.free_id += 1
  103. def visit_model_attr_instance(self, tree):
  104. def constructors_compile(code):
  105. code_fragments = code.split("\n")
  106. code_fragments = [i for i in code_fragments if i.strip() != ""]
  107. code_fragments = [i.replace(" ", "\t") for i in code_fragments]
  108. initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
  109. code_fragments = [i[initial_tabs:] for i in code_fragments]
  110. code = "\n".join(code_fragments)
  111. code += "\n"
  112. with open(".code.alc", 'w') as f:
  113. f.write(code)
  114. f.flush()
  115. directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
  116. compiled = do_compile(".code.alc", directory + "/../grammars/actionlanguage.g", "CS")
  117. return compiled
  118. children = tree.get_children("MODEL_ID")
  119. attr_name = children[0].get_text()
  120. if tree.get_children("value"):
  121. # Value attribute
  122. attr_value = tree.get_children("value")[0].get_tail()[0]
  123. if attr_value.head == "STRVALUE":
  124. attr_value = attr_value.get_text()[1:-1]
  125. elif attr_value.head == "TRUE":
  126. attr_value = True
  127. elif attr_value.head == "FALSE":
  128. attr_value = False
  129. elif attr_value.head == "DEC_NUMBER":
  130. attr_value = int(attr_value.get_text())
  131. elif attr_value.head == "FLOAT_NUMBER":
  132. attr_value = float(attr_value.get_text())
  133. else:
  134. raise Exception(attr_value.head)
  135. self.constructors.extend(["instantiate_attribute", self.current_element[-1], attr_name, attr_value])
  136. elif tree.get_children("DOLLAR"):
  137. # Coded attribute
  138. self.constructors.extend(["instantiate_attribute_code", self.current_element[-1], attr_name])
  139. self.constructors.extend(constructors_compile(tree.get_children("ANYTHING_EXCEPT_DOLLAR")[0].get_text()))