State Machine Implementation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nilavya

    #1

    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.

  • anderberg

    #2
    Re: State Machine Implementation

    On 2005-12-07, nilavya wrote:[color=blue]
    > 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[/color]

    [... switch/case ...]

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

    --
    anderberg: People are just mutations. Some worse than others.

    Comment

    • EventHelix.com

      #3
      Re: State Machine Implementation

      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

      Comment

      • mlimber

        #4
        Re: State Machine Implementation

        nilavya wrote:[color=blue]
        > 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.[/color]

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

        Cheers! --M

        Comment

        • Tim Clacy

          #5
          Re: State Machine Implementation

          nilavya wrote:[color=blue]
          > 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[color=green]
          >>[/color]
          > 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.[/color]

          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++


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


          Comment

          • Neil Cerutti

            #6
            Re: State Machine Implementation

            On 2005-12-07, nilavya <nilavya@gmail. com> wrote:[color=blue]
            > 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.[/color]

            Any part of the code that's truly redundant can be moved to a
            function that all the states can rely on.
            [color=blue]
            > 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???[/color]

            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(termina l);
            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.functio n != terminal; ++i) {
            machine = machine.functio n(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

            Comment

            • Neil Cerutti

              #7
              Re: State Machine Implementation

              On 2005-12-07, Neil Cerutti <leadvoice@emai l.com> wrote:[color=blue]
              > On 2005-12-07, nilavya <nilavya@gmail. com> wrote:[color=green]
              >> 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.[/color]
              >
              > Any part of the code that's truly redundant can be moved to a
              > function that all the states can rely on.
              >[color=green]
              >> 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???[/color]
              >
              > 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;
              > };[/color]

              Sorry that got garbled before I pasted.

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

              --
              Neil Cerutti

              Comment

              Working...