Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > State Machine Implementation

Reply
Thread Tools

State Machine Implementation

 
 
nilavya
Guest
Posts: n/a
 
      12-07-2005
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.

 
Reply With Quote
 
 
 
 
anderberg
Guest
Posts: n/a
 
      12-07-2005
On 2005-12-07, nilavya wrote:
> 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?

--
anderberg: People are just mutations. Some worse than others.
 
Reply With Quote
 
 
 
 
EventHelix.com
Guest
Posts: n/a
 
      12-07-2005
Use a Hierarchical State Machine. Checkout the following link:

http://www.eventhelix.com/RealtimeMa...ateMachine.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      12-07-2005
nilavya wrote:
> 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

 
Reply With Quote
 
Tim Clacy
Guest
Posts: n/a
 
      12-07-2005
nilavya wrote:
> 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).


 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      12-07-2005
On 2005-12-07, nilavya <(E-Mail Removed)> wrote:
> 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[i]);
}
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.

--
Neil Cerutti
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      12-07-2005
On 2005-12-07, Neil Cerutti <(E-Mail Removed)> wrote:
> On 2005-12-07, nilavya <(E-Mail Removed)> wrote:
>> 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;
> };


Sorry that got garbled before I pasted.

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

--
Neil Cerutti
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
State machine - Vending machine - strange behaviour fenster VHDL 3 12-23-2011 09:53 AM
state machine implementation Mark C Programming 4 12-11-2010 02:31 PM
state machine implementation (similar states) Enno Luebbers VHDL 3 09-07-2005 03:51 PM
State machine: how to stay in a state? David Lamb VHDL 1 09-15-2003 05:24 PM



Advertisments