Browse Source

add test for simulation events

Bastien Sevajol 7 years ago
parent
commit
72a4a3862b
3 changed files with 56 additions and 6 deletions
  1. 5 4
      synergine2/cycle.py
  2. 1 1
      synergine2/simulation.py
  3. 50 1
      tests/test_cycle.py

+ 5 - 4
synergine2/cycle.py View File

107
                     str(mechanism_data),
107
                     str(mechanism_data),
108
                 ))
108
                 ))
109
 
109
 
110
-            mechanisms_data[type(mechanism)] = mechanism_data
110
+            mechanisms_data[mechanism.__class__.__name__] = mechanism_data
111
 
111
 
112
         behaviours = self.simulation.behaviours.values()
112
         behaviours = self.simulation.behaviours.values()
113
         self.logger.info('{} behaviours to compute'.format(str(len(behaviours))))
113
         self.logger.info('{} behaviours to compute'.format(str(len(behaviours))))
126
                 ))
126
                 ))
127
 
127
 
128
             if behaviour_data:
128
             if behaviour_data:
129
-                behaviours_data[type(behaviour)] = behaviour_data
129
+                behaviours_data[id(behaviour.__class__)] = behaviour_data
130
 
130
 
131
         return behaviours_data
131
         return behaviours_data
132
 
132
 
168
         results_by_processes = self.process_manager.make_them_work(JOB_TYPE_SIMULATION)
168
         results_by_processes = self.process_manager.make_them_work(JOB_TYPE_SIMULATION)
169
 
169
 
170
         for process_result in results_by_processes:
170
         for process_result in results_by_processes:
171
-            for behaviour_class, behaviour_result in process_result.items():
171
+            for behaviour_class_id, behaviour_result in process_result.items():
172
+                behaviour_class = self.simulation.index[behaviour_class_id]
172
                 results[behaviour_class] = behaviour_class.merge_data(
173
                 results[behaviour_class] = behaviour_class.merge_data(
173
                     behaviour_result,
174
                     behaviour_result,
174
                     results.get(behaviour_class),
175
                     results.get(behaviour_class),
178
 
179
 
179
         # Make events
180
         # Make events
180
         for behaviour_class, behaviour_data in results.items():
181
         for behaviour_class, behaviour_data in results.items():
181
-            behaviour_events = self.simulation.behaviours[behaviour_class].action(behaviour_data)
182
+            behaviour_events = self.simulation.behaviours[behaviour_class.__name__].action(behaviour_data)
182
             self.logger.info('{} behaviour generate {} events'.format(
183
             self.logger.info('{} behaviour generate {} events'.format(
183
                 str(behaviour_class),
184
                 str(behaviour_class),
184
                 str(len(behaviour_events)),
185
                 str(len(behaviour_events)),

+ 1 - 1
synergine2/simulation.py View File

253
     def repr_debug(self) -> str:
253
     def repr_debug(self) -> str:
254
         return self.__class__.__name__
254
         return self.__class__.__name__
255
 
255
 
256
-    def run(self, process_id: int=None, process_count: int=None):
256
+    def run(self, process_number: int=None, process_count: int=None):
257
         raise NotImplementedError()
257
         raise NotImplementedError()
258
 
258
 
259
 
259
 

+ 50 - 1
tests/test_cycle.py View File

4
 from synergine2.log import SynergineLogger
4
 from synergine2.log import SynergineLogger
5
 from synergine2.share import shared
5
 from synergine2.share import shared
6
 from synergine2.simulation import Simulation
6
 from synergine2.simulation import Simulation
7
+from synergine2.simulation import SimulationBehaviour
8
+from synergine2.simulation import SimulationMechanism
7
 from synergine2.simulation import Event
9
 from synergine2.simulation import Event
8
 from synergine2.simulation import Subject
10
 from synergine2.simulation import Subject
9
 from synergine2.simulation import Subjects
11
 from synergine2.simulation import Subjects
42
     pass
44
     pass
43
 
45
 
44
 
46
 
47
+class MySimulationMechanism(SimulationMechanism):
48
+    def run(self, process_number: int = None, process_count: int = None):
49
+        return process_number + 1000
50
+
51
+
52
+class MySimulationBehaviour(SimulationBehaviour):
53
+    use = [MySimulationMechanism]
54
+
55
+    @classmethod
56
+    def merge_data(cls, new_data, start_data=None):
57
+        start_data = start_data or 0
58
+        return start_data + new_data
59
+
60
+    def run(self, data):
61
+        return data['MySimulationMechanism'] * 2
62
+
63
+    def action(self, data) -> [Event]:
64
+        return [MyEvent(data)]
65
+
66
+
67
+class MySimulation(Simulation):
68
+    behaviours_classes = [MySimulationBehaviour]
69
+
70
+
45
 class TestCycle(BaseTest):
71
 class TestCycle(BaseTest):
46
     def test_subjects_cycle(self):
72
     def test_subjects_cycle(self):
47
         shared.reset()
73
         shared.reset()
75
 
101
 
76
     def test_new_subject(self):
102
     def test_new_subject(self):
77
         shared.reset()
103
         shared.reset()
78
-        subject_ids = shared.get('subject_ids')
79
         config = Config({'core': {'use_x_cores': 1}})
104
         config = Config({'core': {'use_x_cores': 1}})
80
         logger = SynergineLogger(name='test')
105
         logger = SynergineLogger(name='test')
81
 
106
 
111
         event_values = [e.value for e in events]
136
         event_values = [e.value for e in events]
112
         assert all([s.id * 2 in event_values for s in subjects])
137
         assert all([s.id * 2 in event_values for s in subjects])
113
 
138
 
139
+    def test_simulation_events(self):
140
+        shared.reset()
141
+        config = Config({'core': {'use_x_cores': 2}})
142
+        logger = SynergineLogger(name='test')
143
+
144
+        simulation = MySimulation(config)
145
+
146
+        # Prepare simulation class index
147
+        simulation.add_to_index(MySimulationBehaviour, MySimulationMechanism)
148
+
149
+        subjects = MySubjects(simulation=simulation)
150
+        simulation.subjects = subjects
151
+
152
+        cycle_manager = CycleManager(
153
+            config=config,
154
+            logger=logger,
155
+            simulation=simulation,
156
+        )
157
+
158
+        events = cycle_manager.next()
159
+        cycle_manager.stop()
160
+
161
+        assert 1 == len(events)
162
+        assert events[0].value == 4002