Boost.Statechart 错误帮助

发布于 2024-07-25 17:39:00 字数 3576 浏览 5 评论 0原文

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

在 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
越野车
越野车
BUGGY

编辑::

这是一个示例状态机,它显示了我在实际处理的更大状态机中遇到的问题。 我知道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;
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-08-01 17:39:01

人们预计该事件只会
触发一个“BUGGY”。

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

这三个中的每一个都没有反应,因此它会在外部状态中寻找反应,并在 top 中找到一个反应并调用它,然后结果是一个 forward< /code> 任意选择下一个尚未访问的最内层状态,这同样适用。

我无法返回discard_event(),因为我
需要事件达到多个
状态(通常深入正交
状态集)。

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

很难提供建议,因为您显然不想打印一次“buggy”,您到底想实现什么目标?

(*) 郑重声明,我只在 state_machine 是新的时候稍微使用过它,从未在生产代码中使用过它,所以我根本不是专家

One would expect the event would only
trigger one "BUGGY".

I wouldn't to be honest. You specify three orthogonal states, with a react() defined on an outer state. As I understand it (*) you have three innermost states, as long as you keep forward'ing the event, all three will be processed.

Each of those three doesn't have a react, so it goes looking for a react in a outer state, and finds one in top and calls it, then as the result is a forward arbitrarily selects the next not yet visited innermost state, for which the same applies.

I cannot return discard_event() as I
need the event to reach multiple
states (usually deep in an orthogonal
set of states).

But that's exactly what you're achieving here. You're saying you want to reach multiple states, but you don't want the react() of those states to fire? That doesn't make much sense to be honest.

It's hard to give advice as you obviously are not trying to print 'buggy' once, what exactly are you trying to achieve?

(*) for the record, I only have played with state_machine a little bit when it was new, never used it in production code, so I'm not at all an expert

梦里°也失望 2024-08-01 17:39:00

将您想要的反应向下移动到 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
儿童反应
家长的反应

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文