polar.py 4.9 KB

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