cz.cuni.amis.fsm
Class FSM<SYMBOL,CONTEXT>

java.lang.Object
  extended by cz.cuni.amis.fsm.FSM<SYMBOL,CONTEXT>
Type Parameters:
SYMBOL -
CONTEXT -
All Implemented Interfaces:
IFSM<SYMBOL,CONTEXT>

public class FSM<SYMBOL,CONTEXT>
extends java.lang.Object
implements IFSM<SYMBOL,CONTEXT>

Context aware FSM implementation.

FSM = Finite State Machine, also called FSA = Finite State Automaton, more info at http://en.wikipedia.org/wiki/Finite_state_machine

This FSM is based upon the annotations FSMState, FSMTransition, FSMInitialState and FSMTerminalState.

Every class annotated with FSMState may be inserted to the FSM (in constructor) as FSM state. The annotation always contains the transitions that leads from the state to other states.

Unlike many other implementations of the FSMs there is out there in the wild, this one isn't driven by integers. It works with whole objects instead treating their classes as the symbols. Generic parameter SYMBOL should the common ancestor of all symbols you will want to pass to the FSM. This allows you (for instance) to implement protocols based upon real messages (where every message has it's own class!).

Note that every action of FSM's states and transitions have "context" parameter. Use this object to pass the common interface you will need during the state/transition work (stateEntering(), stateSymbol(), stateLeaving(), stepped()).

Pros of this implementation:

Cons of this implementation:


Nested Class Summary
protected static class FSM.StateWrapper<SYMBOL,CONTEXT>
           
protected static class FSM.TransitionWrapper<SYMBOL,CONTEXT>
           
 
Constructor Summary
FSM(CONTEXT context, java.lang.Class<? extends IFSMState<SYMBOL,CONTEXT>> state, java.util.logging.Logger log)
          FSM constructor that tries to instantiate all states / transitions for itself.
FSM(CONTEXT context, IFSMState<SYMBOL,CONTEXT>[] states, IFSMTransition[] transitions, java.util.logging.Logger log)
          Note that this constructor will use "states" and "transitions" to instantiate the FSM but you may omit those states/transitions that contains implicite constructor (parameter-less).
FSM(CONTEXT context, IFSMState<SYMBOL,CONTEXT> state, java.util.logging.Logger log)
          FSM constructor that tries to instantiate all (but first "state") states / transitions for itself.
 
Method Summary
static IFSMState getState(java.lang.Class<? extends IFSMState> state, java.util.logging.Logger log)
          Construct and returns IFSMState of the given class, parameter-less constructor is sought.
protected  java.util.Collection<FSM.StateWrapper<SYMBOL,CONTEXT>> getStates()
           
protected static IFSMTransition getTransition(java.lang.Class<? extends IFSMTransition> transition, java.util.logging.Logger log)
          Constructs and returns IFSMTransition of the given class, parameter-less constructor is sought.
 java.util.Collection<IFSMTransition<SYMBOL,CONTEXT>> getTransitions()
           
 boolean isTerminal()
           
 void push(CONTEXT context, SYMBOL symbol)
           
 void restart(CONTEXT context)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FSM

public FSM(CONTEXT context,
           java.lang.Class<? extends IFSMState<SYMBOL,CONTEXT>> state,
           java.util.logging.Logger log)
FSM constructor that tries to instantiate all states / transitions for itself.

First state to try is "state".

Parameters:
context -
state -
log - may be null

FSM

public FSM(CONTEXT context,
           IFSMState<SYMBOL,CONTEXT> state,
           java.util.logging.Logger log)
FSM constructor that tries to instantiate all (but first "state") states / transitions for itself.

First state to try is "state".

Parameters:
context -
state -
log - may be null

FSM

public FSM(CONTEXT context,
           IFSMState<SYMBOL,CONTEXT>[] states,
           IFSMTransition[] transitions,
           java.util.logging.Logger log)
Note that this constructor will use "states" and "transitions" to instantiate the FSM but you may omit those states/transitions that contains implicite constructor (parameter-less). This constructor will create those states/transitions for itself via reflection.

Parameters:
context -
states -
transitions -
log - may be null
Method Detail

getStates

protected java.util.Collection<FSM.StateWrapper<SYMBOL,CONTEXT>> getStates()

getTransitions

public java.util.Collection<IFSMTransition<SYMBOL,CONTEXT>> getTransitions()

getState

public static IFSMState getState(java.lang.Class<? extends IFSMState> state,
                                 java.util.logging.Logger log)
Construct and returns IFSMState of the given class, parameter-less constructor is sought.

Throws FSMBuildException if constructor not found.

Parameters:
state -
log - may be null
Returns:

getTransition

protected static IFSMTransition getTransition(java.lang.Class<? extends IFSMTransition> transition,
                                              java.util.logging.Logger log)
Constructs and returns IFSMTransition of the given class, parameter-less constructor is sought.

Throws FSMBuildException if constructor not found.

Parameters:
state -
log - may be null
Returns:

isTerminal

public boolean isTerminal()
Specified by:
isTerminal in interface IFSM<SYMBOL,CONTEXT>

push

public void push(CONTEXT context,
                 SYMBOL symbol)
Specified by:
push in interface IFSM<SYMBOL,CONTEXT>

restart

public void restart(CONTEXT context)
Specified by:
restart in interface IFSM<SYMBOL,CONTEXT>