TestChangeMode.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from intelligine.synergy.object.Food import Food
  2. from intelligine.tests.simulation.mode.Base import Base
  3. from intelligine.synergy.Colony import Colony
  4. from intelligine.synergy.Simulation import Simulation
  5. from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
  6. from intelligine.synergy.event.move.MoveAction import MoveAction
  7. from intelligine.synergy.event.move.direction import NORTH, SOUTH
  8. from intelligine.tests.src.event.MoveAction import MoveAction as TestMoveAction
  9. from synergine.synergy.collection.SynergyCollection import SynergyCollection
  10. from synergine.synergy.collection.Configuration import Configuration
  11. from intelligine.core.Context import Context
  12. from intelligine.cst import MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, MOVE_MODE, MOVE_MODE_HOME, PHEROMON_DIR_NONE
  13. from intelligine.cst import PHEROMON_DIR_EXPLO
  14. class TestChangeMode(Base):
  15. def __init__(self, methodName='runTest'):
  16. super().__init__(methodName)
  17. self.ant = None
  18. self.food = None
  19. self._force_move = self._force_move
  20. @staticmethod
  21. def _force_move(self_move_action, object_id, context):
  22. object_movement_mode = context.metas.value.get(MOVE_MODE, object_id)
  23. if object_movement_mode == MOVE_MODE_GOHOME:
  24. return SOUTH
  25. return NORTH
  26. def _get_set_up_simulations(self):
  27. return [Simulation([self._get_colony(), self._get_foods()])]
  28. def _get_colony(self):
  29. test_case = self
  30. class TestColony(Colony):
  31. def __init__(self, configuration):
  32. super().__init__(configuration)
  33. self._actions.remove(MoveAction)
  34. TestMoveAction.set_move_event(test_case._force_move)
  35. self._actions.append(TestMoveAction)
  36. return TestColony(self._get_colony_configuration())
  37. def _get_colony_configuration(self):
  38. test_case = self
  39. class TestColonyConfiguration(ColonyConfiguration):
  40. _start_position = (0, 0, 0)
  41. _ant_count = 1
  42. def get_start_objects(self, collection, context):
  43. ants = super().get_start_objects(collection, context)
  44. test_case.ant = ants[0]
  45. return ants
  46. return TestColonyConfiguration()
  47. def _get_foods(self):
  48. class Foods(SynergyCollection):
  49. pass
  50. return Foods(self._get_food_configuration())
  51. def _get_food_configuration(self):
  52. test_case = self
  53. class FoodConfiguration(Configuration):
  54. def get_start_objects(self, collection, context):
  55. foods = []
  56. food = Food(collection, context)
  57. food.set_position((0, 0, -3))
  58. #  TEST (en attendant d'avoir des algo pour deposer dans un depot)
  59. food1 = Food(collection, context)
  60. food1.set_position((0, 0, -1))
  61. food1.is_takable = lambda: False
  62. foods.append(food1)
  63. foods.append(food)
  64. test_case.food = food
  65. return foods
  66. return FoodConfiguration()
  67. def _get_core_configuration(self, cycles, main_process=True):
  68. config = super()._get_core_configuration(cycles, main_process)
  69. config.update({
  70. 'app': {
  71. 'classes': {
  72. 'Context': Context
  73. }
  74. },
  75. 'ant': {
  76. 'take': {
  77. # Disable this constrain to test in little space
  78. 'cant_put_still': 0
  79. }
  80. }
  81. })
  82. return config
  83. def test_from_exploration_to_go_home(self):
  84. self._run_and_get_core(0)
  85. self.assertEquals((0, 0, 0), self.ant.get_position())
  86. self.assertEquals(MOVE_MODE_EXPLO, self.ant.get_brain().get_movement_mode())
  87. self.assertFalse(self.ant.is_carrying())
  88. self._run_and_get_core(1)
  89. self.assertEquals((0, 0, -1), self.ant.get_position())
  90. self.assertEquals(MOVE_MODE_EXPLO, self.ant.get_brain().get_movement_mode())
  91. self.assertFalse(self.ant.is_carrying())
  92. # Ant has take Food piece
  93. self._run_and_get_core(2)
  94. self.assertEquals((0, 0, -2), self.ant.get_position())
  95. self.assertTrue(self.ant.is_carrying())
  96. self.assertIsNotNone(self.ant.get_carried())
  97. self.assertEquals(self.food.__class__, self.ant.get_carried().__class__)
  98. molecule = self.ant.get_movement_molecule_gland().get_molecule()
  99. # Now it appose exploration molecule
  100. self.assertEquals((PHEROMON_DIR_EXPLO, 0), (molecule.get_type(), molecule.get_distance()))
  101. self.assertEquals(MOVE_MODE_GOHOME, self.ant.get_brain().get_movement_mode())
  102. self.assertEquals(PHEROMON_DIR_EXPLO, self.ant.get_movement_molecule_gland().get_molecule_type())
  103. self._run_and_get_core(3)
  104. self.assertEquals((0, 0, -1), self.ant.get_position())
  105. self.assertTrue(self.ant.is_carrying())
  106. self._run_and_get_core(4)
  107. self.assertEquals((0, 0, 0), self.ant.get_position())
  108. # Ant has NOT put his food piece
  109. self.assertTrue(self.ant.is_carrying())
  110. # TODO: Le "poser" depend maintenant de matiere où poser. Tests à ecrires.