Package PyDSTool :: Package Toolbox :: Module FSM :: Class FSM
[hide private]
[frames] | no frames]

Class FSM

source code

object --+
         |
        FSM
Known Subclasses:

This is a Finite State Machine (FSM).

Instance Methods [hide private]
 
__init__(self, initial_state, memory=None)
This creates the FSM.
source code
 
reset(self)
This sets the current_state to the initial_state and sets input_symbol to None.
source code
 
add_transition(self, input_symbol, state, action=None, next_state=None)
This adds a transition that associates:
source code
 
add_transition_list(self, list_input_symbols, state, action=None, next_state=None)
This adds the same transition for a list of input symbols.
source code
 
add_transition_any(self, state, action=None, next_state=None)
This adds a transition that associates:
source code
 
set_default_transition(self, action, next_state)
This sets the default transition.
source code
 
get_transition(self, input_symbol, state)
This returns (action, next state) given an input_symbol and state.
source code
 
process(self, input_symbol)
This is the main method that you call to process input.
source code
 
process_list(self, input_symbols)
This takes a list and sends each element to process().
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, initial_state, memory=None)
(Constructor)

source code 

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.

Overrides: object.__init__

reset(self)

source code 

This sets the current_state to the initial_state and sets input_symbol to None. The initial state was set by the constructor __init__().

add_transition(self, input_symbol, state, action=None, next_state=None)

source code 
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_list(self, list_input_symbols, state, action=None, next_state=None)

source code 

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.

add_transition_any(self, state, action=None, next_state=None)

source code 
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. 

set_default_transition(self, action, next_state)

source code 

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.

get_transition(self, input_symbol, state)

source code 
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)

source code 

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)

source code 

This takes a list and sends each element to process(). The list may be a string or any iterable object.