TestChangeMode.py 5.4KB

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