model_visitor.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. self.constructors.append('"model"')
  24. for t in tree.get_tail():
  25. self.visit(t)
  26. self.constructors.append('"exit"')
  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"', jsonstr(url.get_text()), jsonstr(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"', jsonstr(target.get_text()), jsonstr(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"', jsonstr(model_type), jsonstr(model_name)])
  40. self.constructors.extend(['"define_inheritance"', jsonstr(model_name), jsonstr("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"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name), jsonstr(source_name), jsonstr(target_name)])
  57. else:
  58. self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
  59. self.current_element = element_name
  60. for attr in tree.get_children("model_attribute"):
  61. self.visit(attr)
  62. def visit_model_attribute(self, tree):
  63. children = tree.get_children("MODEL_ID")
  64. is_definition = bool(tree.get_children("COLON"))
  65. if is_definition:
  66. # is definition
  67. attr_name = children[0].get_text()
  68. attr_type = children[1].get_text()
  69. self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr("Association"), jsonstr(self.current_element + "_" + attr_name), jsonstr(self.current_element), jsonstr(attr_type)])
  70. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_model), jsonstr(self.current_element + "_" + attr_name), jsonstr("name"), jsonstr(attr_name)])
  71. else:
  72. # is assign
  73. attr_name = children[0].get_text()
  74. attr_value = tree.get_children("value")[0]
  75. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_model), jsonstr(self.current_element), jsonstr(attr_name), jsonstr(attr_value.get_text()) if attr_value.head == "STRVALUE" else attr_value.get_text()])