test_xyz.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # coding: utf-8
  2. # -*- coding: utf-8 -*-
  3. from synergine2.config import Config
  4. from synergine2.share import shared
  5. from synergine2.simulation import Subject
  6. from synergine2_xyz.mechanism import ProximitySubjectMechanism
  7. from synergine2_xyz.simulation import XYZSimulation
  8. from synergine2_xyz.xyz import XYZSubjectMixin
  9. from synergine2_xyz.subjects import XYZSubjects
  10. from synergine2_xyz.utils import get_positions_from_str_representation
  11. from synergine2_xyz.utils import get_str_representation_from_positions
  12. from tests import BaseTest
  13. from tests import str_kwargs
  14. class MySubject(XYZSubjectMixin, Subject):
  15. pass
  16. class MyProximityMechanism(ProximitySubjectMechanism):
  17. distance = 10
  18. class TestXYZ(BaseTest):
  19. def test_proximity_mechanism_with_one(self):
  20. shared.reset()
  21. simulation = XYZSimulation(Config())
  22. simulation.add_to_index(
  23. MyProximityMechanism,
  24. MySubject,
  25. )
  26. subject = MySubject(Config(), simulation, position=(0, 0, 0))
  27. other_subject = MySubject(Config(), simulation, position=(5, 0, 0))
  28. simulation.subjects = XYZSubjects(
  29. [subject, other_subject],
  30. simulation=simulation,
  31. )
  32. simulation.subjects.auto_expose = False
  33. proximity_mechanism = MyProximityMechanism(
  34. config=Config(),
  35. simulation=simulation,
  36. subject=subject,
  37. )
  38. assert 5 == proximity_mechanism.get_distance_of(
  39. position=subject.position,
  40. subject=other_subject,
  41. )
  42. assert [{
  43. 'subject_id': other_subject.id,
  44. 'direction': 90.0,
  45. 'distance': 5.0,
  46. }] == proximity_mechanism.run()
  47. def test_proximity_mechanism_excluding(self):
  48. shared.reset()
  49. simulation = XYZSimulation(Config())
  50. simulation.add_to_index(
  51. MyProximityMechanism,
  52. MySubject,
  53. )
  54. subject = MySubject(Config(), simulation, position=(0, 0, 0))
  55. other_subject = MySubject(Config(), simulation, position=(11, 0, 0))
  56. simulation.subjects = XYZSubjects(
  57. [subject, other_subject],
  58. simulation=simulation,
  59. )
  60. simulation.subjects.auto_expose = False
  61. proximity_mechanism = MyProximityMechanism(
  62. config=Config(),
  63. simulation=simulation,
  64. subject=subject,
  65. )
  66. assert 11 == proximity_mechanism.get_distance_of(
  67. position=subject.position,
  68. subject=other_subject,
  69. )
  70. # other_subject is to far away
  71. assert [] == proximity_mechanism.run()
  72. def test_proximity_mechanism_with_multiple(self):
  73. shared.reset()
  74. simulation = XYZSimulation(Config())
  75. simulation.add_to_index(
  76. MyProximityMechanism,
  77. MySubject,
  78. )
  79. subject = MySubject(Config(), simulation, position=(0, 0, 0))
  80. other_subjects = []
  81. for i in range(3):
  82. other_subjects.append(MySubject(Config(), simulation, position=(i, i, 0)))
  83. simulation.subjects = XYZSubjects([subject], simulation=simulation)
  84. simulation.subjects.extend(other_subjects)
  85. simulation.subjects.auto_expose = False
  86. proximity_mechanism = MyProximityMechanism(
  87. config=Config(),
  88. simulation=simulation,
  89. subject=subject,
  90. )
  91. data = proximity_mechanism.run()
  92. assert [
  93. {
  94. 'direction': 0,
  95. 'subject_id': other_subjects[0].id,
  96. 'distance': 0.0,
  97. },
  98. {
  99. 'direction': 135.0,
  100. 'subject_id': other_subjects[1].id,
  101. 'distance': 1.41
  102. },
  103. {
  104. 'direction': 135.0,
  105. 'subject_id': other_subjects[2].id,
  106. 'distance': 2.83
  107. },
  108. ] == data
  109. def test_str_representation_from_str(self):
  110. str_ = """
  111. 0 0 1 0 0
  112. 0 1 1 1 0
  113. 0 0 1 0 0
  114. """
  115. items_positions = {
  116. '0': [
  117. (-2, -1, 0),
  118. (-1, -1, 0),
  119. (1, -1, 0),
  120. (2, -1, 0),
  121. (-2, 0, 0),
  122. (2, 0, 0),
  123. (-2, 1, 0),
  124. (-1, 1, 0),
  125. (1, 1, 0),
  126. (2, 1, 0),
  127. ],
  128. '1': [
  129. (0, -1, 0),
  130. (-1, 0, 0),
  131. (0, 0, 0),
  132. (1, 0, 0),
  133. (0, 1, 0),
  134. ],
  135. }
  136. assert items_positions == get_positions_from_str_representation(str_)
  137. def test_str_representation_to_str(self):
  138. expected = """
  139. 0 0 1 0 0
  140. 0 1 1 1 0
  141. 0 0 1 0 0
  142. """
  143. items_positions = {
  144. '0': [
  145. (-2, -1, 0),
  146. (-1, -1, 0),
  147. (1, -1, 0),
  148. (2, -1, 0),
  149. (-2, 0, 0),
  150. (2, 0, 0),
  151. (-2, 1, 0),
  152. (-1, 1, 0),
  153. (1, 1, 0),
  154. (2, 1, 0),
  155. ],
  156. '1': [
  157. (0, -1, 0),
  158. (-1, 0, 0),
  159. (0, 0, 0),
  160. (1, 0, 0),
  161. (0, 1, 0),
  162. ],
  163. }
  164. assert expected == \
  165. get_str_representation_from_positions(
  166. items_positions,
  167. **str_kwargs
  168. )
  169. # def test_str_representation_to_str_multi_levels(self):
  170. # expected = """
  171. # 0 0 1 0 0
  172. # 0 1 1 1 0
  173. # 0 0 1 0 0
  174. # ----
  175. # 0 0 0 0 0
  176. # 0 0 1 0 0
  177. # 0 0 0 0 0
  178. # """
  179. # items_positions = {
  180. # '0': [
  181. # (-2, -1, 0),
  182. # (-1, -1, 0),
  183. # (1, -1, 0),
  184. # (2, -1, 0),
  185. # (-2, 0, 0),
  186. # (2, 0, 0),
  187. # (-2, 1, 0),
  188. # (-1, 1, 0),
  189. # (1, 1, 0),
  190. # (2, 1, 0),
  191. # (-2, -1, 1),
  192. # (-1, -1, 1),
  193. # (1, -1, 1),
  194. # (2, -1, 1),
  195. # (-1, 0, 1),
  196. # (-2, 0, 1),
  197. # (2, 0, 1),
  198. # (-2, 1, 1),
  199. # (-1, 1, 1),
  200. # (1, 1, 1),
  201. # (2, 1, 1),
  202. # (2, -1, 1),
  203. # (1, 0, 1),
  204. # (2, 1, 1),
  205. # ],
  206. # '1': [
  207. # (0, -1, 0),
  208. # (-1, 0, 0),
  209. # (0, 0, 0),
  210. # (1, 0, 0),
  211. # (0, 1, 0),
  212. # (0, 0, 1),
  213. # ],
  214. # }
  215. #
  216. # assert expected == \
  217. # get_str_representation_from_positions(
  218. # items_positions,
  219. # **str_kwargs
  220. # )