PROBLEM:
aissa@bnr.ca (Al Issa)
I am looking for some information regarding implementing a Finite State
Machine base class in C++. There seems to be some innate problems with
inheriting from FSMs.
RESPONSE:
rmartin@rcmcon.com (Robert Martin), 21 Jun 93
There are indeed problems with inheriting from FSMs. However, you can
decompose an FSM into two component parts, one of which CAN support
derivation.
When an FSM receives an event, it undergoes a transition. Associated
with every transition is a set of behaviors. If we gather all the
behaviors into a class, then this class harbors all the functionality
of the FSM with none of the sequencing or control. I call this class
the Context. The Context of a finite state machine can be extended
through inheritance.
The other part of an FSM is the control element. This can be created
as a single inheritance hierarchy; a base class for all the states
which has virtual functions for all the events. Each state is
supported by its own derivation from the base state, and all the
virtual event functions are overridden.
Inheriting anything from these classes which implement the control
element of the FSM is pretty meaningless. But there is very little
code withing these classes. They represent the "wiring" which
controls the sequencing of the execution of the behaviors within the
context.
New FSMs can be created from old ones by derivation, IF the context
and control are separated. The context can be extended with new
capabilities, and then given a new set of control elements.
I have a simple yacc/lex program for DOS and UNIX which converts a
simple textual representation of a State/Transition table into a set
of classes which obey the above model. Anyone interested, just drop
me a line and I'll email it to you.
[ I have the latest. -adc ]