Package org.glassfish.pfl.basic.fsm
Class StateEngine
- java.lang.Object
-
- org.glassfish.pfl.basic.fsm.StateEngine
-
public class StateEngine extends java.lang.ObjectEncodes the state transition function for a finite state machine.
-
-
Field Summary
Fields Modifier and Type Field Description private ActiondefaultActionprivate static ActionemptyActionprivate booleaninitializingprivate java.util.Map<State.Kind,java.util.Set<State>>stateKindsprivate java.util.Map<State.Kind,java.util.Set<State>>unmodifiableStateKinds
-
Constructor Summary
Constructors Modifier Constructor Description privateStateEngine()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StateEngineadd(State oldState, java.util.Set<Input> input, Action action, State newState)Repeatedly call add( State, Input, Action, State ) for each element of input.StateEngineadd(State oldState, java.util.Set<Input> input, Guard guard, Action action, State newState)Repeatedly calls add( State, Input, Guard, Action, State ) for each element of input.StateEngineadd(State oldState, Input input, Action action, State newState)Add a transition with a guard that always evaluates to true.StateEngineadd(State oldState, Input input, Guard guard, Action action, State newState)Add a new transition (old,in,guard,act,new) to the state engine.static StateEnginecreate()Create an empty uninitialized state engine.voiddoIt(Runner runner, Input in, boolean debug)Actually perform a state transition on the FSM on the runner.peek() FSM under Input in.voiddone()Called after all transitions have been added to the state engine.private voiddprint(java.lang.String msg)private ActiongetDefaultAction(State currentState)private StategetDefaultNextState(State currentState)private java.util.Set<State>getKindSet(State.Kind kind)java.util.Set<State>getStates(State.Kind kind)Return the set of final states for this state engine.private voidinnerDoIt(Runner runner, Input in, boolean debug)private voidmustBeInitializing()private voidmustNotBeInitializing()(package private) voidperformStateTransition(Runner runner, Input in, State nextState, Action action, boolean debug)StateEnginesetDefault(State oldState)Euaivalent to setDefault( oldState, oldState )StateEnginesetDefault(State oldState, Action action, State newState)Set the default transition and action for a state.StateEnginesetDefault(State oldState, State newState)Equivalent to setDefault( oldState, act, newState ) where act is an action that does nothing.voidsetDefaultAction(Action act)Set the default action used in this state engine.private voidupdateStateMap(State oldState, State newState)
-
-
-
Field Detail
-
emptyAction
private static Action emptyAction
-
initializing
private boolean initializing
-
defaultAction
private Action defaultAction
-
stateKinds
private java.util.Map<State.Kind,java.util.Set<State>> stateKinds
-
unmodifiableStateKinds
private java.util.Map<State.Kind,java.util.Set<State>> unmodifiableStateKinds
-
-
Method Detail
-
create
public static StateEngine create()
Create an empty uninitialized state engine.
-
dprint
private void dprint(java.lang.String msg)
-
getKindSet
private java.util.Set<State> getKindSet(State.Kind kind)
-
getStates
public java.util.Set<State> getStates(State.Kind kind)
Return the set of final states for this state engine. This may be an empty set.
-
add
public StateEngine add(State oldState, Input input, Guard guard, Action action, State newState)
Add a new transition (old,in,guard,act,new) to the state engine. Multiple calls to add with the same old and in are permitted, in which case only a transition in which the guard evaluates to true will be taken. If no such transition is enabled, a default will be taken. If more than one transition is enabled, one will be chosen arbitrarily. This method can only be called before done(). An attempt to call it after done() results in an IllegalStateException.
-
add
public StateEngine add(State oldState, java.util.Set<Input> input, Guard guard, Action action, State newState)
Repeatedly calls add( State, Input, Guard, Action, State ) for each element of input.
-
add
public StateEngine add(State oldState, Input input, Action action, State newState)
Add a transition with a guard that always evaluates to true.
-
add
public StateEngine add(State oldState, java.util.Set<Input> input, Action action, State newState)
Repeatedly call add( State, Input, Action, State ) for each element of input.
-
setDefault
public StateEngine setDefault(State oldState, Action action, State newState)
Set the default transition and action for a state. This transition will be used if no more specific transition was defined for the actual input. Repeated calls to this method simply change the default. This method can only be called before done(). An attempt to call it after done() results in an IllegalStateException.
-
setDefault
public StateEngine setDefault(State oldState, State newState)
Equivalent to setDefault( oldState, act, newState ) where act is an action that does nothing.
-
setDefault
public StateEngine setDefault(State oldState)
Euaivalent to setDefault( oldState, oldState )
-
done
public void done()
Called after all transitions have been added to the state engine. This provides an opportunity for the implementation to optimize its representation before the state engine is used. This method may only be called once. An attempt to call it more than once results in an IllegalStateException.Note that a valid StateEngine must satisfy the following conditions:
- It must contain exactly one state with Kind INITIAL.
- No final state may have an outgoing transition.
-
setDefaultAction
public void setDefaultAction(Action act)
Set the default action used in this state engine. This is the action that is called whenever there is no applicable transition. Normally this would simply flag an error. This method can only be called before done(). An attempt to call it after done() results in an IllegalStateException.
-
doIt
public void doIt(Runner runner, Input in, boolean debug)
Actually perform a state transition on the FSM on the runner.peek() FSM under Input in.
-
performStateTransition
void performStateTransition(Runner runner, Input in, State nextState, Action action, boolean debug)
-
mustBeInitializing
private void mustBeInitializing()
-
mustNotBeInitializing
private void mustNotBeInitializing()
-
-