-
Notifications
You must be signed in to change notification settings - Fork 1
/
statemachine.py
33 lines (29 loc) · 983 Bytes
/
statemachine.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
class StateMachine:
'''
Base class for the State machine.
'''
def __init__(self):
self.handlers = {}
self.startState = None
self.endStates = []
self.handler=None
def add_state(self, name, handler, end_state=0):
name = name.upper()
self.handlers[name] = handler
if end_state:
self.endStates.append(name)
def set_start(self, name):
self.startState = name.upper()
def runOneStep(self, cargo):
if(self.handler is None):
try:
self.handler = self.handlers[self.startState]
except:
raise InitializationError("must call .set_start() before .run()")
newState= self.handler(cargo)
if newState.upper() in self.endStates:
print("reached end state", newState)
else:
print("State: ", newState)
self.handler = self.handlers[newState.upper()]
return newState