-
Notifications
You must be signed in to change notification settings - Fork 0
/
statechart.py
418 lines (347 loc) · 13.5 KB
/
statechart.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""Implementation of statechart statechart.
Generated by YAKINDU Statechart Tools code generator.
"""
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))
from yakindu.rx import Observable
class Statechart:
"""Implementation of the state machine Statechart.
"""
class State:
""" State Enum
"""
(
main_region_off,
main_region_ready,
main_region_ready_ready_warming,
main_region_ready_ready_cooling,
main_region_warmup,
main_region_cooldown,
null_state
) = range(7)
class EvBuf:
"""Implementation of internal scope.
"""
def __init__(self):
self.set_target_temp = False
self.set_target_temp_value = None
class StatechartEvBuf:
"""Implementation of internal scope.
"""
def __init__(self, iface):
self.iface = iface
def __init__(self):
""" Declares all necessary variables including list of states, histories etc.
"""
self.target_temp = None
self.set_target_temp = None
self.set_target_temp_value = None
self.ready = None
self.ready_observable = Observable()
self.off = None
self.off_observable = Observable()
self.operation_callback = None
self.__threshold = None
# enumeration of all states:
self.__State = Statechart.State
self.__state_conf_vector_changed = None
self.__state_vector = [None] * 1
for __state_index in range(1):
self.__state_vector[__state_index] = self.State.null_state
# initializations:
self.__threshold = 5
self.target_temp = 0.0
self.__current = self.StatechartEvBuf(
self.EvBuf()
)
self.__is_executing = False
def is_active(self):
"""Checks if the state machine is active.
"""
return self.__state_vector[0] is not self.__State.null_state
def is_final(self):
"""Checks if the statemachine is final.
Always returns 'false' since this state machine can never become final.
"""
return False
def is_state_active(self, state):
"""Checks if the state is currently active.
"""
s = state
if s == self.__State.main_region_off:
return self.__state_vector[0] == self.__State.main_region_off
if s == self.__State.main_region_ready:
return (self.__state_vector[0] >= self.__State.main_region_ready)\
and (self.__state_vector[0] <= self.__State.main_region_ready_ready_cooling)
if s == self.__State.main_region_ready_ready_warming:
return self.__state_vector[0] == self.__State.main_region_ready_ready_warming
if s == self.__State.main_region_ready_ready_cooling:
return self.__state_vector[0] == self.__State.main_region_ready_ready_cooling
if s == self.__State.main_region_warmup:
return self.__state_vector[0] == self.__State.main_region_warmup
if s == self.__State.main_region_cooldown:
return self.__state_vector[0] == self.__State.main_region_cooldown
return False
def raise_set_target_temp(self, value):
"""Raise method for event set_target_temp.
"""
self.set_target_temp = True
self.set_target_temp_value = value
def __entry_action_main_region_ready(self):
"""Entry action for state 'Ready'..
"""
self.ready_observable.next()
def __entry_action_main_region_ready_ready_warming(self):
"""Entry action for state 'Warming'..
"""
self.operation_callback.heat_on()
def __entry_action_main_region_ready_ready_cooling(self):
"""Entry action for state 'Cooling'..
"""
self.operation_callback.heat_off()
def __entry_action_main_region_warmup(self):
"""Entry action for state 'Warmup'..
"""
self.operation_callback.heat_on()
def __entry_action_main_region_cooldown(self):
"""Entry action for state 'Cooldown'..
"""
self.operation_callback.heat_off()
def __enter_sequence_main_region_off_default(self):
"""'default' enter sequence for state Off.
"""
self.__state_vector[0] = self.State.main_region_off
self.__state_conf_vector_changed = True
def __enter_sequence_main_region_ready_default(self):
"""'default' enter sequence for state Ready.
"""
self.__entry_action_main_region_ready()
self.__enter_sequence_main_region_ready_ready_default()
def __enter_sequence_main_region_ready_ready_warming_default(self):
"""'default' enter sequence for state Warming.
"""
self.__entry_action_main_region_ready_ready_warming()
self.__state_vector[0] = self.State.main_region_ready_ready_warming
self.__state_conf_vector_changed = True
def __enter_sequence_main_region_ready_ready_cooling_default(self):
"""'default' enter sequence for state Cooling.
"""
self.__entry_action_main_region_ready_ready_cooling()
self.__state_vector[0] = self.State.main_region_ready_ready_cooling
self.__state_conf_vector_changed = True
def __enter_sequence_main_region_warmup_default(self):
"""'default' enter sequence for state Warmup.
"""
self.__entry_action_main_region_warmup()
self.__state_vector[0] = self.State.main_region_warmup
self.__state_conf_vector_changed = True
def __enter_sequence_main_region_cooldown_default(self):
"""'default' enter sequence for state Cooldown.
"""
self.__entry_action_main_region_cooldown()
self.__state_vector[0] = self.State.main_region_cooldown
self.__state_conf_vector_changed = True
def __enter_sequence_main_region_default(self):
"""'default' enter sequence for region main region.
"""
self.__react_main_region__entry_default()
def __enter_sequence_main_region_ready_ready_default(self):
"""'default' enter sequence for region ready.
"""
self.__react_main_region_ready_ready__entry_default()
def __exit_sequence_main_region_off(self):
"""Default exit sequence for state Off.
"""
self.__state_vector[0] = self.State.null_state
def __exit_sequence_main_region_ready(self):
"""Default exit sequence for state Ready.
"""
self.__exit_sequence_main_region_ready_ready()
def __exit_sequence_main_region_ready_ready_warming(self):
"""Default exit sequence for state Warming.
"""
self.__state_vector[0] = self.State.null_state
def __exit_sequence_main_region_ready_ready_cooling(self):
"""Default exit sequence for state Cooling.
"""
self.__state_vector[0] = self.State.null_state
def __exit_sequence_main_region_warmup(self):
"""Default exit sequence for state Warmup.
"""
self.__state_vector[0] = self.State.null_state
def __exit_sequence_main_region_cooldown(self):
"""Default exit sequence for state Cooldown.
"""
self.__state_vector[0] = self.State.null_state
def __exit_sequence_main_region(self):
"""Default exit sequence for region main region.
"""
state = self.__state_vector[0]
if state == self.State.main_region_off:
self.__exit_sequence_main_region_off()
elif state == self.State.main_region_ready_ready_warming:
self.__exit_sequence_main_region_ready_ready_warming()
elif state == self.State.main_region_ready_ready_cooling:
self.__exit_sequence_main_region_ready_ready_cooling()
elif state == self.State.main_region_warmup:
self.__exit_sequence_main_region_warmup()
elif state == self.State.main_region_cooldown:
self.__exit_sequence_main_region_cooldown()
def __exit_sequence_main_region_ready_ready(self):
"""Default exit sequence for region ready.
"""
state = self.__state_vector[0]
if state == self.State.main_region_ready_ready_warming:
self.__exit_sequence_main_region_ready_ready_warming()
elif state == self.State.main_region_ready_ready_cooling:
self.__exit_sequence_main_region_ready_ready_cooling()
def __react_main_region__entry_default(self):
"""Default react sequence for initial entry .
"""
self.__enter_sequence_main_region_off_default()
def __react_main_region_ready_ready__entry_default(self):
"""Default react sequence for initial entry .
"""
self.__enter_sequence_main_region_ready_ready_cooling_default()
def __react(self, transitioned_before):
"""Implementation of __react function.
"""
if self.__current.iface.set_target_temp:
self.target_temp = self.__current.iface.set_target_temp_value
return transitioned_before
def __main_region_off_react(self, transitioned_before):
"""Implementation of __main_region_off_react function.
"""
transitioned_after = transitioned_before
transitioned_after = self.__react(transitioned_before)
if transitioned_after < 0:
if (self.__current.iface.set_target_temp) and (self.target_temp > 0):
self.__exit_sequence_main_region_off()
self.__enter_sequence_main_region_warmup_default()
transitioned_after = 0
return transitioned_after
def __main_region_ready_react(self, transitioned_before):
"""Implementation of __main_region_ready_react function.
"""
transitioned_after = transitioned_before
transitioned_after = self.__react(transitioned_before)
if transitioned_after < 0:
if (self.__current.iface.set_target_temp) and (self.operation_callback.get_temp() > self.target_temp):
self.__exit_sequence_main_region_ready()
self.__enter_sequence_main_region_cooldown_default()
transitioned_after = 0
elif (self.__current.iface.set_target_temp) and (self.operation_callback.get_temp() < self.target_temp):
self.__exit_sequence_main_region_ready()
self.__enter_sequence_main_region_warmup_default()
transitioned_after = 0
return transitioned_after
def __main_region_ready_ready_warming_react(self, transitioned_before):
"""Implementation of __main_region_ready_ready_warming_react function.
"""
transitioned_after = transitioned_before
transitioned_after = self.__main_region_ready_react(transitioned_before)
if transitioned_after < 0:
if self.operation_callback.get_temp() > (self.target_temp + self.__threshold):
self.__exit_sequence_main_region_ready_ready_warming()
self.__enter_sequence_main_region_ready_ready_cooling_default()
transitioned_after = 0
return transitioned_after
def __main_region_ready_ready_cooling_react(self, transitioned_before):
"""Implementation of __main_region_ready_ready_cooling_react function.
"""
transitioned_after = transitioned_before
transitioned_after = self.__main_region_ready_react(transitioned_before)
if transitioned_after < 0:
if self.operation_callback.get_temp() < (self.target_temp - self.__threshold):
self.__exit_sequence_main_region_ready_ready_cooling()
self.__enter_sequence_main_region_ready_ready_warming_default()
transitioned_after = 0
return transitioned_after
def __main_region_warmup_react(self, transitioned_before):
"""Implementation of __main_region_warmup_react function.
"""
transitioned_after = transitioned_before
transitioned_after = self.__react(transitioned_before)
if transitioned_after < 0:
if self.operation_callback.get_temp() > self.target_temp:
self.__exit_sequence_main_region_warmup()
self.__enter_sequence_main_region_ready_default()
transitioned_after = 0
elif (self.__current.iface.set_target_temp) and (self.operation_callback.get_temp() > self.target_temp):
self.__exit_sequence_main_region_warmup()
self.__enter_sequence_main_region_cooldown_default()
transitioned_after = 0
return transitioned_after
def __main_region_cooldown_react(self, transitioned_before):
"""Implementation of __main_region_cooldown_react function.
"""
transitioned_after = transitioned_before
transitioned_after = self.__react(transitioned_before)
if transitioned_after < 0:
if self.operation_callback.get_temp() <= self.target_temp and self.target_temp <= 0:
self.__exit_sequence_main_region_cooldown()
self.off_observable.next()
self.__enter_sequence_main_region_off_default()
transitioned_after = 0
elif self.operation_callback.get_temp() < self.target_temp:
self.__exit_sequence_main_region_cooldown()
self.__enter_sequence_main_region_ready_default()
transitioned_after = 0
elif (self.__current.iface.set_target_temp) and (self.operation_callback.get_temp() < self.target_temp):
self.__exit_sequence_main_region_cooldown()
self.__enter_sequence_main_region_warmup_default()
transitioned_after = 0
return transitioned_after
def __swap_in_events(self):
"""Implementation of __swap_in_events function.
"""
self.__current.iface.set_target_temp = self.set_target_temp
self.__current.iface.set_target_temp_value = self.set_target_temp_value
self.set_target_temp = False
def __clear_in_events(self):
"""Implementation of __clear_in_events function.
"""
self.set_target_temp = False
def __micro_step(self):
"""Implementation of __micro_step function.
"""
state = self.__state_vector[0]
if state == self.State.main_region_off:
self.__main_region_off_react(-1)
elif state == self.State.main_region_ready_ready_warming:
self.__main_region_ready_ready_warming_react(-1)
elif state == self.State.main_region_ready_ready_cooling:
self.__main_region_ready_ready_cooling_react(-1)
elif state == self.State.main_region_warmup:
self.__main_region_warmup_react(-1)
elif state == self.State.main_region_cooldown:
self.__main_region_cooldown_react(-1)
def run_cycle(self):
"""Implementation of run_cycle function.
"""
if self.operation_callback is None:
raise ValueError("Operation callback must be set.")
if self.__is_executing:
return
self.__is_executing = True
self.__swap_in_events()
self.__micro_step()
self.__is_executing = False
def enter(self):
"""Implementation of enter function.
"""
if self.operation_callback is None:
raise ValueError("Operation callback must be set.")
if self.__is_executing:
return
self.__is_executing = True
self.__enter_sequence_main_region_default()
self.__is_executing = False
def exit(self):
"""Implementation of exit function.
"""
if self.__is_executing:
return
self.__is_executing = True
self.__exit_sequence_main_region()
self.__is_executing = False