TestChangeMode.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from intelligine.synergy.Environment import Environment
  2. from intelligine.synergy.object.Food import Food
  3. from intelligine.synergy.object.StockedFood import StockedFood
  4. from intelligine.tests.simulation.mode.Base import Base
  5. from intelligine.synergy.Colony import Colony
  6. from intelligine.synergy.Simulation import Simulation
  7. from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
  8. from intelligine.synergy.event.move.MoveAction import MoveAction
  9. from intelligine.synergy.event.move.direction import NORTH, SOUTH
  10. from intelligine.tests.src.event.MoveAction import MoveAction as TestMoveAction
  11. from synergine.synergy.collection.SynergyCollection import SynergyCollection
  12. from synergine.synergy.collection.Configuration import Configuration
  13. from intelligine.core.Context import Context
  14. from intelligine.cst import MODE_EXPLO, MODE_GOHOME, MODE, MODE_HOME, PHEROMON_DIR_NONE
  15. from intelligine.cst import PHEROMON_DIR_EXPLO
  16. class TestChangeMode(Base):
  17. def __init__(self, *args, **kwargs):
  18. super().__init__(*args, **kwargs)
  19. self.ant = None
  20. self.food = None
  21. self._force_move = self._force_move
  22. @staticmethod
  23. def _force_move(self_move_action, object_id, context):
  24. object_movement_mode = context.metas.value.get(MODE, object_id)
  25. if object_movement_mode == MODE_GOHOME or object_movement_mode == MODE_HOME:
  26. return SOUTH
  27. return NORTH
  28. def _get_set_up_simulations(self):
  29. return [Simulation([self._get_colony(), self._get_foods(), self._get_environment()])]
  30. def _get_colony(self):
  31. test_case = self
  32. class TestColony(Colony):
  33. def __init__(self, configuration):
  34. super().__init__(configuration)
  35. self._actions.remove(MoveAction)
  36. TestMoveAction.set_move_event(test_case._force_move)
  37. self._actions.append(TestMoveAction)
  38. return TestColony(self._get_colony_configuration())
  39. def _get_colony_configuration(self):
  40. test_case = self
  41. class TestColonyConfiguration(ColonyConfiguration):
  42. _start_position = (0, 0, 0)
  43. _ant_count = 1
  44. def get_start_objects(self, collection, context):
  45. ants = super().get_start_objects(collection, context)
  46. test_case.ant = ants[0]
  47. return ants
  48. return TestColonyConfiguration()
  49. def _get_foods(self):
  50. class Foods(SynergyCollection):
  51. pass
  52. return Foods(self._get_food_configuration())
  53. def _get_food_configuration(self):
  54. test_case = self
  55. class FoodConfiguration(Configuration):
  56. def get_start_objects(self, collection, context):
  57. foods = []
  58. food = Food(collection, context)
  59. stocked_food = StockedFood(collection, context)
  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(19)
  102. self.assertEquals((0, 0, -19), 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(32)
  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(33)
  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(34)
  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(37)
  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(38)
  127. self.assertEquals((0, 0, -2), self.ant.get_position())
  128. self.assertFalse(self.ant.is_carrying())