State Machine Implementation

Discussion in 'C++' started by nilavya, Dec 7, 2005.

  1. nilavya

    nilavya Guest

    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.
    nilavya, Dec 7, 2005
    #1
    1. Advertising

  2. nilavya

    anderberg Guest

    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.
    anderberg, Dec 7, 2005
    #2
    1. Advertising

  3. EventHelix.com, Dec 7, 2005
    #3
  4. nilavya

    mlimber Guest

    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
    mlimber, Dec 7, 2005
    #4
  5. nilavya

    Tim Clacy Guest

    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).
    Tim Clacy, Dec 7, 2005
    #5
  6. nilavya

    Neil Cerutti Guest

    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(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.

    --
    Neil Cerutti
    Neil Cerutti, Dec 7, 2005
    #6
  7. nilavya

    Neil Cerutti Guest

    On 2005-12-07, Neil Cerutti <> wrote:
    > 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(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
    Neil Cerutti, Dec 7, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lamb
    Replies:
    1
    Views:
    680
  2. Weng Tianxiang
    Replies:
    7
    Views:
    1,105
    Mike Treseler
    Nov 25, 2003
  3. Enno Luebbers
    Replies:
    3
    Views:
    936
    Ben Jones
    Sep 7, 2005
  4. Mark

    state machine implementation

    Mark, Dec 9, 2010, in forum: C Programming
    Replies:
    4
    Views:
    445
    Jorgen Grahn
    Dec 11, 2010
  5. fenster
    Replies:
    3
    Views:
    1,159
    jeppe
    Dec 23, 2011
Loading...

Share This Page