V1.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from uuid import UUID
  2. from state.base import State
  3. from services.bottom.V0 import Bottom as BottomV0
  4. class Bottom:
  5. def __init__(self, model: UUID, state: State):
  6. type_model_id = state.read_dict(state.read_root(), "Bottom")
  7. self.type_model = UUID(state.read_value(type_model_id))
  8. self.model = model
  9. self.bottom = BottomV0(state)
  10. def create_node(self, name: str, value=None):
  11. if value is None:
  12. n = self.bottom.create_node()
  13. else:
  14. n = self.bottom.create_node(value)
  15. self.bottom.create_edge(self.model, n, label=name)
  16. def create_edge(self, name: str, source: str, target: str):
  17. try:
  18. src, = self.bottom.read_outgoing_elements(self.model, source)
  19. except ValueError:
  20. raise RuntimeError(f"No element named {source}")
  21. try:
  22. tgt, = self.bottom.read_outgoing_elements(self.model, source)
  23. except ValueError:
  24. raise RuntimeError(f"No element named {target}")
  25. e = self.bottom.create_edge(src, tgt)
  26. self.bottom.create_edge(self.model, e, label=name)
  27. def read_value(self, name: str):
  28. try:
  29. element, = self.bottom.read_outgoing_elements(self.model, name)
  30. return self.bottom.read_value(element)
  31. except ValueError:
  32. raise RuntimeError(f"No element named {name}")
  33. def read_edge_source(self, name: str):
  34. try:
  35. element, = self.bottom.read_outgoing_elements(self.model, name)
  36. return self.bottom.read_value(element)
  37. except ValueError:
  38. raise RuntimeError(f"No element named {name}")
  39. def read_edge_target(self, name: str):
  40. pass
  41. def delete_element(self, name: str):
  42. try:
  43. element, = self.bottom.read_outgoing_elements(self.model, name)
  44. self.bottom.delete_element(element)
  45. except ValueError:
  46. raise RuntimeError(f"No element named {name}")
  47. def list_elements(self):
  48. tm_names = {}
  49. for key in self.bottom.read_keys(self.type_model):
  50. element, = self.bottom.read_outgoing_elements(self.type_model, key)
  51. tm_names[element] = key
  52. unsorted = []
  53. for key in self.bottom.read_keys(self.model):
  54. element, = self.bottom.read_outgoing_elements(self.model, key)
  55. element_types = self.bottom.read_outgoing_elements(element, "Morphism")
  56. type_model_elements = self.bottom.read_outgoing_elements(self.type_model)
  57. element_type_node, = [e for e in element_types if e in type_model_elements]
  58. unsorted.append(f"{key} : {tm_names[element_type_node]}")
  59. return sorted(unsorted)
  60. def to_bottom(self):
  61. pass # already encoded as bottom
  62. def from_bottom(self):
  63. pass # already encoded as bottom