pear/fsm

Finite State Machine

dev-master 2021-03-19 18:49 UTC

This package is auto-updated.

Last update: 2024-11-20 02:52:37 UTC


README

User Documentation

Contents

1   About the FSM Package

The FSM Package implements a Finite State Machine. In addition to maintaining state, this FSM also manages a user-defined payload, therefore effectively making the machine a Pushdown Automaton (a finite state machine with memory).

This code is based largely on Noah Spurrier's excellent FSM Python class.

2   Building a Finite State Machine

The first step in building a Finite State Machine involves listing the finite set of states. Then, all of the permissible transitions between these states must be defined. A symbol and an optional callback function are associated with each transition. The input processing routine will attempt to match its current symbol against the list of registered transitions. If a transition from the current state using that symbol is found, the machine will move to the new state specified by the transition and, if one has been specified, the associated callback function will be invoked.

2.1   Creating a New FSM Object

Start by including the FSM package in your script:

require 'FSM.php';

When constructing a new FSM object, you must specify the machine's initial state and provide a payload variable. The payload will be passed to all of the callback functions, supplying them with state information without (ab)using global variables.

In this example, we pass an array representing a stack as the payload. The machine's initial state is set to START.

$stack = array();
$fsm = new FSM('START', $stack);

2.2   Defining Transitions

We'll need to define some transitions in order to make our machine useful. Let's assume our machine has two additional states: MIDDLE and END. Here's how we would define transitions to move us from START to MIDDLE and from MIDDLE to END:

function FirstCallback($symbol, &$payload, $currentState, $nextState)
{
    echo "First Transition\n";
}

function SecondCallback($symbol, &$payload, $currentState, $nextState)
{
    echo "Second Transition\n";
}

$fsm->addTransition('FIRST', 'START', 'MIDDLE', 'FirstCallback');
$fsm->addTransition('SECOND', 'MIDDLE', 'END', 'SecondCallback');

Our machine is now aware of three states (START, MIDDLE, and END) and two symbols (FIRST and SECOND). Two transitions (START to MIDDLE and MIDDLE to END) have been defined and associated with callbacks. The following code will process the symbols FIRST and SECOND and move us from our initial state (START) through the MIDDLE state to the END state.

$fsm->process('FIRST');
$fsm->process('SECOND');

The processing routine will invoke our two callbacks along the way, as well, resulting in the following being printed:

First Transition
Second Transition

2.3   Setting Default Transitions

Now we'll set up a default transition. This transition will be used whenever the processing routine cannot find a better match for the current state and symbol. For our example, we'll consider this an error and print a warning for the user.

function ErrorCallback($symbol, &$payload, $currentState, $nextState)
{
    echo "This symbol does not compute: $symbol\n";
}

$fsm->setDefaultTransition('START', 'ErrorCallback');

Now let's process our symbols in an unexcepted order:

$fsm->process('SECOND');
$fsm->process('FIRST');

Because the SECOND transition doesn't specify START as its initial state, the default transition will be used and the error callback will be invoked. The FIRST transition will work as expected, however, because the machine will still be in the START state.

3   Plotting a State Machine

The FSM package optionally supports the ability to plot a machine's states with the help of the Image_GraphViz package. Doing so is as simple as creating a new FSM_GraphViz object using an existing state machine instance and then exporting the graph.

require_once 'FSM/GraphViz.php';
$converter = new FSM_GraphViz($fsm);
$graph = $converter->export();

The resulting graph object is an Image_GraphViz instance. To export the graph as an image, use the image() method:

$graph->image('png');

This will produce an image similar to the following:

Example State Machine Plot

Consult the Image_GraphViz documentation for additional usage information.

4   Development and Support

4.1   Reporting Problems and Suggestions

If you run into a problem or would like to make a suggestion, please use the PEAR Bug Tracker. Feel free to contact me directly for other issues, but please try to use the bug tracker whenever possible so that others in the community will benefit from your feedback and my responses.

4.2   Coming Soon

This section contains a list of "todo" items that will hopefully be addressed in future releases.

  • No items at this time.

If you have feature suggestions, please submit them using the PEAR Bug Tracker.