model_visitor.py 6.0 KB

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