finite state machine : transitions in absence of external events
Hello,
I am desigining a FSM (plan to use C++ to implement it). One of the
issues I am encountering is dealing with transient states ex:
A----B--------C-----------D-------E
| |
| |
| |
|
The transition from A-B happens because of some external event (like
timer expiration).
The transition from B-C is always immediate, basically state B exists
to perform some actions before entering state C. Its also possible
that FSM starts off with initial state as B instead of A which is
why
state B is needed. The transition from C-D is again immediate, C
executes some actions before entering state D. The reason we have
state C is because we need to reexecute these actions when we
transition from E to C(loopback)
Transition from D to E and from E to C are based on external events
(timer events).
(Transition from C to E not possible)
So in above FSM A,D,E are states that transition to next state only
on receipt of some external event while states B,C are transient/
temporary states that exist only to execute some actions before
transitioning to next state. One way I could think of achieving this
is to provide entry/exit routines for each of the state. Essentially
B::onEntry() {
do B's actions
moveToState(C)
}
B::onExit() {
//Do nothing or if there are any exit actions for B
}
C::onEntry() {
do C's actions
moveToState(D)
}
C::onExit() {
//Do nothing or if there are any exit actions for C
}
..............
//Some class that has pointer to currentState
moveToState(newState) {
currentState->exit();
currentState = newState;
newState->entry();
}
This was only solution I could think of. What I dont like about what
happens when we move from say A-B, we finally end in D (A-B,B-C,C-D)
and then at runtime call graph looks like: procesTimeoutInA()-
B::entry
()->B's actions->B()::exit()- (B'S::exit called from with B::s entry
()!!)->C::entry()->C's actions->C's::exit() (again C's exit is called
from C's entry)->D's::entry() and unwind in the opposite order. Is
there any better way to handle this?
Thanks Much,
Sunil