Bladeren bron

add test for simulation events

Bastien Sevajol 7 jaren geleden
bovenliggende
commit
72a4a3862b
3 gewijzigde bestanden met toevoegingen van 56 en 6 verwijderingen
  1. 5 4
      synergine2/cycle.py
  2. 1 1
      synergine2/simulation.py
  3. 50 1
      tests/test_cycle.py

+ 5 - 4
synergine2/cycle.py Bestand weergeven

@@ -107,7 +107,7 @@ class CycleManager(BaseObject):
107 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 112
         behaviours = self.simulation.behaviours.values()
113 113
         self.logger.info('{} behaviours to compute'.format(str(len(behaviours))))
@@ -126,7 +126,7 @@ class CycleManager(BaseObject):
126 126
                 ))
127 127
 
128 128
             if behaviour_data:
129
-                behaviours_data[type(behaviour)] = behaviour_data
129
+                behaviours_data[id(behaviour.__class__)] = behaviour_data
130 130
 
131 131
         return behaviours_data
132 132
 
@@ -168,7 +168,8 @@ class CycleManager(BaseObject):
168 168
         results_by_processes = self.process_manager.make_them_work(JOB_TYPE_SIMULATION)
169 169
 
170 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 173
                 results[behaviour_class] = behaviour_class.merge_data(
173 174
                     behaviour_result,
174 175
                     results.get(behaviour_class),
@@ -178,7 +179,7 @@ class CycleManager(BaseObject):
178 179
 
179 180
         # Make events
180 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 183
             self.logger.info('{} behaviour generate {} events'.format(
183 184
                 str(behaviour_class),
184 185
                 str(len(behaviour_events)),

+ 1 - 1
synergine2/simulation.py Bestand weergeven

@@ -253,7 +253,7 @@ class SimulationMechanism(Mechanism):
253 253
     def repr_debug(self) -> str:
254 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 257
         raise NotImplementedError()
258 258
 
259 259
 

+ 50 - 1
tests/test_cycle.py Bestand weergeven

@@ -4,6 +4,8 @@ from synergine2.cycle import CycleManager
4 4
 from synergine2.log import SynergineLogger
5 5
 from synergine2.share import shared
6 6
 from synergine2.simulation import Simulation
7
+from synergine2.simulation import SimulationBehaviour
8
+from synergine2.simulation import SimulationMechanism
7 9
 from synergine2.simulation import Event
8 10
 from synergine2.simulation import Subject
9 11
 from synergine2.simulation import Subjects
@@ -42,6 +44,30 @@ class MySubjects(Subjects):
42 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 71
 class TestCycle(BaseTest):
46 72
     def test_subjects_cycle(self):
47 73
         shared.reset()
@@ -75,7 +101,6 @@ class TestCycle(BaseTest):
75 101
 
76 102
     def test_new_subject(self):
77 103
         shared.reset()
78
-        subject_ids = shared.get('subject_ids')
79 104
         config = Config({'core': {'use_x_cores': 1}})
80 105
         logger = SynergineLogger(name='test')
81 106
 
@@ -111,3 +136,27 @@ class TestCycle(BaseTest):
111 136
         event_values = [e.value for e in events]
112 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