Dear All,
I'm new to C++.
How can I implement a State Machine in C++ ?
I'm getting only the messages and should know the next state.
What is the proper structure I need to use ?
Thanks, Igal
Dear All,
I'm new to C++.
How can I implement a State Machine in C++ ?
I'm getting only the messages and should know the next state.
What is the proper structure I need to use ?
Thanks, Igal
For simple state machines you can just use a switch statement inside a loop, e.g.
for (;;)
{
switch (state)
{
case STATE_1:
// do stuff
// maybe change state
break;
case STATE_2:
// do stuff
// maybe change state
break;
case STATE_3:
// do stuff
// maybe change state
break;
// ...
}
}
Unless you are implementing a state machine for the sake of implementing one, I would highly recommend that you use a state machine generator instead. Ragel is one very good and robust solution.
switch(currentState) {
state1:
currentState = NEXT_STATE2;
break;
....
}
typedef std::pair<State,Message> StateMessagePair;
typedef std::map<StateMessagePair,State> StateDiagram;
StateDiagram sd;
// add logic to diagram
...
State currentState = getInitialState();
...
// process message
Message m = getMessage();
StateDiagram::iterator it=sd.find(std::make_pair<currentState,m>));
if (it==sd.end()) exit("incorrect message");
currentState = it->second;
EDIT: Building up the state diagram is done like this (example is for a cola-vending machine):
StateDiagram.insert(std::make_pair(State::Idle ,Message::MakeChoice ),State::WaitingForMoney);
StateDiagram.insert(std::make_pair(State::WaitingForMoney,Message::Cancel ),State::Idle);
StateDiagram.insert(std::make_pair(State::WaitingForMoney,Message::MoneyEntered ),State::FindCan);
StateDiagram.insert(std::make_pair(State::FindCan ,Message::CanSentToUser),State::Idle);
Default actions can be implemented using a second map, where the key is only the State, like this:
typedef std::map<State,State> StateDiagramForDefaults;
Instead of printing "incorrect message", the logic can perform a lookup in the StateDiagramForDefaults.
If actions needs to be added to the state diagram, the value of the map should be a pair consisting of an action, and a new state, like this:
typedef std::pair<State,Message> StateMessagePair;
typedef std::pair<State,IAction *> StateActionPair;
typedef std::map<StateMessagePair,StateActionPair> StateDiagram;
The logic that builds up the diagram should then "new" an instance of a class that implements IAction, and put that in the StateDiagram.
The executing logic then just executes the IAction implementation via a virtual method (e.g. execute() or the ()-operator).
One way is to use a class like this (rough example code ahead):
class State
{
//pass a new Message into the current State
//current State does (state-specific) processing of
//the message, and returns a pointer to the new State
//if there's a state change
virtual State* getNewState(const Message&) = 0;
};
class ExampleState
{
virtual State* getNewState(const Message& message)
{
switch (message.type)
{
case MessageType.Stop:
//change state to stopped
return new StoppedState();
}
//no state change
return 0;
}
};
On of the complications is whether states should be static flyweights, or whether they carry instance data and should therefore be newed and deleted.
Of course, boost have something in store for you: boost Statechart library. You can also find some nice tutorials there.
The standard state machine implementation techniques are:
If you are new to state machines and implementation in C or C++, I would recommend my Dr.Dobbs article "Back to Basics" available at http://www.ddj.com/184401737 (you need to click on the Print link at the top to conveniently read the text.)
None of the standard techniques is suitable for the hierarchical state machines (such as UML statecharts). If you are interested in the modern UML state machines, I'd recommend my 3-part Embedded.com article "A crash course in UML state machines" (http://www.embedded.com/design/testissue/215801043).
See http://cs.atu.edu/~morell/classes/4163/notes/fa.html for several different ways of building finite state machines.
You can use Ragel state machine compiler: http://www.complang.org/ragel/
the one i am using is based on this one
http://ehiti.de/machine_objects/ MACHine Objects
seems to be well optimized and is a hell of a lot less complex to use than boost Statechart
Gosh, it isn’t as complicated as it seems. State machine code is very simple and short.
Coding a state machines is just about trivial. The hard part is designing a state machine that behaves correctly in all possible cases.
But let us assume that you have the correct design. How do you code it?
Store the state in an attribute, myState perhaps.
Whenever you receive a message switch on the myState attribute to execute the code for that state.
3 In each state, switch on the message to execute the code for that state AND that message
So you need a nested switch statement
cStateMachine::HandleMessage( msg_t msg )
{
switch( myState ) {
case A:
switch( msg ) {
case M:
// here is the code to handle message M when in state A
...
Once you have this up and running, it is fun and easy to add more states and messages.