Python: module FSM

FSM | index /home/noah/pexpect/trunk/pexpect/FSM.py

`This module implements a Finite State Machine (FSM). In addition to state
this FSM also maintains a user defined "memory". So this FSM can be used as a
Push-down Automata (PDA) since a PDA is a FSM + memory.
The following describes how the FSM works, but you will probably also need to
see the example function to understand how the FSM is used in practice.
You define an FSM by building tables of transitions. For a given input symbol
the process() method uses these tables to decide what action to call and what
the next state will be. The FSM has a table of transitions that associate:
(input_symbol, current_state) --> (action, next_state)
Where "action" is a function you define. The symbols and states can be any
objects. You use the add_transition() and add_transition_list() methods to add
to the transition table. The FSM also has a table of transitions that
associate:
(current_state) --> (action, next_state)
You use the add_transition_any() method to add to this transition table. The
FSM also has one default transition that is not associated with any specific
input_symbol or state. You use the set_default_transition() method to set the
default transition.
When an action function is called it is passed a reference to the FSM. The
action function may then access attributes of the FSM such as input_symbol,
current_state, or "memory". The "memory" attribute can be any object that you
want to pass along to the action functions. It is not used by the FSM itself.
For parsing you would typically pass a list to be used as a stack.
The processing sequence is as follows. The process() method is given an
input_symbol to process. The FSM will search the table of transitions that
associate:
(input_symbol, current_state) --> (action, next_state)
If the pair (input_symbol, current_state) is found then process() will call the
associated action function and then set the current state to the next_state.
If the FSM cannot find a match for (input_symbol, current_state) it will then
search the table of transitions that associate:
(current_state) --> (action, next_state)
If the current_state is found then the process() method will call the
associated action function and then set the current state to the next_state.
Notice that this table lacks an input_symbol. It lets you define transitions
for a current_state and ANY input_symbol. Hence, it is called the "any" table.
Remember, it is always checked after first searching the table for a specific
(input_symbol, current_state).
For the case where the FSM did not match either of the previous two cases the
FSM will try to use the default transition. If the default transition is
defined then the process() method will call the associated action function and
then set the current state to the next_state. This lets you define a default
transition as a catch-all case. You can think of it as an exception handler.
There can be only one default transition.
Finally, if none of the previous cases are defined for an input_symbol and
current_state then the FSM will raise an exception. This may be desirable, but
you can always prevent this just by defining a default transition.
Noah Spurrier 20020822`

Modules
`` |  |
optparse os | string sys | time traceback |

Classes
`` |  | FSM exceptions.Exception(exceptions.BaseException) ExceptionFSM
class ExceptionFSM(exceptions.Exception)
`` | `This is the FSM Exception class.`
| Method resolution order: ExceptionFSM exceptions.Exception exceptions.BaseException __builtin__.object Methods defined here: __init__(self, value) __str__(self) Data descriptors defined here: __weakref__ `list of weak references to the object (if defined)` Data and other attributes inherited from exceptions.Exception: __new__ = <built-in method __new__ of type object at 0x81400e0> `T.__new__(S, ...) -> a new object with type S, a subtype of T` Methods inherited from exceptions.BaseException: __delattr__(...) `x.__delattr__('name') <==> del x.name` __getattribute__(...) `x.__getattribute__('name') <==> x.name` __getitem__(...) `x.__getitem__(y) <==> x[y]` __getslice__(...) `x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported.` __reduce__(...) __repr__(...) `x.__repr__() <==> repr(x)` __setattr__(...) `x.__setattr__('name', value) <==> x.name = value` __setstate__(...) Data descriptors inherited from exceptions.BaseException: __dict__ args message `exception message`
class FSM
`` | `This is a Finite State Machine (FSM).`
| Methods defined here: __init__(self, initial_state, memory=None) `This creates the FSM. You set the initial state here. The "memory" attribute is any object that you want to pass along to the action functions. It is not used by the FSM. For parsing you would typically pass a list to be used as a stack.` add_transition(self, input_symbol, state, action=None, next_state=None) `This adds a transition that associates: (input_symbol, current_state) --> (action, next_state) The action may be set to None in which case the process() method will ignore the action and only set the next_state. The next_state may be set to None in which case the current state will be unchanged. You can also set transitions for a list of symbols by using add_transition_list().` add_transition_any(self, state, action=None, next_state=None) `This adds a transition that associates: (current_state) --> (action, next_state) That is, any input symbol will match the current state. The process() method checks the "any" state associations after it first checks for an exact match of (input_symbol, current_state). The action may be set to None in which case the process() method will ignore the action and only set the next_state. The next_state may be set to None in which case the current state will be unchanged.` add_transition_list(self, list_input_symbols, state, action=None, next_state=None) `This adds the same transition for a list of input symbols. You can pass a list or a string. Note that it is handy to use string.digits, string.whitespace, string.letters, etc. to add transitions that match character classes. The action may be set to None in which case the process() method will ignore the action and only set the next_state. The next_state may be set to None in which case the current state will be unchanged.` get_transition(self, input_symbol, state) `This returns (action, next state) given an input_symbol and state. This does not modify the FSM state, so calling this method has no side effects. Normally you do not call this method directly. It is called by process(). The sequence of steps to check for a defined transition goes from the most specific to the least specific. 1. Check state_transitions[] that match exactly the tuple, (input_symbol, state) 2. Check state_transitions_any[] that match (state) In other words, match a specific state and ANY input_symbol. 3. Check if the default_transition is defined. This catches any input_symbol and any state. This is a handler for errors, undefined states, or defaults. 4. No transition was defined. If we get here then raise an exception.` process(self, input_symbol) `This is the main method that you call to process input. This may cause the FSM to change state and call an action. This method calls get_transition() to find the action and next_state associated with the input_symbol and current_state. If the action is None then the action is not called and only the current state is changed. This method processes one complete input symbol. You can process a list of symbols (or a string) by calling process_list().` process_list(self, input_symbols) `This takes a list and sends each element to process(). The list may be a string or any iterable object.` reset(self) `This sets the current_state to the initial_state and sets input_symbol to None. The initial state was set by the constructor __init__().` set_default_transition(self, action, next_state) `This sets the default transition. This defines an action and next_state if the FSM cannot find the input symbol and the current state in the transition list and if the FSM cannot find the current_state in the transition_any list. This is useful as a final fall-through state for catching errors and undefined states. The default transition can be removed by setting the attribute default_transition to None.`

Functions
`` |  | BeginBuildNumber(fsm) BuildNumber(fsm) DoEqual(fsm) DoOperator(fsm) EndBuildNumber(fsm) Error(fsm) main() `This is where the example starts and the FSM state transitions are defined. Note that states are strings (such as 'INIT'). This is not necessary, but it makes the example easier to read.`