base.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from abc import ABC, abstractmethod
  2. from state.base import State
  3. from core.element import Element, String
  4. class Context(ABC):
  5. def __init__(self, state: State, model: Element, metamodel: Element):
  6. self.state = state
  7. self.model = model
  8. self.metamodel = metamodel
  9. @abstractmethod
  10. def __enter__(self):
  11. pass
  12. @abstractmethod
  13. def __exit__(self):
  14. pass
  15. @abstractmethod
  16. def instantiate(self, type_name: String, instance_name: String):
  17. pass
  18. @abstractmethod
  19. def instantiate_value(self, type_name: String, instance_name: String, value: Element):
  20. pass
  21. @abstractmethod
  22. def instantiate_link(self, type_name: String, name: String, source: String, target: String):
  23. pass
  24. @abstractmethod
  25. def delete_element(self, name: String):
  26. pass
  27. @abstractmethod
  28. def verify(self):
  29. pass
  30. @abstractmethod
  31. def list_elements(self):
  32. pass
  33. def list_types(self):
  34. # can be implemented here since we assume that metamodel
  35. # is always in graph fo, i.e. in the MV-state graph.
  36. unsorted = []
  37. model_root = self.state.read_dict(self.metamodel.id, "Model")
  38. for elem_edge in self.state.read_outgoing(model_root):
  39. # get element name
  40. label_edge, = self.state.read_outgoing(elem_edge)
  41. _, label_node = self.state.read_edge(label_edge)
  42. label = self.state.read_value(label_node)
  43. # find element type
  44. elem_type_node = self.state.read_dict(label_node, "Type")
  45. elem_type = self.state.read_value(elem_type_node)
  46. unsorted.append(f"{label} : {elem_type if elem_type is not None else '_'}")
  47. for i in sorted(unsorted):
  48. print(i)