0

The processing done inside a state depends on whether another state is active or not. How can I determine if a specific state is Active or not in boost-msm?

I had sth like the following pseudo code in mind:

auto state = fsm.get_state<MyFsm_::State_x&>(); bool state_Status = state.isActive();

qwa
  • 123
  • 10
  • What do you want to know? Simply, getting active state from outside of fsm? I'm not sure what "The processing done inside a state" mean. Is that action of the transition? – Takatoshi Kondo Jul 23 '18 at 13:16
  • I want to simply check if a state is active (from inside the SM). In my usecase i want to do sth like: `if(stateX==active){ do this ... & transition to stateY} else{ do that ... & transition to stateZ}` – qwa Jul 24 '18 at 05:56
  • You can get state index using ` const int* current_state()` https://www.boost.org/doc/libs/1_67_0/libs/msm/doc/HTML/ch03s05.html#d0e2340. State index is defined as the following order https://www.boost.org/doc/libs/1_67_0/libs/msm/doc/HTML/ch06s03.html#internals-state-id. – Takatoshi Kondo Jul 24 '18 at 07:10
  • You mentioned "from inside the SM", when the statement start with "if(stateX==actime){ ..." is triggered? I think that "inside the SM" means action, e.g.entry/exit, transition action. By the way, how many state-machine instance are you using in this case? – Takatoshi Kondo Jul 24 '18 at 07:16
  • `fsm.current_state()` returns an array of ints (i.e. active state IDs). How can i use it when I dont know the size of the array? – qwa Jul 24 '18 at 10:28
  • I don't know. Maybe if the state is determined, you can know the depth of sub-machine, and the depth is the number of elements. By the way, `current_state()` is for debug purpose. I don't recommend use it to determine transition. Basically, when you use Boost.MSM, you shouldn't depends on the querying active state. If your process is the action or guard, you can know the active state via the template parameter. – Takatoshi Kondo Jul 24 '18 at 14:31
  • I studied the number of current_state(). It is a number of orthogonal states of the fsm. – Takatoshi Kondo Aug 15 '18 at 12:57
  • Here is the code that demonstrates orthogonal state and current_state() https://wandbox.org/permlink/M0ctToFln7ATfFYM `sm1.current_state()[2]` is undefined. – Takatoshi Kondo Aug 15 '18 at 12:58

1 Answers1

0

You can have a look at the link (about Flags & Getting a state): https://www.boost.org/doc/libs/1_64_0/libs/msm/doc/HTML/ch03s05.html#d0e2489

However, boost-msm is fast but very complicated as well. It will make your code hard to understand if not used properly.

I recommend you to use the State Pattern.

Wei Guo
  • 544
  • 3
  • 15
  • Using a flag requires me to define an additional struct to be used as a "Flag" for each state (e.g. `struct S1Active{}`) & then assigning the flag to each state (i.e. `typedef mpl::vector flag_list`). I thought boost-MSM would have devised a better way for such a usecase (which i guess would be common). One could alternatively use a `bool isActive` inside the state and check it as required (`fsm.get_state().isActive`). **Any other/better solution is highly appreciated**. – qwa Jul 23 '18 at 11:31
  • @Guo: Your second note (_use the state pattern_) is important: I have been advised to use a mature state-machine implementation that is well tested **for production** rather than implementing the "design patern" from scratch by myself. _I would like other comments on this issue_ ... thanks in advance. – qwa Jul 23 '18 at 11:44