model_visitor.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_export(self, tree):
  30. url = tree.get_children("MV_URL")[0]
  31. target = tree.get_children("MODEL_ID")[0]
  32. self.constructors.extend(['"export_node"', jsonstr(target.get_text()), jsonstr(url.get_text())])
  33. def visit_model(self, tree):
  34. children = tree.get_children("MODEL_ID")
  35. model_type = children[0].get_text()
  36. model_name = children[1].get_text()
  37. self.constructors.extend(['"instantiate_model"', jsonstr(model_type), jsonstr(model_name)])
  38. self.constructors.extend(['"define_inheritance"', jsonstr(model_name), jsonstr("Inheritance")])
  39. self.current_model = model_name
  40. for element in tree.get_children("model_element"):
  41. self.visit(element)
  42. def visit_model_element(self, tree):
  43. children = tree.get_children("MODEL_ID")
  44. element_type = children[0].get_text()
  45. element_name = children[1].get_text()
  46. if len(children) == 4:
  47. source_name = children[2].get_text()
  48. target_name = children[3].get_text()
  49. self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name), jsonstr(source_name), jsonstr(target_name)])
  50. else:
  51. self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
  52. self.current_element = element_name
  53. for attr in tree.get_children("model_attribute"):
  54. self.visit(attr)
  55. def visit_model_attribute(self, tree):
  56. children = tree.get_children("MODEL_ID")
  57. is_definition = bool(tree.get_children("COLON"))
  58. if is_definition:
  59. attr_name = children[0].get_text()
  60. attr_type = children[1].get_text()
  61. self.constructors.extend(['"instantiate_link"', jsonstr("Association"), jsonstr(self.current_element + "_" + attr_name), jsonstr(self.current_element), jsonstr(attr_type)])
  62. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_element + "_" + attr_name), jsonstr("name"), jsonstr(attr_name)])
  63. else:
  64. attr_name = children[0].get_text()
  65. attr_value = children[1]
  66. 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()])