473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

State Machine Implementation

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.

Dec 7 '05 #1
6 12509
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.
Dec 7 '05 #2
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

Dec 7 '05 #3
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

Dec 7 '05 #4
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).
Dec 7 '05 #5
On 2005-12-07, nilavya <ni*****@gmail.com> 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
Dec 7 '05 #6
On 2005-12-07, Neil Cerutti <le*******@email.com> wrote:
On 2005-12-07, nilavya <ni*****@gmail.com> 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
Dec 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Leonard J. Reder | last post by:
Hello list, I have been searching on the web for a while now for a specific Python implementation of an FSM. More specifically what I am looking for is a Python implementation of the so called...
0
by: Leonard J. Reder | last post by:
Hello, Although posted this a few weeks ago I still have gotten much feedback so I want to put this out again and see if the response gets a bit more interesting. I have been searching on the...
4
by: Tobin Harris | last post by:
Hi there, I was just wondering if anyone out there has done any apps with lots of UI Wizards? I'm currently finding myself doing loads of these, and am trying to find a nice way of making things...
6
by: Ian Williamson | last post by:
Greetings, My company has an ASP.NET based enterprise product that is undergoing some changes and I need some community input to help solve a problem. In the current implementation, any given...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
4
by: libster | last post by:
Hello, I'm writing code and I have a scenario where I'm waiting to receive a msg and a timeout message at the same time. For example, I've set a timer so that if I don't receive a message in a...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
3
by: galathaea | last post by:
it surprises me how often engineers confuse states with actions i think this is the fundamental reification behind procedural statemess and this mistake infects a lot of great projects with...
4
by: laktofil | last post by:
This may seem like an abstract question on behavioral inheritance. Anyway, I'm building a hierarchical state machine in C++ (with gcc for target platform Gentoo Linux). More precisely, I'm using this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.