首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost.Statechart错误的帮助

Boost.Statechart错误的帮助
EN

Stack Overflow用户
提问于 2009-06-26 12:36:15
回答 2查看 1.2K关注 0票数 1

有什么办法解决这个问题吗?

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

在下面的statechart中,短语"BUGGY“被打印了三次。有人会认为这个事件只会触发一个"BUGGY“。在我正在处理的项目中,我不能返回discard_event(),因为我需要事件到达多个状态(通常在一组正交的状态中)。如果有可以应用而不是修改statechart的解决方法,我想知道。

$ cat bug.cpp

代码语言:javascript
复制
#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

编辑::

这是一个状态机的例子,它展示了我在实际工作中遇到的更大的问题。我知道top会转发evSay。请注意,c1、c2、c3不会对evSay做出反应。这是一个我需要转发的示例,以便两个状态可以对evSay做出反应。

代码语言:javascript
复制
#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;
}

输出: BUGGY

子反应

BUGGY

BUGGY

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-07 14:34:05

将所需的反应向下移动到top状态的子状态。这使它脱离了forward_state事件的范围。我没有把它实现为一个内部类型,但是你可以。

代码语言:javascript
复制
#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

子反应

父母反应

票数 1
EN

Stack Overflow用户

发布于 2009-06-26 13:40:53

One预计该事件只会触发一个"BUGGY“。

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

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

我不能返回discard_event(),因为我需要事件到达多个状态(通常在一组正交的状态中)。

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

很难给出建议,因为你显然没有尝试打印“buggy”一次,你到底想要实现什么?

(*)郑重声明,我只在state_machine刚出现的时候用过一点,从来没有在产品代码中使用过它,所以我根本不是专家

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1048904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档