cartesian.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. """
  7. Implements services for the point cartesian LTM.
  8. Implementation is done in terms of Python data structures
  9. """
  10. def __init__(self, model: UUID, state: State):
  11. type_model_id = state.read_dict(state.read_root(), "PointCartesian")
  12. self.type_model = UUID(state.read_value(type_model_id))
  13. self.model = model
  14. self.state = state
  15. self.point = None
  16. def create_point(self, x: float, y: float):
  17. """
  18. Creates a point.
  19. Args:
  20. x: x coordinate
  21. y: y coordinate
  22. Returns:
  23. Nothing.
  24. """
  25. if self.point is None:
  26. self.point = (x, y)
  27. else:
  28. raise RuntimeError("A PointCartesian model can contain at most 1 point.")
  29. def read_point(self):
  30. """
  31. Reads point.
  32. Returns:
  33. Textual representation of the point data.
  34. """
  35. if self.point is None:
  36. raise RuntimeError("No point found in model.")
  37. else:
  38. return f"(X = {self.point[0]}, Y = {self.point[1]})"
  39. def delete_point(self):
  40. """
  41. Deletes point.
  42. Returns:
  43. Nothing.
  44. """
  45. self.point = None
  46. def apply_movement(self, delta_x: float, delta_y: float):
  47. """
  48. Moves point.
  49. Args:
  50. delta_x: change in x dimension
  51. delta_y: change in y dimension
  52. Returns:
  53. Nothing.
  54. """
  55. if self.point is not None:
  56. self.point = (self.point[0] + delta_x, self.point[1] + delta_y)
  57. else:
  58. raise RuntimeError("No point found in model.")
  59. def to_bottom(self):
  60. """
  61. Converts implementation specific model representation to
  62. canonical representation.
  63. Returns:
  64. Nothing.
  65. """
  66. bottom = Bottom(self.state)
  67. # clear residual model
  68. for element in bottom.read_outgoing_elements(self.model):
  69. bottom.delete_element(element)
  70. # create primitive models
  71. c1_model = bottom.create_node()
  72. c2_model = bottom.create_node()
  73. Float(c1_model, self.state).create(self.point[0])
  74. Float(c2_model, self.state).create(self.point[1])
  75. # instantiate Point class
  76. point_node = bottom.create_node() # create point node
  77. bottom.create_edge(self.model, point_node, "point") # attach to model
  78. morph_node, = bottom.read_outgoing_elements(self.type_model, "PointCartesian") # retrieve type
  79. bottom.create_edge(point_node, morph_node, "Morphism") # create morphism link
  80. # instantiate c1 attribute
  81. c1_node = bottom.create_node(str(c1_model))
  82. bottom.create_edge(self.model, c1_node, "point.c1")
  83. c1_link = bottom.create_edge(point_node, c1_node)
  84. bottom.create_edge(self.model, c1_link, "point.c1_link")
  85. ltm_point_node, = bottom.read_outgoing_elements(self.type_model, "Float")
  86. ltm_point_link, = bottom.read_outgoing_elements(self.type_model, "PointCartesian_c1")
  87. bottom.create_edge(c1_node, ltm_point_node, "Morphism")
  88. bottom.create_edge(c1_link, ltm_point_link, "Morphism")
  89. # instantiate c2 attribute
  90. c2_node = bottom.create_node(str(c2_model))
  91. bottom.create_edge(self.model, c2_node, "point.c2")
  92. c2_link = bottom.create_edge(point_node, c2_node)
  93. bottom.create_edge(self.model, c2_link, "point.c2_link")
  94. ltm_point_node, = bottom.read_outgoing_elements(self.type_model, "Float")
  95. ltm_point_link, = bottom.read_outgoing_elements(self.type_model, "PointCartesian_c2")
  96. bottom.create_edge(c2_node, ltm_point_node, "Morphism")
  97. bottom.create_edge(c2_link, ltm_point_link, "Morphism")
  98. def from_bottom(self):
  99. """
  100. Converts canonical representation to
  101. implementation specific model representation.
  102. Returns:
  103. Nothing.
  104. """
  105. bottom = Bottom(self.state)
  106. keys = bottom.read_keys(self.model)
  107. x_key, = filter(lambda k: k.endswith(".c1"), keys)
  108. y_key, = filter(lambda k: k.endswith(".c2"), keys)
  109. x_ref_node, = bottom.read_outgoing_elements(self.model, x_key)
  110. y_ref_node, = bottom.read_outgoing_elements(self.model, y_key)
  111. x_model = UUID(bottom.read_value(x_ref_node))
  112. y_model = UUID(bottom.read_value(y_ref_node))
  113. x_val_node, = bottom.read_outgoing_elements(x_model)
  114. y_val_node, = bottom.read_outgoing_elements(y_model)
  115. self.point = (bottom.read_value(x_val_node), bottom.read_value(y_val_node))