Search code examples
c++boost-statechart

Help for Boost.Statechart bug


Any ideas how to fix this?

using 1.39_0 on ubuntu 8.10 w/g++ 4.3.2

In the following statechart, the phrase "BUGGY" is printed three times. One would expect the event would only trigger one "BUGGY". In the case of the project I am working on, I cannot return discard_event() as I need the event to reach multiple states (usually deep in an orthogonal set of states). If there is a workaround that can be applied instead of modifying statechart, I would like to know.

$ cat bug.cpp

#include <boost/intrusive_ptr.hpp>
#include <boost/mpl/list.hpp>    #include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/state_machine.hpp>
#include <iostream>

using namespace std;
namespace sc = boost::statechart;
namespace mpl = boost::mpl;

struct evSay : sc::event<evSay>{ };
struct top;
struct c1;
struct c2;
struct c3;
struct sm : public sc::state_machine<sm,top> { };

struct top : sc::simple_state<top,sm,mpl::list<c1,c2,c3> > {
       typedef sc::custom_reaction<evSay> reactions;
       sc::result react(const evSay &) {
               cout<<"BUGGY"<<endl;
               return forward_event();
       }
};

struct c1 : sc::simple_state <c1, top::orthogonal<0> > { };

struct c2 : sc::simple_state <c2, top::orthogonal<1> > { };

struct c3 : sc::state <c3, top::orthogonal<2> > {
       c3( my_context  ctx) : my_base(ctx) {
               post_event( boost::intrusive_ptr< evSay > (
                               new evSay() ) );
       }
};

int main() {
       sm* fsm = new sm();
       fsm->initiate();
       delete fsm;
       return 0;
}

$ g++ bug.cpp && ./a.out
BUGGY
BUGGY
BUGGY

EDIT::

This is an example Statemachine that shows my problem that I run into in my much larger one I actually working on. I know that top will forward evSay. Note that c1,c2,c3 do not react to evSay. Here is an example where I need forwarding so that two states may react to evSay.

#include <boost/intrusive_ptr.hpp>    
#include <boost/mpl/list.hpp>    
#include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/state_machine.hpp>    
#include <iostream>
using namespace std;
namespace sc = boost::statechart;
namespace mpl = boost::mpl;
namespace BUG {
struct evSay : sc::event<evSay>{ };
struct top;struct c1;struct c2;struct c3;struct c2_1;

struct sm : public sc::state_machine<sm,top> { };

struct top : sc::simple_state<top,sm,mpl::list<c1,c2,c3> > {
    typedef sc::simple_state<top,sm,mpl::list<c1,c2,c3> > my_type;
    typedef sc::custom_reaction<evSay> reactions;
    sc::result react(const evSay &) {
        cout<<"BUGGY"<<endl;
        return forward_event();
    }
};

struct c1 : sc::simple_state <c1, top::orthogonal<0> > { };
struct c2 : sc::simple_state <c2, top::orthogonal<1>, c2_1 > { };
struct c3 : sc::state <c3, top::orthogonal<2> > {
    c3( my_context  ctx) : my_base(ctx) {
        post_event( boost::intrusive_ptr< evSay > (
                new evSay() ) );
    }
};

struct c2_1 : sc::simple_state<c2_1, c2 > {
    typedef sc::custom_reaction<evSay> reactions;
    sc::result react(const evSay &) {
        cout<<"CHILD REACTION"<<endl;
        return forward_event();
    }
};
}

int main()
{
    BUG::sm* fsm = new BUG::sm();
    fsm->initiate();
    delete fsm;
    return 0;
}

output: BUGGY
CHILD REACTION
BUGGY
BUGGY


Solution

  • Move the reaction you want down to a child state of the top state. This takes it out of the line of forward_state events. I didn't implement it as a inner-type, but you could.

    #include <boost/intrusive_ptr.hpp>    
    #include <boost/mpl/list.hpp>    
    #include <boost/statechart/custom_reaction.hpp>
    #include <boost/statechart/event.hpp>
    #include <boost/statechart/simple_state.hpp>
    #include <boost/statechart/state.hpp>
    #include <boost/statechart/state_machine.hpp>    
    #include <iostream>
    using namespace std;
    namespace sc = boost::statechart;
    namespace mpl = boost::mpl;
    
    
    struct evSay : sc::event<evSay>{ };
    
    struct top;struct c1;struct c2;struct c3;struct c2_1;struct sub_1;
    
    struct sm : public sc::state_machine<sm,top> { };
    
    struct top : sc::simple_state<top,sm, mpl::list<c1,c2,c3, sub_1> > { };
    
    struct sub_1 : sc::simple_state<sub_1, top::orthogonal<3> > {
        typedef sc::custom_reaction<evSay> reactions;
        sc::result react(const evSay &) {
            cout<<"PARENT REACTION"<<endl;
            return forward_event();
        }
    };
    
    struct c1 : sc::simple_state <c1, top::orthogonal<0> > { };
    struct c2 : sc::simple_state <c2, top::orthogonal<1>, c2_1 > { };
    struct c3 : sc::simple_state <c3, top::orthogonal<2> > { };
    
    struct c2_1 : sc::simple_state<c2_1, c2 > {
        typedef sc::custom_reaction<evSay> reactions;
        sc::result react(const evSay &) {
            cout<<"CHILD REACTION"<<endl;
            return forward_event();
        }
    };
    
    int main()
    {
        sm* fsm = new sm();
        fsm->initiate();
        fsm->process_event(  evSay()  );
        delete fsm;
        return 0;
    }
    

    ~$g++ test.cpp -I Downloads/boost_1_45_0
    ~$./a.out
    CHILD REACTION
    PARENT REACTION