-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathactions.py
executable file
·60 lines (48 loc) · 1.31 KB
/
actions.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
#!/usr/bin/env python3
import time
from xstate import Machine
# Trafic light example
# green -> yellow -> red -> green ..
timing = 2
def enterGreen():
print("\tENTER_GREEN callback")
def exitGreen():
print("\tEXIT_GREEN callback")
# fmt: off
lights = Machine(
{
"id": "lights",
"initial": "green",
"states": {
"green": {
"on": {"TIMER": "yellow"},
"entry": [{"type": "enterGreen"}],
"exit": [{"type": "exitGreen"}],
},
"yellow": {
"on": {"TIMER": "red"},
"entry": [{"type": "enterYellow"}]
},
"red": {
"on": {"TIMER": "green"},
"entry": [lambda: print("\tINLINE callback")],
},
},
},
actions={
# action implementations
"enterGreen": enterGreen,
"exitGreen": exitGreen,
"enterYellow": lambda: print("\tENTER_YELLOW callback"),
},
)
# fmt: on
if __name__ == "__main__":
state = lights.initial_state
for i in range(10):
# execute all the actions (before/exit states)
for action in state.actions:
action()
print("VALUE: {}".format(state.value))
time.sleep(timing)
state = lights.transition(state, "TIMER")