|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsm.StateMachine
A state machine to control the interaction with an SMCanvas
.
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 labelled 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 ("sm") { // 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 pressing mouse button 1.. // (see class StateMachine.State.Transition for details). Transition t1 = new Press (BUTTON1, "=> s2") { public boolean guard () { ... return True or False ... } public void action () { ... do something ... } } Transition t2 = ... } public State s2 = new State () { ... } }
Nested Class Summary | |
class |
StateMachine.State
A state of a state machine. |
Constructor Summary | |
StateMachine(java.lang.String n)
Builds a state machine. |
|
StateMachine(java.lang.String n,
InteractiveObject io)
Builds a state machine and attach it to an interactive object. |
Method Summary | |
void |
actionPerformed(java.awt.event.ActionEvent arg0)
|
void |
addSMListener(SMStateMachineEventListener l)
Adds the specified state machine event listener to receive state machine events from this state machine. |
void |
armTimer(int d)
Arms the timer. |
void |
attachTo(InteractiveObject io)
Attaches a state machine to a given interactive object and resets it. |
void |
attachTo(InteractiveObject io,
boolean reset)
Attaches a state machine to a given canvas. |
StateMachine |
consumes(boolean c)
Makes this state machine consume an event. |
void |
detach(InteractiveObject io)
Detaches a state machine from an interactive object. |
void |
disarmTimer()
Disarms the 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. |
java.util.Vector |
getAllStates()
Returns the vector containing all this state machine's states. |
StateMachine.State |
getCurrentState()
Returns the this state machine's current state. |
java.lang.String |
getName()
Returns the name of this state machine. |
java.util.LinkedList |
getObjects()
Returns the linked list of interactive objects to which this state machine is attached, or null is the machine is not attached. |
StateMachine |
greaterPriorityThan(StateMachine sm)
Makes this state machine have a greater priority than another state machine. |
StateMachine |
greatestPriority()
Makes this state machine have the greatest priority. |
boolean |
hasConsumed()
Tests if this state machine has consumed the last event it processed. |
void |
init()
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 |
isAttached(InteractiveObject io)
Returns true if this state machine is attached to a given interactive object. |
boolean |
isAttachedTo(InteractiveObject io)
Tests if this state machine is attached to a given interactive object. |
StateMachine |
lowerPriorityThan(StateMachine sm)
Makes this state machine have a lower priority than another state machine. |
StateMachine |
lowestPriority()
Makes this state machine have the lowest priority. |
void |
processEvent(SMVirtualEvent event)
Processes in the state machine the virtual event received. |
void |
processEvent(SMVirtualEvent event,
int modifier)
Processes in the state machine the virtual event received. |
void |
processEvent(SMVirtualEvent event,
int modifier,
java.awt.geom.Point2D point)
Processes in the state machine the virtual event received. |
void |
processEvent(SMVirtualEvent event,
java.awt.geom.Point2D point)
Processes in the state machine the virtual event received. |
void |
removeSMListener(SMStateMachineEventListener l)
Removes the specified state machine event listener so that it no longer receives state machine events from this state machine. |
void |
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. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public StateMachine(java.lang.String n)
n
- the name of the state machine.public StateMachine(java.lang.String n, InteractiveObject io)
n
- the name of the state machine.io
- the interactive object (SMCanvas, SMTag, SMShape) to link to this state machine.Method Detail |
public StateMachine lowerPriorityThan(StateMachine sm)
sm
- The state machine that must have a greater priority than this state machine.
public StateMachine lowestPriority()
public StateMachine greaterPriorityThan(StateMachine sm)
sm
- The state machine that must have a lower priority than this state machine.
public StateMachine greatestPriority()
public StateMachine consumes(boolean c)
c
- True if this state machine must consume this event, false otherwise.
public boolean hasConsumed()
public StateMachine.State getCurrentState()
public java.util.Vector getAllStates()
public void addSMListener(SMStateMachineEventListener l)
l
- The state machine event listener to add.public void removeSMListener(SMStateMachineEventListener l)
l
- The state machine event listener to remove.public void attachTo(InteractiveObject io, boolean reset)
io
- The interactive object to which this state machine must be linkedreset
- Whether to reset the state machine once attachedpublic void attachTo(InteractiveObject io)
io
- The interactive object to which this state machine must be linkedpublic void detach(InteractiveObject io)
io
- The interactive object to which this state machine must be detachedpublic boolean isAttachedTo(InteractiveObject io)
io
- The interactive object to test
io
.public java.lang.String getName()
public void doReset()
reset()
public final void reset()
public boolean isActive()
suspend()
has been called.
public void doSuspend()
suspend()
public final void suspend()
public final void setActive(boolean active)
resume
or suspend
).
active
- True to makes this state machine be active, false to makes this state machine be inactive.resume()
,
suspend()
public void doResume()
resume()
public void resume()
public boolean isAttached(InteractiveObject io)
io
- The interactive object to test.
public java.util.LinkedList getObjects()
public void init()
public void actionPerformed(java.awt.event.ActionEvent arg0)
actionPerformed
in interface java.awt.event.ActionListener
public void armTimer(int d)
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.public void disarmTimer()
public final void processEvent(SMVirtualEvent event, java.awt.geom.Point2D point)
event
- The virtual event to processpoint
- The location where occured the received eventpublic final void processEvent(SMVirtualEvent event, int modifier, java.awt.geom.Point2D point)
event
- The virtual event to processmodifier
- The modifier of the eventpoint
- The location where occured the received eventpublic final void processEvent(SMVirtualEvent event, int modifier)
event
- The virtual event to processmodifier
- The modifier of the eventpublic final void processEvent(SMVirtualEvent event)
event
- The virtual event to process
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |