【问题标题】:Help for Boost.Statechart bugBoost.Statechart 错误帮助
【发布时间】:2009-06-26 12:36:14
【问题描述】:

任何想法如何解决这个问题?

在 ubuntu 8.10 w/g++ 4.3.2 上使用 1.39_0

在下面的状态图中,短语“BUGGY”打印了三个 次。人们会期望该事件只会触发一个“BUGGY”。在 我正在从事的项目的情况,我无法返回 discard_event() 因为我需要事件达到多个状态(通常 深入一组正交状态)。如果有解决方法 可以应用而不是修改状态图,我想知道。

$ 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
越野车
越野车
越野车

编辑::

这是一个示例状态机,它显示了我在实际处理的更大的问题中遇到的问题。我知道top会转发evSay。请注意,c1、c2、c3 不会对 evSay 做出反应。这是一个我需要转发的示例,以便两个状态可以对 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;
}

输出: 越野车
儿童反应
越野车
越野车

【问题讨论】:

    标签: c++ boost-statechart


    【解决方案1】:

    将您想要的反应下移到top 状态的子状态。这使它脱离了forward_state 事件的行列。我没有将它实现为内部类型,但你可以。

    #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
    儿童反应
    家长反应

    【讨论】:

      【解决方案2】:

      人们会期望该事件只会 触发一个“BUGGY”。

      我不会说实话。您指定三个正交状态,并在外部状态上定义 react()。据我了解(*)你有三个最里面的状态,只要你继续转发事件,所有三个都会被处理。

      这三个中的每一个都没有react,所以它在外部状态下寻找react,并在top中找到一个并调用它,然后结果是forward任意选择下一个尚未访问过的最内层状态,同样适用。

      我不能像我一样返回discard_event() 需要事件到达多个 状态(通常在正交的深处 状态集)。

      但这正是您在这里实现的目标。你是说你想达到多个状态,但你不希望这些状态的 react() 触发?老实说,这没有多大意义。

      很难给出建议,因为您显然不想打印一次“越野车”,您到底想达到什么目的?

      (*) 作为记录,我只是在 state_machine 是新的时候玩过一点点,从未在生产代码中使用过它,所以我根本不是专家

      【讨论】:

      • 我希望 TOP 对 evSay 做出反应。我希望其他州也能够对 evSay 做出反应。但我不希望每个子州都继承 Top 对 evSay 的反应。您会注意到我的代码对 c1、c2 或 c3 中的 evSay 没有反应。
      • 所以您基本上是在说您正在转发 evSay 但并不真的希望转发?你要么放弃,要么接受转发实际上会被转发到下一个队列...
      • 查看编辑。为什么您认为将事件 (evSay) 转发到不对其做出反应的状态 (c1, c2, c3) 应该导致执行父母的反应?如果没有响应的事件重新启动机器,BUGGY 不应该打印 4 次吗? (上面一个,三个没有反应的孩子每人一个)
      猜你喜欢
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2011-09-02
      • 2011-05-12
      • 2011-04-17
      相关资源
      最近更新 更多