public abstract class StateMachine extends java.lang.Object implements java.awt.event.ActionListener, StateMachineListener
A state machine consists of a set of states and a set of transitions.
Each transition goes from an input state to an output state (which can be the same),
and is labeled by an event, an optional guard and an optional action.
At any one time, the machine is one of its states, called the current state.
When the state machine receives an event, it looks for the first outgoing transition
of the current state that matches the event and whose guard
method returns True.
If it finds such a transition, it fires it, i.e. it calls the current state's leave()
method,
then the transition's action()
method, finally sets the current state to the transition's output state
and calls the output state's enter()
method.
If no transition matches, the event is simply ignored.
The declaration of a state machine uses Java's anonymous class as follows:
guard
and action
methods;
In summary, the structure of a state machine is as follows:
StateMachine sm = new StateMachine () { // local fields and methods if needed ... public State s1 = new State () { // local fields and methods if needed ... public void enter () { ... do something when entering this state ... } // optional public void leave () { ... do something when leaving this state ...} // optional // declare a transition to state s2 when receiving an event "anEvent".. // (see class StateMachine.State.Transition for details). Transition t1 = new Event ("anEvent", ">> s2") { public boolean guard () { ... return True or False ... } public void action () { ... do something ... } } Transition t2 = ... } public State s2 = new State () { ... } }
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
TIME_OUT
The key string of events that triggered
AnimationStopped transitions. |
Constructor and Description |
---|
StateMachine()
Builds a state machine.
|
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(java.awt.event.ActionEvent arg0) |
void |
addStateMachineListener(StateMachineEventListener l)
Adds the specified state machine event listener to receive state machine events from this state machine.
|
void |
addStateMachineListener(StateMachineListener listener)
Adds the specified state machine listener to receive events fired by this state machine.
|
void |
armTimer(int d,
boolean repeat)
Arms the default timer.
|
void |
armTimer(java.lang.String tag,
int d,
boolean repeat)
Arms a tagged timer.
|
StateMachine |
consumes(boolean c)
Makes this state machine consume an event.
|
void |
disarmTimer()
Disarms the timer.
|
void |
disarmTimer(java.lang.String tag)
Disarms a tagged timer.
|
void |
doReset()
Method called when this state machine is reset.
|
void |
doResume()
Method called when this state machine is resumed.
|
void |
doSuspend()
Method called when this state machine is suspended.
|
void |
eventOccured(java.util.EventObject eventObject)
Invoked when the state machine has fired an event.
|
void |
fireEvent(java.util.EventObject event)
Makes this state machine fire an event that will be heard by all
its
StateMachineListener . |
void |
fireEvent(java.lang.String nameEvent)
Makes this state machine fire a virtual event having a
given name that will be heard by all
its
StateMachineListener . |
java.util.Vector<State> |
getAllStates()
Returns the vector containing all this state machine's states.
|
State |
getCurrentState()
Returns this state machine's current state.
|
State |
getInitialState()
Returns this state machine's initial state.
|
State |
getState(java.lang.String s)
Look up a state by its name.
|
boolean |
hasConsumed()
Tests if this state machine has consumed the last event it processed.
|
void |
init()
This method is called by the constructor of a state machine.
|
void |
initStatesAndTransitions()
Internal initialization of the state machine: resolve the state names into their corresponding objects.
|
boolean |
isActive()
Returns the active state of this state machine.
|
boolean |
isInited() |
void |
processEvent(java.util.EventObject event)
Processes in the state machine the virtual event received.
|
void |
processEvent(java.lang.String event)
Processes in the state machine a virtual event having a given name.
|
void |
removeStateMachineListener(StateMachineEventListener l)
Removes the specified state machine event listener so that it no longer receives state machine events from this state machine.
|
void |
removeStateMachineListener(StateMachineListener listener)
Removes the specified state machine listener.
|
StateMachine |
reset()
Sets the state of this state machine to the initial state.
|
void |
resume()
Makes this state machine active.
|
void |
setActive(boolean active)
Makes this state machine be active or inactive (calls
resume or suspend ). |
void |
suspend()
Makes this state machine inactive.
|
public static java.lang.String TIME_OUT
AnimationStopped
transitions.public StateMachine consumes(boolean c)
c
- True if this state machine must consume this event, false otherwise.public boolean hasConsumed()
public State getCurrentState()
public State getInitialState()
public java.util.Vector<State> getAllStates()
public void addStateMachineListener(StateMachineEventListener l)
l
- The state machine event listener to add.public void removeStateMachineListener(StateMachineEventListener l)
l
- The state machine event listener to remove.public void doReset()
reset()
public StateMachine reset()
public boolean isActive()
suspend()
has been called.public void doSuspend()
suspend()
public void suspend()
public void setActive(boolean active)
resume
or suspend
).public void doResume()
resume()
public void resume()
public void init()
public void initStatesAndTransitions()
public void actionPerformed(java.awt.event.ActionEvent arg0)
actionPerformed
in interface java.awt.event.ActionListener
public void armTimer(int d, boolean repeat)
TimeOut
event is sent to the state machine.
Each state machine has a single timer.
Calling armTimer
before it has expired effectively rearms it.d
- the delay of the timer.repeat
- If false, only one TimeOut
event is fired. If true, a TimeOut
event is fired every d
milliseconds.public void armTimer(java.lang.String tag, int d, boolean repeat)
TimeOut
event is sent to the state machine.
Calling armTimer
before it has expired effectively rearms it.tag
- the tag.d
- the delay of the timer.repeat
- If false, only one TimeOut
event is fired. If true, a TimeOut
event is fired every d
milliseconds.public void disarmTimer()
public void disarmTimer(java.lang.String tag)
tag
- the tag.public void addStateMachineListener(StateMachineListener listener)
sendEvent
to make a state machine fire an event.listener
- The state machine listener to add.fireEvent(EventObject)
,
removeStateMachineListener(StateMachineEventListener)
public void removeStateMachineListener(StateMachineListener listener)
listener
- The state machine listener to remove.public void fireEvent(java.lang.String nameEvent)
StateMachineListener
.nameEvent
- The name of the VirtualEvent
to fire.public void fireEvent(java.util.EventObject event)
StateMachineListener
.event
- The event to fire.addStateMachineListener(StateMachineEventListener)
public void eventOccured(java.util.EventObject eventObject)
eventOccured
in interface StateMachineListener
eventObject
- the event.public void processEvent(java.lang.String event)
event
- The name of the virtual event to processpublic void processEvent(java.util.EventObject event)
event
- The virtual event to processpublic State getState(java.lang.String s) throws StateNotFoundException
new State("myState")
),
otherwise the state name is the name of the field where it is stored in the state machine (public State myState = new State()
).
Note that for this to work, the field must be declared public.s
- the name of the state to look upStateNotFoundException
public boolean isInited()