|
@@ -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
|