TestChangeMode.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from os import getcwd
  2. from sys import path as ppath
  3. ppath.insert(1,getcwd()+'/modules')
  4. from intelligine.synergy.object.Food import Food
  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, methodName='runTest'):
  19. super().__init__(methodName)
  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:
  27. return SOUTH
  28. return NORTH
  29. def _get_set_up_simulations(self):
  30. return [Simulation([self._get_colony(), self._get_foods()])]
  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. food.set_position((0, 0, -3))
  61. #  TEST (en attendant d'avoir des algo pour deposer dans un depot)
  62. food1 = Food(collection, context)
  63. food1.set_position((0, 0, -1))
  64. food1.is_takable = lambda: False
  65. foods.append(food1)
  66. foods.append(food)
  67. test_case.food = food
  68. return foods
  69. return FoodConfiguration()
  70. def _get_core_configuration(self, cycles, main_process=True):
  71. config = super()._get_core_configuration(cycles, main_process)
  72. config.update({
  73. 'app': {
  74. 'classes': {
  75. 'Context': Context
  76. }
  77. },
  78. 'ant': {
  79. 'take': {
  80. # Disable this constrain to test in little space
  81. 'cant_put_still': 0
  82. }
  83. }
  84. })
  85. return config
  86. def test_from_exploration_to_go_home(self):
  87. self._run_and_get_core(0)
  88. self.assertEquals((0, 0, 0), self.ant.get_position())
  89. self.assertEquals(MOVE_MODE_EXPLO, self.ant.get_brain().get_movement_mode())
  90. self.assertFalse(self.ant.is_carrying())
  91. self._run_and_get_core(1)
  92. self.assertEquals((0, 0, -1), self.ant.get_position())
  93. self.assertEquals(MOVE_MODE_EXPLO, self.ant.get_brain().get_movement_mode())
  94. self.assertFalse(self.ant.is_carrying())
  95. # Ant has take Food piece
  96. self._run_and_get_core(2)
  97. self.assertEquals((0, 0, -2), self.ant.get_position())
  98. self.assertTrue(self.ant.is_carrying())
  99. self.assertIsNotNone(self.ant.get_carried())
  100. self.assertEquals(self.food.__class__, self.ant.get_carried().__class__)
  101. molecule = self.ant.get_movement_molecule_gland().get_molecule()
  102. # Now it appose exploration molecule
  103. self.assertEquals((PHEROMON_DIR_EXPLO, 0), (molecule.get_type(), molecule.get_distance()))
  104. self.assertEquals(MOVE_MODE_GOHOME, self.ant.get_brain().get_movement_mode())
  105. self.assertEquals(PHEROMON_DIR_EXPLO, self.ant.get_movement_molecule_gland().get_molecule_type())
  106. self._run_and_get_core(3)
  107. self.assertEquals((0, 0, -1), self.ant.get_position())
  108. self.assertTrue(self.ant.is_carrying())
  109. self._run_and_get_core(4)
  110. self.assertEquals((0, 0, 0), self.ant.get_position())
  111. # Ant has NOT put his food piece
  112. self.assertTrue(self.ant.is_carrying())
  113. # TODO: Le "poser" depend maintenant de matiere où poser. Tests à ecrires.