3 """This module implements a Finite State Machine (FSM). In addition to state
4 this FSM also maintains a user defined "memory". So this FSM can be used as a
5 Push-down Automata (PDA) since a PDA is a FSM + memory.
7 The following describes how the FSM works, but you will probably also need to
8 see the example function to understand how the FSM is used in practice.
10 You define an FSM by building tables of transitions. For a given input symbol
11 the process() method uses these tables to decide what action to call and what
12 the next state will be. The FSM has a table of transitions that associate:
14 (input_symbol, current_state) --> (action, next_state)
16 Where "action" is a function you define. The symbols and states can be any
17 objects. You use the add_transition() and add_transition_list() methods to add
18 to the transition table. The FSM also has a table of transitions that
21 (current_state) --> (action, next_state)
23 You use the add_transition_any() method to add to this transition table. The
24 FSM also has one default transition that is not associated with any specific
25 input_symbol or state. You use the set_default_transition() method to set the
28 When an action function is called it is passed a reference to the FSM. The
29 action function may then access attributes of the FSM such as input_symbol,
30 current_state, or "memory". The "memory" attribute can be any object that you
31 want to pass along to the action functions. It is not used by the FSM itself.
32 For parsing you would typically pass a list to be used as a stack.
34 The processing sequence is as follows. The process() method is given an
35 input_symbol to process. The FSM will search the table of transitions that
38 (input_symbol, current_state) --> (action, next_state)
40 If the pair (input_symbol, current_state) is found then process() will call the
41 associated action function and then set the current state to the next_state.
43 If the FSM cannot find a match for (input_symbol, current_state) it will then
44 search the table of transitions that associate:
46 (current_state) --> (action, next_state)
48 If the current_state is found then the process() method will call the
49 associated action function and then set the current state to the next_state.
50 Notice that this table lacks an input_symbol. It lets you define transitions
51 for a current_state and ANY input_symbol. Hence, it is called the "any" table.
52 Remember, it is always checked after first searching the table for a specific
53 (input_symbol, current_state).
55 For the case where the FSM did not match either of the previous two cases the
56 FSM will try to use the default transition. If the default transition is
57 defined then the process() method will call the associated action function and
58 then set the current state to the next_state. This lets you define a default
59 transition as a catch-all case. You can think of it as an exception handler.
60 There can be only one default transition.
62 Finally, if none of the previous cases are defined for an input_symbol and
63 current_state then the FSM will raise an exception. This may be desirable, but
64 you can always prevent this just by defining a default transition.
66 Noah Spurrier 20020822
70 class ExceptionFSM(Exception):
72 """This is the FSM Exception class."""
74 def __init__(self, value):
78 return repr(self.value)
83 """This is a Finite State Machine (FSM).
86 def __init__(self, initial_state, memory=None):
87 """This creates the FSM. You set the initial state here. The "memory"
88 attribute is any object that you want to pass along to the action
89 functions. It is not used by the FSM. For parsing you would typically
90 pass a list to be used as a stack. """
92 # Map (input_symbol, current_state) --> (action, next_state).
93 self.state_transitions = {}
94 # Map (current_state) --> (action, next_state).
95 self.state_transitions_any = {}
96 self.default_transition = None
98 self.input_symbol = None
99 self.initial_state = initial_state
100 self.current_state = self.initial_state
101 self.next_state = None
106 """This sets the current_state to the initial_state and sets
107 input_symbol to None. The initial state was set by the constructor
110 self.current_state = self.initial_state
111 self.input_symbol = None
119 """This adds a transition that associates:
121 (input_symbol, current_state) --> (action, next_state)
123 The action may be set to None in which case the process() method will
124 ignore the action and only set the next_state. The next_state may be
125 set to None in which case the current state will be unchanged.
127 You can also set transitions for a list of symbols by using
128 add_transition_list(). """
130 if next_state is None:
132 self.state_transitions[(input_symbol, state)] = (action, next_state)
134 def add_transition_list(
140 """This adds the same transition for a list of input symbols.
141 You can pass a list or a string. Note that it is handy to use
142 string.digits, string.whitespace, string.letters, etc. to add
143 transitions that match character classes.
145 The action may be set to None in which case the process() method will
146 ignore the action and only set the next_state. The next_state may be
147 set to None in which case the current state will be unchanged. """
149 if next_state is None:
151 for input_symbol in list_input_symbols:
152 self.add_transition(input_symbol, state, action, next_state)
154 def add_transition_any(self, state, action=None, next_state=None):
155 """This adds a transition that associates:
157 (current_state) --> (action, next_state)
159 That is, any input symbol will match the current state.
160 The process() method checks the "any" state associations after it first
161 checks for an exact match of (input_symbol, current_state).
163 The action may be set to None in which case the process() method will
164 ignore the action and only set the next_state. The next_state may be
165 set to None in which case the current state will be unchanged. """
167 if next_state is None:
169 self.state_transitions_any[state] = (action, next_state)
171 def set_default_transition(self, action, next_state):
172 """This sets the default transition. This defines an action and
173 next_state if the FSM cannot find the input symbol and the current
174 state in the transition list and if the FSM cannot find the
175 current_state in the transition_any list. This is useful as a final
176 fall-through state for catching errors and undefined states.
178 The default transition can be removed by setting the attribute
179 default_transition to None. """
181 self.default_transition = (action, next_state)
183 def get_transition(self, input_symbol, state):
184 """This returns (action, next state) given an input_symbol and state.
185 This does not modify the FSM state, so calling this method has no side
186 effects. Normally you do not call this method directly. It is called by
189 The sequence of steps to check for a defined transition goes from the
190 most specific to the least specific.
192 1. Check state_transitions[] that match exactly the tuple,
193 (input_symbol, state)
195 2. Check state_transitions_any[] that match (state)
196 In other words, match a specific state and ANY input_symbol.
198 3. Check if the default_transition is defined.
199 This catches any input_symbol and any state.
200 This is a handler for errors, undefined states, or defaults.
202 4. No transition was defined. If we get here then raise an exception.
205 if (input_symbol, state) in self.state_transitions:
206 return self.state_transitions[(input_symbol, state)]
207 elif state in self.state_transitions_any:
208 return self.state_transitions_any[state]
209 elif self.default_transition is not None:
210 return self.default_transition
212 raise ExceptionFSM('Transition is undefined: (%s, %s).' %
213 (str(input_symbol), str(state)))
215 def process(self, input_symbol):
216 """This is the main method that you call to process input. This may
217 cause the FSM to change state and call an action. This method calls
218 get_transition() to find the action and next_state associated with the
219 input_symbol and current_state. If the action is None then the action
220 is not called and only the current state is changed. This method
221 processes one complete input symbol. You can process a list of symbols
222 (or a string) by calling process_list(). """
224 self.input_symbol = input_symbol
225 (self.action, self.next_state) = self.get_transition(
226 self.input_symbol, self.current_state)
227 if self.action is not None:
229 self.current_state = self.next_state
230 self.next_state = None
232 def process_list(self, input_symbols):
233 """This takes a list and sends each element to process(). The list may
234 be a string or any iterable object. """
236 for s in input_symbols:
239 ##############################################################################
240 # The following is an example that demonstrates the use of the FSM class to
241 # process an RPN expression. Run this module from the command line. You will
242 # get a prompt > for input. Enter an RPN Expression. Numbers may be integers.
243 # Operators are * / + - Use the = sign to evaluate and print the expression.
246 # 167 3 2 2 * * * 1 - =
251 ##############################################################################
261 # These define the actions.
262 # Note that "memory" is a list being used as a stack.
266 def BeginBuildNumber(fsm):
267 fsm.memory.append(fsm.input_symbol)
270 def BuildNumber(fsm):
272 s = s + fsm.input_symbol
276 def EndBuildNumber(fsm):
278 fsm.memory.append(int(s))
282 ar = fsm.memory.pop()
283 al = fsm.memory.pop()
284 if fsm.input_symbol == '+':
285 fsm.memory.append(al + ar)
286 elif fsm.input_symbol == '-':
287 fsm.memory.append(al - ar)
288 elif fsm.input_symbol == '*':
289 fsm.memory.append(al * ar)
290 elif fsm.input_symbol == '/':
291 fsm.memory.append(al / ar)
295 print str(fsm.memory.pop())
299 print 'That does not compute.'
300 print str(fsm.input_symbol)
304 """This is where the example starts and the FSM state transitions are
305 defined. Note that states are strings (such as 'INIT'). This is not
306 necessary, but it makes the example easier to read. """
308 f = FSM('INIT', []) # "memory" will be used as a stack.
309 f.set_default_transition(Error, 'INIT')
310 f.add_transition_any('INIT', None, 'INIT')
311 f.add_transition('=', 'INIT', DoEqual, 'INIT')
312 f.add_transition_list(
317 f.add_transition_list(
322 f.add_transition_list(
327 f.add_transition_list('+-*/', 'INIT', DoOperator, 'INIT')
330 print 'Enter an RPN Expression.'
331 print 'Numbers may be integers. Operators are * / + -'
332 print 'Use the = sign to evaluate and print the expression.'
333 print 'For example: '
334 print ' 167 3 2 2 * * * 1 - ='
335 inputstr = raw_input('> ')
336 f.process_list(inputstr)
338 if __name__ == '__main__':
340 start_time = time.time()
341 parser = optparse.OptionParser(
342 formatter=optparse.TitledHelpFormatter(),
343 usage=globals()['__doc__'],
344 version='$Id: FSM.py 490 2007-12-07 15:46:24Z noah $')
350 help='verbose output')
351 (options, args) = parser.parse_args()
358 print 'TOTAL TIME IN MINUTES:',
360 print (time.time() - start_time) / 60.0
362 except KeyboardInterrupt as e: # Ctrl-C
364 except SystemExit as e: # sys.exit()
366 except Exception as e:
367 print 'ERROR, UNEXPECTED EXCEPTION'
369 traceback.print_exc()