model_visitor.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. element_name = children[1].get_text()
  48. if len(children) == 4:
  49. source_name = children[2].get_text()
  50. target_name = children[3].get_text()
  51. self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name), jsonstr(source_name), jsonstr(target_name)])
  52. else:
  53. self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
  54. self.current_element = element_name
  55. for attr in tree.get_children("model_attribute"):
  56. self.visit(attr)
  57. def visit_model_attribute(self, tree):
  58. children = tree.get_children("MODEL_ID")
  59. is_definition = bool(tree.get_children("COLON"))
  60. if is_definition:
  61. # is definition
  62. attr_name = children[0].get_text()
  63. attr_type = children[1].get_text()
  64. self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr("Association"), jsonstr(self.current_element + "_" + attr_name), jsonstr(self.current_element), jsonstr(attr_type)])
  65. self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_model), jsonstr(self.current_element + "_" + attr_name), jsonstr("name"), jsonstr(attr_name)])
  66. else:
  67. # is assign
  68. attr_name = children[0].get_text()
  69. attr_value = children[1]
  70. 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()])