|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object de.fhg.igd.jhsm.HSMInterpreter
An Interpreter for generic hsm data structures. This implementation was designed to run a generic hierarchical state machine in a multi threaded environment. One thread is responsible for processing the state machine. It runs the main loop of this class until the processing is finished or interrupted externally. An arbitrary number of other threads may call the event handling method of this class. Events will be held in a queue until the main thread is ready to handle them.
Note that only one thread may run the main loop of this class or else the flow of control might be corrupted.
This implementation is intended to be used only once for a given state machine. The hsm that should be interpreted is passed to the constructor and cannot be changed later. So if the interpratation is completed the interpereter can be thrown away.
Field Summary | |
private Context |
context_
This corresponds to the context as defined in the Interpreter design pattern. |
private State |
current_
The currently active state. |
private List |
eventQueue_
A queue for incoming events. |
private boolean |
firedInternal_
A flag indicating if recently an internal transition did fire. |
private static Logger |
LOG
|
private CompositeState |
root_
A referece to the root state of the state machine that is processed by this class. |
private boolean |
running_
A flag that indicates if this interpreter is running. |
private boolean |
suspended_
A flag that indicates if this interpreter is suspended. |
private BooleanLock |
waiting_
This tool is used to manage the wait/notify behavior of the control flow. |
Constructor Summary | |
HSMInterpreter(CompositeState root)
Sole Constructor. |
Method Summary | |
private void |
callEntry(State current,
State target,
State source)
Recursively calls State.entry(Context) on all states that are on a path between the current state and the
target state. |
private void |
callExit(State current,
State target,
State source)
Recursively calls exit() on all states that are on a
path between the target state and the source
state. |
void |
eventReceived(Event event)
|
private void |
fireTransition(InternalTransition trans)
|
protected void |
fireTransition(Transition trans)
|
Context |
getContext()
Returns the context that is associated with the interpreted state machine. |
private State |
getCurrentState()
Returns the state that is currently processed. |
CompositeState |
getRoot()
Returns the root state of the state machine that is being processed. |
private boolean |
processQueuedEvents(State state)
Processes pending events. |
private void |
processState(CompositeState state)
|
private void |
processState(FinalState state)
|
protected void |
processState(State state)
|
private boolean |
processTransitions(State state)
Fires a triggerless transition of the current state if the guard condition is satisfied. |
private boolean |
processTriggeredTransitions(State state,
Event event)
Processes triggered transitions. |
private void |
queueEvent(Event event)
|
void |
run()
Runs the interpretation of the state machine. |
void |
setContext(Context context)
Initializes the context that will be associated with all actions of the interpreted state machine. |
private void |
setCurrentState(State current)
|
private void |
setTriggerEvent(Event trigger)
|
void |
shutdown()
Signals the interpreter that it should stop the interpretation as soon as possible. |
void |
suspend()
Signals the interpreter that it should suspend the interpretation as soon as possible. |
String |
toString()
Overwritten for debugging purposes. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private static final Logger LOG
private final List eventQueue_
private final BooleanLock waiting_
private CompositeState root_
private State current_
private volatile boolean running_
false
the processing of the state machine will stop.
This is volatile
because it may be set/read by the main
thread and several other (event dispatching) threads.
private volatile boolean suspended_
volatile
because it may be set/read by the main
thread and several other (external) threads.
private Context context_
null
since a context is not needed always.
private boolean firedInternal_
Constructor Detail |
public HSMInterpreter(CompositeState root)
root
.
root
- The root state of the state machine that is to be processed
by this class.Method Detail |
public void eventReceived(Event event)
eventReceived
in interface EventListener
public CompositeState getRoot()
Interpreter
getRoot
in interface Interpreter
public void run()
Interpreter
run
in interface Interpreter
public void suspend()
Interpreter
suspend
in interface Interpreter
public void shutdown()
Interpreter
shutdown
in interface Interpreter
public String toString()
private void callEntry(State current, State target, State source)
current
state and the
target
state.
The path is a sequence of states that lie between current and target in
an inorder traversal of the state hierarchy.
current
- The state that is currently processed by this method.
This is replaced during recursion.target
- Terminates the sequence of states (and the recursion).source
- The beginning of the sequence of states.private void callExit(State current, State target, State source)
exit()
on all states that are on a
path between the target
state and the source
state.
The path is a sequence of states that lie between current and target in
an inorder traversal of the state hierarchy.
current
- The state that is currently processed by this method.
This is replaced during recursion.target
- Terminates the sequence of states (and the recursion).source
- The beginning of the sequence of states.private State getCurrentState()
private void fireTransition(InternalTransition trans)
protected void fireTransition(Transition trans)
protected void processState(State state)
private void processState(CompositeState state)
private void processState(FinalState state)
private boolean processTransitions(State state)
private boolean processQueuedEvents(State state)
Note: Access to the event queue is not synchronized here! This must be done externally.
state
- The state whose transitions will be checked.
true
if an event did trigger a transition,
false
else.private boolean processTriggeredTransitions(State state, Event event)
event
- The event that might be a trigger for a transition.state
- The state whose transitions will be checked.
true
if the event did trigger a transition,
false
else.private void queueEvent(Event event)
private void setCurrentState(State current)
public void setContext(Context context)
Interpreter
The context does not necessarily have to be set. There are cases in which a global context is not needed. However, if the context is set, it must remain fixed!
setContext
in interface Interpreter
context
- The context that will be associated with all actions.public Context getContext()
Interpreter
getContext
in interface Interpreter
private void setTriggerEvent(Event trigger)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |