Current File : //proc/self/root/usr/share/doc/pexpect-2.3/doc/FSM.html |
<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module FSM</title>
</head><body bgcolor="#f0f0f8">
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>FSM</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/noah/pexpect/trunk/pexpect/FSM.py">/home/noah/pexpect/trunk/pexpect/FSM.py</a></font></td></tr></table>
<p><tt>This module implements a Finite State Machine (<a href="#FSM">FSM</a>). In addition to state<br>
this <a href="#FSM">FSM</a> also maintains a user defined "memory". So this <a href="#FSM">FSM</a> can be used as a<br>
Push-down Automata (PDA) since a PDA is a <a href="#FSM">FSM</a> + memory.<br>
<br>
The following describes how the <a href="#FSM">FSM</a> works, but you will probably also need to<br>
see the example function to understand how the <a href="#FSM">FSM</a> is used in practice.<br>
<br>
You define an <a href="#FSM">FSM</a> by building tables of transitions. For a given input symbol<br>
the process() method uses these tables to decide what action to call and what<br>
the next state will be. The <a href="#FSM">FSM</a> has a table of transitions that associate:<br>
<br>
(input_symbol, current_state) --> (action, next_state)<br>
<br>
Where "action" is a function you define. The symbols and states can be any<br>
objects. You use the add_transition() and add_transition_list() methods to add<br>
to the transition table. The <a href="#FSM">FSM</a> also has a table of transitions that<br>
associate:<br>
<br>
(current_state) --> (action, next_state)<br>
<br>
You use the add_transition_any() method to add to this transition table. The<br>
<a href="#FSM">FSM</a> also has one default transition that is not associated with any specific<br>
input_symbol or state. You use the set_default_transition() method to set the<br>
default transition.<br>
<br>
When an action function is called it is passed a reference to the <a href="#FSM">FSM</a>. The<br>
action function may then access attributes of the <a href="#FSM">FSM</a> such as input_symbol,<br>
current_state, or "memory". The "memory" attribute can be any object that you<br>
want to pass along to the action functions. It is not used by the <a href="#FSM">FSM</a> itself.<br>
For parsing you would typically pass a list to be used as a stack.<br>
<br>
The processing sequence is as follows. The process() method is given an<br>
input_symbol to process. The <a href="#FSM">FSM</a> will search the table of transitions that<br>
associate:<br>
<br>
(input_symbol, current_state) --> (action, next_state)<br>
<br>
If the pair (input_symbol, current_state) is found then process() will call the<br>
associated action function and then set the current state to the next_state.<br>
<br>
If the <a href="#FSM">FSM</a> cannot find a match for (input_symbol, current_state) it will then<br>
search the table of transitions that associate:<br>
<br>
(current_state) --> (action, next_state)<br>
<br>
If the current_state is found then the process() method will call the<br>
associated action function and then set the current state to the next_state.<br>
Notice that this table lacks an input_symbol. It lets you define transitions<br>
for a current_state and ANY input_symbol. Hence, it is called the "any" table.<br>
Remember, it is always checked after first searching the table for a specific<br>
(input_symbol, current_state).<br>
<br>
For the case where the <a href="#FSM">FSM</a> did not match either of the previous two cases the<br>
<a href="#FSM">FSM</a> will try to use the default transition. If the default transition is<br>
defined then the process() method will call the associated action function and<br>
then set the current state to the next_state. This lets you define a default<br>
transition as a catch-all case. You can think of it as an exception handler.<br>
There can be only one default transition.<br>
<br>
Finally, if none of the previous cases are defined for an input_symbol and<br>
current_state then the <a href="#FSM">FSM</a> will raise an exception. This may be desirable, but<br>
you can always prevent this just by defining a default transition.<br>
<br>
Noah Spurrier 20020822</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#aa55cc">
<td colspan=3 valign=bottom> <br>
<font color="#fffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr>
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="optparse.html">optparse</a><br>
<a href="os.html">os</a><br>
</td><td width="25%" valign=top><a href="string.html">string</a><br>
<a href="sys.html">sys</a><br>
</td><td width="25%" valign=top><a href="time.html">time</a><br>
<a href="traceback.html">traceback</a><br>
</td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="FSM.html#FSM">FSM</a>
</font></dt><dt><font face="helvetica, arial"><a href="exceptions.html#Exception">exceptions.Exception</a>(<a href="exceptions.html#BaseException">exceptions.BaseException</a>)
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="FSM.html#ExceptionFSM">ExceptionFSM</a>
</font></dt></dl>
</dd>
</dl>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="ExceptionFSM">class <strong>ExceptionFSM</strong></a>(<a href="exceptions.html#Exception">exceptions.Exception</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt>This is the <a href="#FSM">FSM</a> <a href="exceptions.html#Exception">Exception</a> class.<br> </tt></td></tr>
<tr><td> </td>
<td width="100%"><dl><dt>Method resolution order:</dt>
<dd><a href="FSM.html#ExceptionFSM">ExceptionFSM</a></dd>
<dd><a href="exceptions.html#Exception">exceptions.Exception</a></dd>
<dd><a href="exceptions.html#BaseException">exceptions.BaseException</a></dd>
<dd><a href="__builtin__.html#object">__builtin__.object</a></dd>
</dl>
<hr>
Methods defined here:<br>
<dl><dt><a name="ExceptionFSM-__init__"><strong>__init__</strong></a>(self, value)</dt></dl>
<dl><dt><a name="ExceptionFSM-__str__"><strong>__str__</strong></a>(self)</dt></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list of weak references to the object (if defined)</tt></dd>
</dl>
<hr>
Data and other attributes inherited from <a href="exceptions.html#Exception">exceptions.Exception</a>:<br>
<dl><dt><strong>__new__</strong> = <built-in method __new__ of type object at 0x81400e0><dd><tt>T.<a href="#ExceptionFSM-__new__">__new__</a>(S, ...) -> a new object with type S, a subtype of T</tt></dl>
<hr>
Methods inherited from <a href="exceptions.html#BaseException">exceptions.BaseException</a>:<br>
<dl><dt><a name="ExceptionFSM-__delattr__"><strong>__delattr__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionFSM-__delattr__">__delattr__</a>('name') <==> del x.name</tt></dd></dl>
<dl><dt><a name="ExceptionFSM-__getattribute__"><strong>__getattribute__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionFSM-__getattribute__">__getattribute__</a>('name') <==> x.name</tt></dd></dl>
<dl><dt><a name="ExceptionFSM-__getitem__"><strong>__getitem__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionFSM-__getitem__">__getitem__</a>(y) <==> x[y]</tt></dd></dl>
<dl><dt><a name="ExceptionFSM-__getslice__"><strong>__getslice__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionFSM-__getslice__">__getslice__</a>(i, j) <==> x[i:j]<br>
<br>
Use of negative indices is not supported.</tt></dd></dl>
<dl><dt><a name="ExceptionFSM-__reduce__"><strong>__reduce__</strong></a>(...)</dt></dl>
<dl><dt><a name="ExceptionFSM-__repr__"><strong>__repr__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionFSM-__repr__">__repr__</a>() <==> repr(x)</tt></dd></dl>
<dl><dt><a name="ExceptionFSM-__setattr__"><strong>__setattr__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionFSM-__setattr__">__setattr__</a>('name', value) <==> x.name = value</tt></dd></dl>
<dl><dt><a name="ExceptionFSM-__setstate__"><strong>__setstate__</strong></a>(...)</dt></dl>
<hr>
Data descriptors inherited from <a href="exceptions.html#BaseException">exceptions.BaseException</a>:<br>
<dl><dt><strong>__dict__</strong></dt>
</dl>
<dl><dt><strong>args</strong></dt>
</dl>
<dl><dt><strong>message</strong></dt>
<dd><tt>exception message</tt></dd>
</dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="FSM">class <strong>FSM</strong></a></font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt>This is a Finite State Machine (<a href="#FSM">FSM</a>).<br> </tt></td></tr>
<tr><td> </td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="FSM-__init__"><strong>__init__</strong></a>(self, initial_state, memory<font color="#909090">=None</font>)</dt><dd><tt>This creates the <a href="#FSM">FSM</a>. You set the initial state here. The "memory"<br>
attribute is any object that you want to pass along to the action<br>
functions. It is not used by the <a href="#FSM">FSM</a>. For parsing you would typically<br>
pass a list to be used as a stack.</tt></dd></dl>
<dl><dt><a name="FSM-add_transition"><strong>add_transition</strong></a>(self, input_symbol, state, action<font color="#909090">=None</font>, next_state<font color="#909090">=None</font>)</dt><dd><tt>This adds a transition that associates:<br>
<br>
(input_symbol, current_state) --> (action, next_state)<br>
<br>
The action may be set to None in which case the <a href="#FSM-process">process</a>() method will<br>
ignore the action and only set the next_state. The next_state may be<br>
set to None in which case the current state will be unchanged.<br>
<br>
You can also set transitions for a list of symbols by using<br>
<a href="#FSM-add_transition_list">add_transition_list</a>().</tt></dd></dl>
<dl><dt><a name="FSM-add_transition_any"><strong>add_transition_any</strong></a>(self, state, action<font color="#909090">=None</font>, next_state<font color="#909090">=None</font>)</dt><dd><tt>This adds a transition that associates:<br>
<br>
(current_state) --> (action, next_state)<br>
<br>
That is, any input symbol will match the current state.<br>
The <a href="#FSM-process">process</a>() method checks the "any" state associations after it first<br>
checks for an exact match of (input_symbol, current_state).<br>
<br>
The action may be set to None in which case the <a href="#FSM-process">process</a>() method will<br>
ignore the action and only set the next_state. The next_state may be<br>
set to None in which case the current state will be unchanged.</tt></dd></dl>
<dl><dt><a name="FSM-add_transition_list"><strong>add_transition_list</strong></a>(self, list_input_symbols, state, action<font color="#909090">=None</font>, next_state<font color="#909090">=None</font>)</dt><dd><tt>This adds the same transition for a list of input symbols.<br>
You can pass a list or a string. Note that it is handy to use<br>
string.digits, string.whitespace, string.letters, etc. to add<br>
transitions that match character classes.<br>
<br>
The action may be set to None in which case the <a href="#FSM-process">process</a>() method will<br>
ignore the action and only set the next_state. The next_state may be<br>
set to None in which case the current state will be unchanged.</tt></dd></dl>
<dl><dt><a name="FSM-get_transition"><strong>get_transition</strong></a>(self, input_symbol, state)</dt><dd><tt>This returns (action, next state) given an input_symbol and state.<br>
This does not modify the <a href="#FSM">FSM</a> state, so calling this method has no side<br>
effects. Normally you do not call this method directly. It is called by<br>
<a href="#FSM-process">process</a>().<br>
<br>
The sequence of steps to check for a defined transition goes from the<br>
most specific to the least specific.<br>
<br>
1. Check state_transitions[] that match exactly the tuple,<br>
(input_symbol, state)<br>
<br>
2. Check state_transitions_any[] that match (state)<br>
In other words, match a specific state and ANY input_symbol.<br>
<br>
3. Check if the default_transition is defined.<br>
This catches any input_symbol and any state.<br>
This is a handler for errors, undefined states, or defaults.<br>
<br>
4. No transition was defined. If we get here then raise an exception.</tt></dd></dl>
<dl><dt><a name="FSM-process"><strong>process</strong></a>(self, input_symbol)</dt><dd><tt>This is the main method that you call to process input. This may<br>
cause the <a href="#FSM">FSM</a> to change state and call an action. This method calls<br>
<a href="#FSM-get_transition">get_transition</a>() to find the action and next_state associated with the<br>
input_symbol and current_state. If the action is None then the action<br>
is not called and only the current state is changed. This method<br>
processes one complete input symbol. You can process a list of symbols<br>
(or a string) by calling <a href="#FSM-process_list">process_list</a>().</tt></dd></dl>
<dl><dt><a name="FSM-process_list"><strong>process_list</strong></a>(self, input_symbols)</dt><dd><tt>This takes a list and sends each element to <a href="#FSM-process">process</a>(). The list may<br>
be a string or any iterable object.</tt></dd></dl>
<dl><dt><a name="FSM-reset"><strong>reset</strong></a>(self)</dt><dd><tt>This sets the current_state to the initial_state and sets<br>
input_symbol to None. The initial state was set by the constructor<br>
<a href="#FSM-__init__">__init__</a>().</tt></dd></dl>
<dl><dt><a name="FSM-set_default_transition"><strong>set_default_transition</strong></a>(self, action, next_state)</dt><dd><tt>This sets the default transition. This defines an action and<br>
next_state if the <a href="#FSM">FSM</a> cannot find the input symbol and the current<br>
state in the transition list and if the <a href="#FSM">FSM</a> cannot find the<br>
current_state in the transition_any list. This is useful as a final<br>
fall-through state for catching errors and undefined states.<br>
<br>
The default transition can be removed by setting the attribute<br>
default_transition to None.</tt></dd></dl>
</td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#eeaa77">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
<tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td>
<td width="100%"><dl><dt><a name="-BeginBuildNumber"><strong>BeginBuildNumber</strong></a>(fsm)</dt></dl>
<dl><dt><a name="-BuildNumber"><strong>BuildNumber</strong></a>(fsm)</dt></dl>
<dl><dt><a name="-DoEqual"><strong>DoEqual</strong></a>(fsm)</dt></dl>
<dl><dt><a name="-DoOperator"><strong>DoOperator</strong></a>(fsm)</dt></dl>
<dl><dt><a name="-EndBuildNumber"><strong>EndBuildNumber</strong></a>(fsm)</dt></dl>
<dl><dt><a name="-Error"><strong>Error</strong></a>(fsm)</dt></dl>
<dl><dt><a name="-main"><strong>main</strong></a>()</dt><dd><tt>This is where the example starts and the <a href="#FSM">FSM</a> state transitions are<br>
defined. Note that states are strings (such as 'INIT'). This is not<br>
necessary, but it makes the example easier to read.</tt></dd></dl>
</td></tr></table>
</body></html>