-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatemachine.py
More file actions
71 lines (60 loc) · 2.37 KB
/
statemachine.py
File metadata and controls
71 lines (60 loc) · 2.37 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
#!/usr/env python2.7
# -*- coding: utf-8 *-*
class StateMachine(object):
def __init__(self):
"""Initialize the Machine with the Startstate."""
self.currentState = None
self.startState = None
self.endStates = {}
self.states = {}
def addState(self, state, isStart=False, isEnd=False):
"""Add a new State to states list."""
self.states[state.name] = state
if isStart:
self.startState = state
if isEnd:
self.endStates[state.name] = state
def setStart(self):
"""Prepare everything."""
if not self.startState:
raise Exception("Startstate not set!")
if not self.endStates:
raise Exception("Endstate not set!")
self.currentState = self.startState
def render(self, input):
"""Check input for the current state.
If input is valid,
- process State with input
"""
newState, data = currentState.process(request)
self.currentState = self.states[newState](data)
def run(self):
print "Starting MyMachine: \n"
while True:
input = raw_input("What you want: \n{state}".format(state=self.currentState.render()))
print "Got input: ", input, " for State: ", self.currentState
newState, data = self.currentState.process(input)
print "RETURNED: ", newState, data
if newState in self.endStates.keys():
print "Yiiiiihhhhaaaa, Ready!"
break
else:
self.currentState = self.states[newState]
self.currentState.render()
print "Bye"
if __name__ == "__main__":
start_state = StartState('start',
{'weiter':{'action':'do', 'callback':'state2', 'errback':'start'}
,'abbruch':{'action':'da', 'callback':'error', 'errback':'error'}})
state2 = State2('state2',
{'weiter':{'action':'do', 'callback':'success', 'errback':'state2'}
,'abbruch':{'action':'da', 'callback':'error', 'errback':'error'}})
success = Success('success')
error = ErrState('error')
sm = StateMachine()
sm.addState(start_state, isStart=True)
sm.addState(state2)
sm.addState(success, isEnd=True)
sm.addState(error, isEnd=True)
sm.setStart()
sm.run()