TestChangeMode.py 6.1KB

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