FSM
[ class tree: FSM ] [ index: FSM ] [ all elements ]

Class: FSM

Source Location: /FSM-1.3.0/FSM.php

Class Overview


This class implements a Finite State Machine (FSM).


Author(s):

Version:

  • $Revision: 1.16 $

Methods


Child classes:

FSM_GraphViz
FSM to Image_GraphViz converter

Inherited Variables

Inherited Methods


Class Details

[line 48]
This class implements a Finite State Machine (FSM).

In addition to maintaining state, this FSM also maintains a user-defined payload, therefore effectively making the machine a Push-Down Automata (a finite state machine with memory).

This code is based on Noah Spurrier's Finite State Machine (FSM) submission to the Python Cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146262



[ Top ]


Method Detail

FSM (Constructor)   [line 116]

FSM FSM( string $initialState, &$payload, mixed $payload)

This method constructs a new Finite State Machine (FSM) object.

In addition to defining the machine's initial state, a "payload" may also be specified. The payload represents a variable that will be passed along to each of the action functions. If the FSM is being used for parsing, the payload is often a array that is used as a stack.


Parameters:

string   $initialState     The initial state of the FSM.
mixed   $payload     A payload that will be passed to each action function.
   &$payload     

[ Top ]

addTransition   [line 148]

void addTransition( string $symbol, string $state, string $nextState, [string $action = null])

This method adds a new transition that associates:

(symbol, currentState) --> (nextState, action)

The action may be set to NULL, in which case the processing routine will ignore the action and just set the next state.


Parameters:

string   $symbol     The input symbol.
string   $state     This transition's starting state.
string   $nextState     This transition's ending state.
string   $action     The name of the function to invoke when this transition occurs.

[ Top ]

addTransitionAny   [line 205]

void addTransitionAny( string $state, string $nextState, [string $action = null])

This method adds a new transition that associates:

(currentState) --> (nextState, action)

The processing routine checks these associations if it cannot first find a match for (symbol, currentState).


Parameters:

string   $state     This transition's starting state.
string   $nextState     This transition's ending state.
string   $action     The name of the function to invoke when this transition occurs.

[ Top ]

addTransitions   [line 164]

void addTransitions( array $symbols, string $state, string $nextState, [string $action = null])

This method adds the same transition for multiple different symbols.

Parameters:

array   $symbols     A list of input symbols.
string   $state     This transition's starting state.
string   $nextState     This transition's ending state.
string   $action     The name of the function to invoke when this transition occurs.

[ Top ]

addTransitionsArray   [line 183]

void addTransitionsArray( array $transitions)

This method adds an array of transitions. Each transition is itself defined as an array of values which will be passed to addTransition() as parameters.

Parameters:

array   $transitions     An array of transitions.

[ Top ]

getTransition   [line 247]

mixed getTransition( string $symbol)

This method returns (nextState, action) given an input symbol and state. The FSM is not modified in any way. This method is rarely called directly (generally only for informational purposes).

If the transition cannot be found in either of the transitions lists, the default transition will be returned. Note that it is possible for the default transition to be set to NULL.

  • Return: Array representing (nextState, action), or NULL if the transition could not be found and not default transition has been defined.

Parameters:

string   $symbol     The input symbol.

[ Top ]

process   [line 278]

void process( string $symbol)

This method is the main processing routine. It causes the FSM to change states and execute actions.

The transition is determined by calling getTransition() with the provided symbol and the current state. If no valid transition is found, process() returns immediately.

The action callback may return the name of a new state. If one is returned, the current state will be updated to the new value.

If no action is defined for the transition, only the state will be changed.


Parameters:

string   $symbol     The input symbol.

[ Top ]

processList   [line 308]

void processList( array $symbols)

This method processes a list of symbols. Each symbol in the list is sent to process().

Parameters:

array   $symbols     List of input symbols to process.

[ Top ]

reset   [line 127]

void reset( )

This method resets the FSM by setting the current state back to the initial state (set by the constructor).

[ Top ]

setDefaultTransition   [line 222]

void setDefaultTransition( string $nextState, string $action)

This method sets the default transition. This defines an action and next state that will be used if the processing routine cannot find a suitable match in either transition list. This is useful for catching errors caused by undefined states.

The default transition can be removed by setting $nextState to NULL.


Parameters:

string   $nextState     The transition's ending state.
string   $action     The name of the function to invoke when this transition occurs.

[ Top ]


Documentation generated on Sun, 10 Feb 2008 18:00:03 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.