Repository navigation
Expand file tree
/
Copy pathstatemachine.py
More file actions
198 lines (155 loc) · 6.07 KB
/
Copy pathstatemachine.py
File metadata and controls
198 lines (155 loc) · 6.07 KB
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
from flow_exceptions import WorkflowInitializationError
# https://bitbucket.org/BruceEckel/python-3-patterns-idioms/src/b1be62c8c0e547f271704adbbbfcb2f6d1caf5a0/code/StateMachine/vendingmachine/VendingMachine.py?at=default
# https://python-3-patterns-idioms-test.readthedocs.org/en/latest/StateMachine.html
# http://stackoverflow.com/questions/226976/how-can-i-know-in-git-if-a-branch-has-been-already-merged-into-master
class State:
def __init__(self, name): self.name = name
def __str__(self): return self.name
State.quiescent = State("Quiesecent")
State.collecting = State("Collecting")
State.selecting = State("Selecting")
State.unavailable = State("Unavailable")
State.wantMore = State("Want More?")
State.noChange = State("Use Exact Change Only")
State.makesChange = State("Machine makes change")
class HasChange:
def __init__(self, name): self.name = name
def __str__(self): return self.name
HasChange.yes = HasChange("Has change")
HasChange.no = HasChange("Cannot make change")
# Inputs to a state machine
class Input: pass
# Condition function object for state machine
class Condition:
def condition(input):
assert 0, "condition() not implemented"
class WorkflowStep:
def __init__(self, stepName, stepCondition, stepTransition, stepSuccessNext, stepFailNext):
self.name = stepName
self.condition = stepCondition
self.transition = stepTransition
self.successNext = stepSuccessNext
self.failNext = stepFailNext
def __str__(self):
return self.name
# A table-driven state machine
class StateMachine:
def __init__(self, initialState, tranTable):
self.state = initialState
self.transitionTable = tranTable
#needs to be a recursive call
def nextState(self, input):
for step in self.transitionTable:
if step.name == 'start':
if step.stepCondition is not None:
step.stepCondition.condition(input)
if step.stepTransition is not None:
step.stepTransition.transition(input)
self.nextState('next')
return
"""
state
transitions (options)
sucesss state
fail state
{(CurrentState, InputA) : (ConditionA, TransitionA, NextA),
(CurrentState, InputB) : (ConditionB, TransitionB, NextB),
(CurrentState, InputC) : (ConditionC, TransitionC, NextC),
...
}
def goforit(self):
buildTable(Object[][][]{
::State.quiescent,
:Money.class, null,showTotal, State.collecting,
::State.collecting,
:Quit.quit, null,returnChange, State.quiescent,
:Money.class, null,showTotal, State.collecting,
:FirstDigit.class, null,showDigit, State.selecting,
::State.selecting,
:Quit.quit, null,returnChange, State.quiescent,
:SecondDigit.class, notEnough,clearSelection, State.collecting,
:SecondDigit.class, itemNotAvailable,clearSelection, State.unavailable,
:SecondDigit.class, itemAvailable,dispense, State.wantMore,
::State.unavailable,
:Quit.quit, null,returnChange, State.quiescent,
:FirstDigit.class, null,showDigit, State.selecting,
::State.wantMore,
:Quit.quit, null,returnChange, State.quiescent,
:FirstDigit.class, null,showDigit, State.selecting,
class StateMachine:
def __init__(self):
self.handlers = {}
self.endStates = []
self.startState = None
def add_state(self, name, handler, end_state=0):
self.handlers[name] = handler
if end_state:
self.endStates.append(name)
def set_start(self, name):
self.startState = name
def run(self, content):
if self.startState in self.handlers:
handler = self.handlers[self.startState]
else:
raise WorkflowInitializationError(".set_start() has to be called before .run()")
if not self.endStates:
raise WorkflowInitializationError("at least one state must be an end_state")
oldState = self.startState
while 1:
(newState, content) = handler(content, oldState)
if newState in self.endStates:
print("reached ", newState, "which is an end state")
break
else:
handler = self.handlers[newState]
oldState = newState
from statemachine import StateMachine
positive_adjectives = ["great","super", "fun", "entertaining", "easy"]
negative_adjectives = ["boring", "difficult", "ugly", "bad"]
def transitions(txt, state):
splitted_txt = txt.split(None,1)
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"")
if state == "Start":
if word == "Python":
newState = "Python_state"
else:
newState = "error_state"
return (newState, txt)
elif state == "Python_state":
if word == "is":
newState = "is_state"
else:
newState = "error_state"
return (newState, txt)
elif state == "is_state":
if word == "not":
newState = "not_state"
elif word in positive_adjectives:
newState = "pos_state"
elif word in negative_adjectives:
newState = "neg_state"
else:
newState = "error_state"
return (newState, txt)
elif state == "not_state":
if word in positive_adjectives:
newState = "neg_state"
elif word in negative_adjectives:
newState = "pos_state"
else:
newState = "error_state"
return (newState, txt)
if __name__== "__main__":
m = StateMachine()
m.add_state("Start", transitions)
m.add_state("Python_state", transitions)
m.add_state("is_state", transitions)
m.add_state("not_state", transitions)
m.add_state("neg_state", None, end_state=1)
m.add_state("pos_state", None, end_state=1)
m.add_state("error_state", None, end_state=1)
m.set_start("Start")
m.run("Python is great")
m.run("Python is difficult")
m.run("Perl is ugly")
"""