-
Notifications
You must be signed in to change notification settings - Fork 12
/
turnstile.py
89 lines (71 loc) · 2.22 KB
/
turnstile.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
# This example is marked up for piecewise inclusion in basics.rst. All code
# relevant to machinist must fall between inclusion markers (so, for example,
# __future__ imports may be outside such markers; also this is required by
# Python syntax). If you damage the markers the documentation will silently
# break. So try not to do that.
from __future__ import print_function
# begin setup
from twisted.python.constants import Names, NamedConstant
class Input(Names):
FARE_PAID = NamedConstant()
ARM_UNLOCKED = NamedConstant()
ARM_TURNED = NamedConstant()
ARM_LOCKED = NamedConstant()
class Output(Names):
ENGAGE_LOCK = NamedConstant()
DISENGAGE_LOCK = NamedConstant()
class State(Names):
LOCKED = NamedConstant()
UNLOCKED = NamedConstant()
ACTIVE = NamedConstant()
# end setup
# begin table def
from machinist import TransitionTable
table = TransitionTable()
# end table def
# begin first transition
table = table.addTransition(
State.LOCKED, Input.FARE_PAID, [Output.DISENGAGE_LOCK], State.ACTIVE)
# end first transition
# begin second transition
table = table.addTransition(
State.UNLOCKED, Input.ARM_TURNED, [Output.ENGAGE_LOCK], State.ACTIVE)
# end second transition
# begin last transitions
table = table.addTransitions(
State.ACTIVE, {
Input.ARM_UNLOCKED: ([], State.UNLOCKED),
Input.ARM_LOCKED: ([], State.LOCKED),
})
# end last transitions
# begin outputer
from machinist import MethodSuffixOutputer
class Outputer(object):
def output_ENGAGE_LOCK(self, engage):
print("Engaging the lock.")
def output_DISENGAGE_LOCK(self, disengage):
print("Disengaging the lock.")
outputer = MethodSuffixOutputer(Outputer())
# end outputer
# begin construct
from machinist import constructFiniteStateMachine
turnstile = constructFiniteStateMachine(
inputs=Input,
outputs=Output,
states=State,
table=table,
initial=State.LOCKED,
richInputs=[],
inputContext={},
world=outputer,
)
# end construct
# begin inputs
def cycle():
turnstile.receive(Input.FARE_PAID)
turnstile.receive(Input.ARM_UNLOCKED)
turnstile.receive(Input.ARM_TURNED)
turnstile.receive(Input.ARM_LOCKED)
# end inputs
if __name__ == '__main__':
cycle()