State Machine Implementation

N

nilavya

HI,

I have got a C++ application involving State machine.

Now I have got too many states and too many events which changes the
state of the Staet machine. So my code is somewhat unmanageable. It is
:
switch(nState)
{
case state1:
{
switch(nEvent)
{
case event1:
break;
case event2:
break;
case event3:
default:
break;
}
}
break;
case state2:
break;
default:
break;
}

Now the problem I have is that some of the handling in various
states-events combo is almost same. That makes my code redundant.Also
the function size is toooo large.
Is there any way or design to write this code. I heard about writing a
seperate function for each state. But not sure???

Can Anyone help me in this....


Thanks.
With Regards,
Bhagat Nirav K.
 
A

anderberg

HI,

I have got a C++ application involving State machine.

Now I have got too many states and too many events which changes the
state of the Staet machine. So my code is somewhat unmanageable. It is

[... switch/case ...]

Maybe a search for "object oriented state machine" may give some
ideas?
 
M

mlimber

nilavya said:
HI,

I have got a C++ application involving State machine.

Now I have got too many states and too many events which changes the
state of the Staet machine. So my code is somewhat unmanageable. It is
:
switch(nState)
{
case state1:
{
switch(nEvent)
{
case event1:
break;
case event2:
break;
case event3:
default:
break;
}
}
break;
case state2:
break;
default:
break;
}

Now the problem I have is that some of the handling in various
states-events combo is almost same. That makes my code redundant.Also
the function size is toooo large.
Is there any way or design to write this code. I heard about writing a
seperate function for each state. But not sure???

Can Anyone help me in this....


Thanks.
With Regards,
Bhagat Nirav K.

Consider the State pattern from _Design Patterns_ by Gamma et al.

Cheers! --M
 
T

Tim Clacy

nilavya said:
HI,

I have got a C++ application involving State machine.

Now I have got too many states and too many events which changes the
state of the Staet machine. So my code is somewhat unmanageable. It is
switch(nState)
{
case state1:
{
switch(nEvent)
{
case event1:
break;
case event2:
break;
case event3:
default:
break;
}
}
break;
case state2:
break;
default:
break;
}

Now the problem I have is that some of the handling in various
states-events combo is almost same. That makes my code redundant.Also
the function size is toooo large.
Is there any way or design to write this code. I heard about writing a
seperate function for each state. But not sure???

Can Anyone help me in this....


Thanks.
With Regards,
Bhagat Nirav K.

Try and make the machine look like a table of data, since that is what it
is. For a given state:

switch (event)
{
case ev1: action1; nextstate1; break;
case ev2: action2; nextstate2; break;
:
case evn: actionn; nextstaten; break;
}

You might be able to make the states hierarchical and use one of the many
Harrel /UML statechart patterns (boost, quantum framework):

This is quite a clean and simple implementation:

A Lightweight Implementation of UML Statecharts in C++
http://www.codeproject.com/samples/Statechart.asp

Alternatively, there are 'C' / C++ code generators for state machines that
use a GUI front end (e.g. IAR VisualSTATE).
 
N

Neil Cerutti

HI,

I have got a C++ application involving State machine.

Now I have got too many states and too many events which
changes the state of the Staet machine. So my code is somewhat
unmanageable. It is:

switch(nState)
{
case state1:
{
switch(nEvent)
{
case event1:
break;
case event2:
break;
case event3:
default:
break;
}
}
break;
case state2:
break;
default:
break;
}

Now the problem I have is that some of the handling in various
states-events combo is almost same. That makes my code
redundant.

Any part of the code that's truly redundant can be moved to a
function that all the states can rely on.
Also the function size is toooo large. Is there any way or
design to write this code. I heard about writing a seperate
function for each state. But not sure???

You can define each state as a function, yes.

What you need is a function type that returns a pointer
to function of it's own type. This is not possible to
declare directly in C++.

Here's a finite state machine, which is implemented using
functions, that recognizes the word "cat". A struct is used to
simulate a recursive function type.

#include <iostream>

struct state_t;

typedef state_t state(char c);

struct state_t
{
state_t(state* p): process(p) { }
state *function;
};

state_t start(char);
state_t got_c(char);
state_t got_a(char);
state_t terminal(char);

state_t start(char c)
{
if (c == 'c') return state_t(got_c);
else return state_t(start);
}

state_t got_c(char c)
{
if (c == 'a') return state_t(got_a);
else if (c == 'c') return state_t(got_c);
else return state_t(start);
}

state_t got_a(char c)
{
if (c == 't') return state_t(terminal);
else if (c == 'c') return state_t(got_c);
else return state_t(start);
}

state_t terminal(char)
{
return state_t(0);
}

int main ()
{
char m[]="The fat cat sits on the hat.";
state_t machine = start;
int i;
int len = strlen(m);
for (i = 0; i < len && machine.function != terminal; ++i) {
machine = machine.function(m);
}
if (i != len) {
std::cout << "found \"cat\" at " << i-2 << '\n';
} else {
std::cout << "\"cat\" not found.\n";
}
return 0;
}

It might be more efficient on your implementation to use generic
function pointers to simulate the recursive return type. I didn't
do that here since I find it more confusing.
 
N

Neil Cerutti

Any part of the code that's truly redundant can be moved to a
function that all the states can rely on.


You can define each state as a function, yes.

What you need is a function type that returns a pointer
to function of it's own type. This is not possible to
declare directly in C++.

Here's a finite state machine, which is implemented using
functions, that recognizes the word "cat". A struct is used to
simulate a recursive function type.

#include <iostream>

struct state_t;

typedef state_t state(char c);

struct state_t
{
state_t(state* p): process(p) { }
state *function;
};

Sorry that got garbled before I pasted.

struct state_t
{
state_t(state* p): function(p) { }
state *function;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top