-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimEngine.py
168 lines (136 loc) · 5.31 KB
/
SimEngine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# built-in
import threading
import time
import datetime
# third-party
# local
class SimEngine(threading.Thread):
'''
Discrete-event simulation engine for a swarm of DotBots.
'''
MODE_PAUSE = 'pause'
MODE_FRAMEFORWARD = 'frameforward'
MODE_PLAY = 'play'
MODE_FASTFORWARD = 'fastforward'
# singleton pattern
_instance = None
_init = False
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(SimEngine, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
# singleton patterm
if self._init:
return
self._init = True
# local variables
self._currentTime = 0 # what time is it for the DotBots
self._mode = self.MODE_PAUSE
self._startTsSim = None
self._startTsReal = None
self._playSpeed = 1.00
self.events = []
self.semNumEvents = threading.Semaphore(0)
self.dataLock = threading.Lock()
self.semIsRunning = threading.Lock()
self.semIsRunning.acquire()
# start thread
threading.Thread.__init__(self)
self.name = 'SimEngine'
self.daemon = True
self.start()
#======================== thread ==========================================
def run(self):
while True:
# wait for simulator to be running
self.semIsRunning.acquire()
self.semIsRunning.release()
# wait for at least one event
self.semNumEvents.acquire()
# handle next event
(ts,cb) = self.events.pop(0)
assert self._currentTime<=ts
self._currentTime = ts
cb()
# switch to MODE_PAUSE if in MODE_FRAMEFORWARD
if self._mode==self.MODE_FRAMEFORWARD:
self._mode=self.MODE_PAUSE
self.semIsRunning.acquire()
# wait if in MODE_PLAY
if self._mode==self.MODE_PLAY:
durSim = self._currentTime-self._startTsSim
durReal = time.time()-self._startTsReal
if durReal*self._playSpeed<durSim:
time.sleep( durSim - (durReal*self._playSpeed) )
#======================== public ==========================================
#=== from other elements in simulator
def currentTime(self):
return self._currentTime
def mode(self):
return self._mode
def formatSimulatedTime(self):
returnVal = []
returnVal += ['[']
returnVal += [' {0} simulated'.format(str(datetime.timedelta(seconds=self._currentTime)).split('.')[0])]
if self._startTsSim!=None :
durSim = self._currentTime-self._startTsSim
durReal = time.time()-self._startTsReal
if durReal>1:
simSpeed = int(durSim / durReal)
else:
simSpeed = '?'
returnVal += ['( {0:>4} × )'.format(simSpeed)]
returnVal += [']']
returnVal = ' '.join(returnVal)
return returnVal
def schedule(self,ts,cb):
# add new event
self.events += [(ts,cb)]
# reorder list
self.events = sorted(self.events, key = lambda e: e[0])
# release semaphore
self.semNumEvents.release()
#=== commands from the GUI
def commandPause(self):
'''
pause the execution of the simulation
'''
with self.dataLock:
if self._mode != self.MODE_PAUSE:
self.semIsRunning.acquire()
self._startTsSim = None
self._startTsReal = None
self._mode = self.MODE_PAUSE
def commandFrameforward(self):
'''
execute next event in list of events
'''
with self.dataLock:
if self._mode == self.MODE_PAUSE:
self.semIsRunning.release()
self._startTsSim = None
self._startTsReal = None
self._mode = self.MODE_FRAMEFORWARD
def commandPlay(self,playSpeed):
'''
(re)start the execution of the simulation at moderate simSpeed
'''
with self.dataLock:
if self._mode == self.MODE_PAUSE:
self.semIsRunning.release()
self._startTsSim = self._currentTime
self._startTsReal = time.time()
self._playSpeed = playSpeed
self._mode = self.MODE_PLAY
def commandFastforward(self):
'''
(re)start the execution of the simulation at full simSpeed
'''
with self.dataLock:
if self._mode == self.MODE_PAUSE:
self.semIsRunning.release()
self._startTsSim = self._currentTime
self._startTsReal = time.time()
self._mode = self.MODE_FASTFORWARD
#======================== private =========================================