org.jscience.computing.automaton.machines
Class FSM

java.lang.Object
  extended by org.jscience.computing.automaton.machines.FSM
All Implemented Interfaces:
Visualizable
Direct Known Subclasses:
TuringMachine

public class FSM
extends java.lang.Object
implements Visualizable

Implements a finite-state machine. This class implements the Visualizable interface to display the FSM in a graphics context.


Nested Class Summary
static class FSM.State
          The FSM states.
 
Field Summary
protected  FSM.State currentState
          The current state in the FSM is in.
protected  FSM.State[] fsmStates
          An array of the states in the finite-state machine.
protected  int numStates
          The number of states in the FSM.
protected  int renderSize
          The size of the states to be rendered.
 
Constructor Summary
FSM()
          Creates a new instance of FSM
FSM(FSM.State initialState)
          Creates a new instance of FSM with an initial state.
FSM(int maxStates)
          Creates a new instance of FSM, with the maximum number of allowed states.
FSM(int maxStates, FSM.State initialState)
          Creates a new instance of FSM with an initial state and the maximum allowed states.
 
Method Summary
 int addState(FSM.State state)
          Add a state to the FSM.
 int addStates(FSM.State[] states)
          Adds an array of states to the FSM.
 void addTransition(FSM.State start, int transition, FSM.State end)
          Add a transition to the finite state machine.
 FSM.State getCurrentState()
          Return the current state the FSM is in.
 int getRenderSize()
          Return the current render size.
 FSM.State getState(int i)
          Retrieve the given state from the FSM.
 int getStates()
          Return the number of states in the FSM.
static void main(java.lang.String[] args)
          A simple test function.
 void removeAllStates()
          Remove all states.
 void render(java.awt.Graphics g, int width, int height)
          Render the FSM in a graphics context.
 void setRenderSize(int rs)
          Set the size (in pixels) to render the states.
 void setState(FSM.State state)
          Set the current state of the finite-state machine.
 FSM.State transition(int transition)
          Transition the FSM from one state to another with a given transition identifier.
 void writeImage(java.lang.String s, int width, int height)
          Write the FSM to an image.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numStates

protected int numStates
The number of states in the FSM.


currentState

protected FSM.State currentState
The current state in the FSM is in.


fsmStates

protected FSM.State[] fsmStates
An array of the states in the finite-state machine.


renderSize

protected int renderSize
The size of the states to be rendered.

Constructor Detail

FSM

public FSM()
Creates a new instance of FSM


FSM

public FSM(int maxStates)
Creates a new instance of FSM, with the maximum number of allowed states.

Parameters:
maxStates - the maximum number of states allowed in the FSM.

FSM

public FSM(FSM.State initialState)
Creates a new instance of FSM with an initial state.

Parameters:
initialState - the initial state.

FSM

public FSM(int maxStates,
           FSM.State initialState)
Creates a new instance of FSM with an initial state and the maximum allowed states.

Parameters:
maxStates - the maximum number of states.
initialState - the initial state.
Method Detail

getCurrentState

public FSM.State getCurrentState()
Return the current state the FSM is in.

Returns:
the current state.

getStates

public int getStates()
Return the number of states in the FSM.

Returns:
the number of states.

setState

public void setState(FSM.State state)
Set the current state of the finite-state machine.

Parameters:
state - the state.

addState

public int addState(FSM.State state)
Add a state to the FSM.

Parameters:
state - the state to be added to the FSM.
Returns:
the number of states currently in the FSM.
Throws:
java.lang.IllegalArgumentException - DOCUMENT ME!

addStates

public int addStates(FSM.State[] states)
Adds an array of states to the FSM.

Parameters:
states - the array of States to add to the FSM.
Returns:
the number of states.

removeAllStates

public void removeAllStates()
Remove all states.


addTransition

public void addTransition(FSM.State start,
                          int transition,
                          FSM.State end)
Add a transition to the finite state machine. Note that adding the transition to the FSM is equivalent to adding a transition to the state itself: fsm.addTransition(state1, transition, state2); // ...is equivalent to: state1.addTransition(transition, state2);

Parameters:
start - the start state.
transition - the transition identifier.
end - the end state.

transition

public FSM.State transition(int transition)
Transition the FSM from one state to another with a given transition identifier. This function will throw a NullPointerException if the transition isn't defined for the current state.

Parameters:
transition - the transition identifier.
Returns:
the new state of the FSM.
Throws:
java.lang.NullPointerException - DOCUMENT ME!

getState

public FSM.State getState(int i)
Retrieve the given state from the FSM.

Parameters:
i - the index of the state to return.
Returns:
the state at this index.

main

public static void main(java.lang.String[] args)
A simple test function.

Parameters:
args - the command line arguments

setRenderSize

public void setRenderSize(int rs)
Set the size (in pixels) to render the states.

Parameters:
rs - the new render size.

getRenderSize

public int getRenderSize()
Return the current render size.

Returns:
the render size.

render

public void render(java.awt.Graphics g,
                   int width,
                   int height)
Render the FSM in a graphics context. The function renders all connections first, then the states themselves.

Specified by:
render in interface Visualizable
Parameters:
g - the graphics context.
width - the width of the context.
height - the height of the context.

writeImage

public void writeImage(java.lang.String s,
                       int width,
                       int height)
Write the FSM to an image.

Specified by:
writeImage in interface Visualizable
Parameters:
s - the filename of the image.
width - the width of the image.
height - the height of the image.