cartesian.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from uuid import UUID
  2. from state.base import State
  3. from services.bottom.V0 import Bottom
  4. from services.primitives.float_type import Float
  5. class PointCartesian:
  6. def __init__(self, model: UUID, state: State):
  7. type_model_id = state.read_dict(state.read_root(), "PointCartesian")
  8. self.type_model = UUID(state.read_value(type_model_id))
  9. self.model = model
  10. self.state = state
  11. self.point = None
  12. def create_point(self, x: float, y: float):
  13. if self.point is None:
  14. self.point = (x, y)
  15. else:
  16. raise RuntimeError("A PointCartesian model can contain at most 1 point.")
  17. def read_point(self):
  18. if self.point is None:
  19. raise RuntimeError("No point found in model.")
  20. else:
  21. return f"(X = {self.point[0]}, Y = {self.point[1]})"
  22. def delete_point(self):
  23. self.point = None
  24. def apply_movement(self, delta_x: float, delta_y: float):
  25. if self.point is not None:
  26. self.point = (self.point[0] + delta_x, self.point[1] + delta_y)
  27. else:
  28. raise RuntimeError("No point found in model.")
  29. def to_bottom(self):
  30. bottom = Bottom(self.state)
  31. # clear residual model
  32. for element in bottom.read_outgoing_elements(self.model):
  33. bottom.delete_element(element)
  34. # create primitive models
  35. c1_model = bottom.create_node()
  36. c2_model = bottom.create_node()
  37. Float(c1_model, self.state).create(self.point[0])
  38. Float(c2_model, self.state).create(self.point[1])
  39. # instantiate Point class
  40. point_node = bottom.create_node() # create point node
  41. bottom.create_edge(self.model, point_node, "point") # attach to model
  42. morph_node, = bottom.read_outgoing_elements(self.type_model, "PointCartesian") # retrieve type
  43. bottom.create_edge(point_node, morph_node, "Morphism") # create morphism link
  44. # instantiate c1 attribute
  45. c1_node = bottom.create_node(str(c1_model))
  46. bottom.create_edge(self.model, c1_node, "point.c1")
  47. c1_link = bottom.create_edge(point_node, c1_node)
  48. bottom.create_edge(self.model, c1_link, "point.c1_link")
  49. ltm_point_node, = bottom.read_outgoing_elements(self.type_model, "Float")
  50. ltm_point_link, = bottom.read_outgoing_elements(self.type_model, "PointCartesian_c1")
  51. bottom.create_edge(c1_node, ltm_point_node, "Morphism")
  52. bottom.create_edge(c1_link, ltm_point_link, "Morphism")
  53. # instantiate c2 attribute
  54. c2_node = bottom.create_node(str(c2_model))
  55. bottom.create_edge(self.model, c2_node, "point.c2")
  56. c2_link = bottom.create_edge(point_node, c2_node)
  57. bottom.create_edge(self.model, c2_link, "point.c2_link")
  58. ltm_point_node, = bottom.read_outgoing_elements(self.type_model, "Float")
  59. ltm_point_link, = bottom.read_outgoing_elements(self.type_model, "PointCartesian_c2")
  60. bottom.create_edge(c2_node, ltm_point_node, "Morphism")
  61. bottom.create_edge(c2_link, ltm_point_link, "Morphism")
  62. def from_bottom(self):
  63. bottom = Bottom(self.state)
  64. keys = bottom.read_keys(self.model)
  65. x_key, = filter(lambda k: k.endswith(".c1"), keys)
  66. y_key, = filter(lambda k: k.endswith(".c2"), keys)
  67. x_ref_node, = bottom.read_outgoing_elements(self.model, x_key)
  68. y_ref_node, = bottom.read_outgoing_elements(self.model, y_key)
  69. x_model = UUID(bottom.read_value(x_ref_node))
  70. y_model = UUID(bottom.read_value(y_ref_node))
  71. x_val_node, = bottom.read_outgoing_elements(x_model)
  72. y_val_node, = bottom.read_outgoing_elements(y_model)
  73. self.point = (bottom.read_value(x_val_node), bottom.read_value(y_val_node))