model_visitor.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from visitor import Visitor
  2. def jsonstr(s):
  3. return '"%s"' % s
  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. def dump(self):
  15. print(self.constructors)
  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_import(self, tree):
  26. url = tree.get_children("MV_URL")[0]
  27. target = tree.get_children("MODEL_ID")[0]
  28. self.constructors.extend(['"import_node"', jsonstr(url.get_text()), jsonstr(target.get_text())])
  29. def visit_model(self, tree):
  30. children = tree.get_children("MODEL_ID")
  31. model_type = children[0].get_text()
  32. model_name = children[1].get_text()
  33. self.constructors.extend(['"instantiate_model"', jsonstr(model_type), jsonstr(model_name)])
  34. self.current_model = model_name
  35. for element in tree.get_children("model_element"):
  36. self.visit(element)
  37. def visit_model_element(self, tree):
  38. children = tree.get_children("MODEL_ID")
  39. element_type = children[0].get_text()
  40. element_name = children[1].get_text()
  41. if len(children) == 4:
  42. source_name = children[2].get_text()
  43. target_name = children[3].get_text()
  44. self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name), jsonstr(source_name), jsonstr(target_name)])
  45. else:
  46. self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
  47. self.current_element = element_name
  48. for attr in tree.get_children("model_attribute"):
  49. self.visit(attr)
  50. def visit_model_attribute(self, tree):
  51. children = tree.get_children("MODEL_ID")
  52. is_definition = bool(tree.get_children("COLON"))
  53. if is_definition:
  54. attr_name = children[0].get_text()
  55. attr_type = children[1].get_text()
  56. self.constructors.extend(['"instantiate_link"', jsonstr("Association"), jsonstr(self.current_element + "_" + attr_name), jsonstr(self.current_element), jsonstr(attr_type)])
  57. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_element + "_" + attr_name), jsonstr("name"), jsonstr(attr_name)])
  58. else:
  59. attr_name = children[0].get_text()
  60. attr_value = children[1]
  61. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_element), jsonstr(attr_name), jsonstr(attr_value.get_text()) if attr_value.head == "STRVALUE" else attr_value.get_text()])