[MSM] Best place to execute an action with return value
Hi, I'm diving into this very useful piece of code which is MSM and I'm modelling a state machine for a gsm modem. When I have to perform a call I need to move from the IDLE state to WAITING_RESPONSE and, in the transaction action I have to call the MakeCall() method on my GSM object. This method has a return value and a non 0 value means it is failed. In this situation the SM don't have to move on to the next state but remains in the IDLE one, so... Since the Actions has no return value, I have to use a Guard instead or I have to add another state called MAKE_CALL? Thanks in advance, Daniele.
Hi,
I'm diving into this very useful piece of code which is MSM and I'm modelling a state machine for a gsm modem.
When I have to perform a call I need to move from the IDLE state to WAITING_RESPONSE and, in the transaction action I have to call the MakeCall() method on my GSM object.
This method has a return value and a non 0 value means it is failed. In this situation the SM don't have to move on to the next state but remains in the IDLE one, so...
Since the Actions has no return value, I have to use a Guard instead or I have to add another state called MAKE_CALL?
Thanks in advance, Daniele.
Hi, it depends how your code is structured. According to UML, guards should not execute any actions (just return a bool), but MSM will let you do. The question is, is MakeCall a long-lasting, blocking function? If yes then you're blocking the run-to-completion (the state machine stays long in an unstable state as it processes a transition), and it has bad side-effects (for example debugging and loggin). In such a case, yes you should have an extra state. If MakeCall is fast, then a possible solution would be to use a guard as you suggested. The guard would execute MakeCall, then return true or false, false would stop the transition at this point. If more transitions with the same event and source state are found in the transition table above this one, they would be tried then, which allows you to continue processing, add error handling, etc. HTH, Christophe
Il 27/11/2013 21:23, Christophe Henry ha scritto:
Hi,
I'm diving into this very useful piece of code which is MSM and I'm modelling a state machine for a gsm modem.
When I have to perform a call I need to move from the IDLE state to WAITING_RESPONSE and, in the transaction action I have to call the MakeCall() method on my GSM object.
This method has a return value and a non 0 value means it is failed. In this situation the SM don't have to move on to the next state but remains in the IDLE one, so...
Since the Actions has no return value, I have to use a Guard instead or I have to add another state called MAKE_CALL?
Thanks in advance, Daniele.
Hi,
it depends how your code is structured. According to UML, guards should not execute any actions (just return a bool), but MSM will let you do. The question is, is MakeCall a long-lasting, blocking function? If yes then you're blocking the run-to-completion (the state machine stays long in an unstable state as it processes a transition), and it has bad side-effects (for example debugging and loggin). In such a case, yes you should have an extra state.
If MakeCall is fast, then a possible solution would be to use a guard as you suggested. The guard would execute MakeCall, then return true or false, false would stop the transition at this point. If more transitions with the same event and source state are found in the transition table above this one, they would be tried then, which allows you to continue processing, add error handling, etc.
HTH, Christophe
Hi Christophe, thanks for your reply (and thanks a lot for this great library!). I ask you another quick thing (also if I should open another topic): using a functor front-end, how can I add more method to the states (Eg. to add the visitor pattern)? Thanks, Daniele
HTH, Christophe
Hi Christophe, thanks for your reply (and thanks a lot for this great library!).
I ask you another quick thing (also if I should open another topic): using a functor front-end, how can I add more method to the states (Eg. to add the visitor pattern)?
Thanks, Daniele
Hi, the link is there: http://svn.boost.org/svn/boost/trunk/libs/msm/doc/HTML/ch03s05.html#d0e2433 But at the moment, you can define only one accept method. Don't forget to make all states and front-end inherit the same base state. HTH, Christophe
Hi Christophe, thanks for your reply (and thanks a lot for this great library!).
I ask you another quick thing (also if I should open another topic): using a functor front-end, how can I add more method to the states (Eg. to add the visitor pattern)?
Thanks, Daniele
Hi,
the link is there: http://svn.boost.org/svn/boost/trunk/libs/msm/doc/HTML/ch03s05.html#d0e2433 But at the moment, you can define only one accept method. Don't forget to make all states and front-end inherit the same base state.
Thanks a lot! Daniele.
participants (2)
-
Christophe Henry
-
Daniele Barzotti