Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

FSM Class Reference

Inheritance diagram for FSM:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 __init__ (initial_state, something)
 reset ()
 add_transition (input_symbol, state, action, next_state)
 add_transition_list (list_input_symbols, state, action, next_state)
 add_transition_any (state, action, next_state)
 set_default_transition (action, next_state)
 get_transition (input_symbol, state)
 process (input_symbol)
 process_list (s)

Detailed Description

This is a Finite State Machine (FSM).

Definition at line 80 of file FSM.py.


Member Function Documentation

__init__ initial_state  ,
something 
 

This creates the FSM. You set the initial state here. The "something" 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 input_symbol  ,
state  ,
action  ,
next_state 
 

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.

You can also set transitions for a list of symbols by using add_transition_list().

add_transition_any state  ,
action  ,
next_state 
 

This adds a transition that associates (current_state) --> (action, next_state) The process() method checks these associations if it cannot first find a match of an (input_symbol, current_state).

add_transition_list list_input_symbols  ,
state  ,
action  ,
next_state 
 

This adds the same transition for lots of different 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.

get_transition input_symbol  ,
state 
 

This returns (action, next state) given an input_symbol and state. This leaves the FSM unchanged. This does not update the current state nor does it trigger the output action. Normally you do not call this method. 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 (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 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 input symbol. You can process a list of symbols (or a string) by calling process_list(). This method returns an uninterpreted status value used to indicate whether the state transition completed successfully.

process_list  ) 
 

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

reset  ) 
 

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 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 for catching errors and undefined states.

The default transition can be removed by setting the attribute default_transition to None.


The documentation for this class was generated from the following file:
Generated on Fri Jul 21 13:26:43 2006 for LATTE R04-12-00 by doxygen 1.4.3