TestChangeMode.test_from_exploration_to_go_home()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

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