TestChangeMode.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from intelligine.synergy.Environment import Environment
  2. from intelligine.synergy.object.Food import Food
  3. from intelligine.tests.simulation.mode.Base import Base
  4. from intelligine.synergy.Colony import Colony
  5. from intelligine.synergy.Simulation import Simulation
  6. from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
  7. from intelligine.synergy.event.move.MoveAction import MoveAction
  8. from intelligine.synergy.event.move.direction import NORTH, SOUTH
  9. from intelligine.tests.src.event.MoveAction import MoveAction as TestMoveAction
  10. from synergine.synergy.collection.SynergyCollection import SynergyCollection
  11. from synergine.synergy.collection.Configuration import Configuration
  12. from intelligine.core.Context import Context
  13. from intelligine.cst import MODE_EXPLO, MODE_GOHOME, MODE, MODE_HOME, PHEROMON_DIR_NONE
  14. from intelligine.cst import PHEROMON_DIR_EXPLO
  15. class TestChangeMode(Base):
  16. def __init__(self, *args, **kwargs):
  17. super().__init__(*args, **kwargs)
  18. self.ant = None
  19. self.food = None
  20. self._force_move = self._force_move
  21. @staticmethod
  22. def _force_move(self_move_action, object_id, context):
  23. object_movement_mode = context.metas.value.get(MODE, object_id)
  24. if object_movement_mode == MODE_GOHOME or object_movement_mode == MODE_HOME:
  25. return SOUTH
  26. return NORTH
  27. def _get_set_up_simulations(self):
  28. return [Simulation([self._get_colony(), self._get_foods(), self._get_environment()])]
  29. def _get_colony(self):
  30. test_case = self
  31. class TestColony(Colony):
  32. def __init__(self, configuration):
  33. super().__init__(configuration)
  34. self._actions.remove(MoveAction)
  35. TestMoveAction.set_move_event(test_case._force_move)
  36. self._actions.append(TestMoveAction)
  37. return TestColony(self._get_colony_configuration())
  38. def _get_colony_configuration(self):
  39. test_case = self
  40. class TestColonyConfiguration(ColonyConfiguration):
  41. _start_position = (0, 0, 0)
  42. _ant_count = 1
  43. def get_start_objects(self, collection, context):
  44. ants = super().get_start_objects(collection, context)
  45. test_case.ant = ants[0]
  46. return ants
  47. return TestColonyConfiguration()
  48. def _get_foods(self):
  49. class Foods(SynergyCollection):
  50. pass
  51. return Foods(self._get_food_configuration())
  52. def _get_food_configuration(self):
  53. test_case = self
  54. class FoodConfiguration(Configuration):
  55. def get_start_objects(self, collection, context):
  56. foods = []
  57. food = Food(collection, context)
  58. stocked_food = Food(collection, context)
  59. stocked_food.transform_to_stocked()
  60. food.set_position((0, 0, -20))
  61. stocked_food.set_position((0, 0, 0))
  62. foods.append(stocked_food)
  63. foods.append(food)
  64. test_case.food = food
  65. return foods
  66. return FoodConfiguration()
  67. def _get_environment(self):
  68. class TestEnvironment(Environment):
  69. pass
  70. return TestEnvironment(self._get_environment_configuration())
  71. def _get_environment_configuration(self):
  72. class TestEnvironmentConfiguration(Configuration):
  73. pass
  74. return TestEnvironmentConfiguration()
  75. def _get_core_configuration(self, cycles, main_process=True):
  76. config = super()._get_core_configuration(cycles, main_process)
  77. config.update({
  78. 'app': {
  79. 'classes': {
  80. 'Context': Context
  81. }
  82. },
  83. 'ant': {
  84. 'take': {
  85. # Disable this constrain to test in little space
  86. 'cant_put_still': 0
  87. }
  88. }
  89. })
  90. return config
  91. def test_from_exploration_to_go_home(self):
  92. self._run_and_get_core(0)
  93. self.assertEquals((0, 0, 0), self.ant.get_position())
  94. self.assertEquals(MODE_EXPLO, self.ant.get_brain().get_movement_mode())
  95. self.assertFalse(self.ant.is_carrying())
  96. self._run_and_get_core(1)
  97. self.assertEquals((0, 0, -1), self.ant.get_position())
  98. self.assertEquals(MODE_EXPLO, self.ant.get_brain().get_movement_mode())
  99. self.assertFalse(self.ant.is_carrying())
  100. # Ant has take Food piece
  101. self._run_and_get_core(20)
  102. self.assertEquals((0, 0, -20), self.ant.get_position())
  103. self.assertTrue(self.ant.is_carrying())
  104. self.assertIsNotNone(self.ant.get_carried())
  105. self.assertEquals(self.food.__class__, self.ant.get_carried().__class__)
  106. molecule = self.ant.get_movement_molecule_gland().get_molecule()
  107. # Now it appose exploration molecule
  108. self.assertEquals((PHEROMON_DIR_EXPLO, 0), (molecule.get_type(), molecule.get_distance()))
  109. self.assertEquals(MODE_GOHOME, self.ant.get_brain().get_movement_mode())
  110. self.assertEquals(PHEROMON_DIR_EXPLO, self.ant.get_movement_molecule_gland().get_molecule_type())
  111. self._run_and_get_core(34)
  112. self.assertEquals((0, 0, -6), self.ant.get_position())
  113. self.assertTrue(self.ant.is_carrying())
  114. self.assertEquals(MODE_HOME, self.ant.get_brain().get_movement_mode())
  115. self._run_and_get_core(35)
  116. self.assertEquals((0, 0, -5), self.ant.get_position())
  117. self.assertTrue(self.ant.is_carrying())
  118. self.assertEquals(MODE_HOME, self.ant.get_brain().get_movement_mode())
  119. self._run_and_get_core(36)
  120. self.assertEquals((0, 0, -4), self.ant.get_position())
  121. self.assertEquals(MODE_HOME, self.ant.get_brain().get_movement_mode())
  122. self._run_and_get_core(39)
  123. self.assertEquals((0, 0, -1), self.ant.get_position())
  124. # Ant has NOT put his food piece
  125. self.assertFalse(self.ant.is_carrying())
  126. self._run_and_get_core(40)
  127. self.assertEquals((0, 0, -2), self.ant.get_position())
  128. self.assertFalse(self.ant.is_carrying())