-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.py
executable file
·45 lines (36 loc) · 1.28 KB
/
fsm.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
#!/usr/bin/env python
import memory
class FSM:
def __init__(self):
self.states = {}
self.state = None
self.memory = memory.memory()
def start(self, state):
self.state = state
def add(self, state, signal, new_state, action = ""):
try:
self.states[state][signal] = (new_state, action)
except KeyError:
self.states[state] = {}
self.states[state][signal] = (new_state, action)
def execute(self, signal, elem = None):
if self.state not in self.states:
raise RuntimeError("No state")
print "-----------------------"
print self.debug(True)
print "Signal: ", signal, ", Current State: ", self.state
state = self.states[self.state]
if signal in state:
new_state, action = state[signal]
self.memory.operation(action,elem)
self.state = new_state
print "New state: ", self.state
print "-----------------------"
return action.split(',')[0]
else:
raise RuntimeWarning("No edge")
def debug(self,onlyMemory = False):
if not onlyMemory:
for state in self.states:
print state
self.memory.debug()