[statechart] Getting state information into the machine
I'm using the Statechart Library in conjunction with the really exciting Asio Library to set up a communication server. There is a very helpful documentation of the Statechart Library too. But one topic is puzzling me: How to to get data into the state machine? Here is an simple example: Each session object of the server has two states: signed-off and authorized. Receiving a login event leads to a transition to the authorized state. Thats pretty easy. But the login data (stored in the login event) got lost. I can hold this data in the state machine itself (outermost_context()). Or I can use a pseudo event to forward this data to the authorized state. But I'm looking for a nifty way to avoid this overhead. I like this concept of state-local storage very much. But I'm not shure about how to use it.
Hi
Here is an simple example: Each session object of the server has two states: signed-off and authorized. Receiving a login event leads to a transition to the authorized state. Thats pretty easy. But the login data (stored in the login event) got lost. I can hold this data in the state machine itself (outermost_context()). Or I can use a pseudo event to forward this data to the authorized state. But I'm looking for a nifty way to avoid this overhead.
Your observations are correct. Provided that you absolutely want to transition when such an event arrives, then these are currently the only ways to make the event available to the destination state. Which way is best depends on your situation but in your example I'd probably simply post the incoming event and have it picked up with an in-state reaction in the destination state.
I like this concept of state-local storage very much. But I'm not shure about how to use it.
Maybe you've noticed the existence of BOOST_STATECHART_RELAX_TRANSITION_CONTEXT? Currently, this doesn't help you. However, one way to solve your problem would be to generalize the relaxing concept, so that you can specify any function that happens to accept the right type of event and also is a member of any state in the source or destination configuration. I've tried to implement this before, but abandoned the idea because it makes at lot of heavily templated code much more complex. Maybe I'll try again but don't count on getting this feature soon! HTH, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.
Thanx for the answer. I would like to say something general in this context.
The Statechart Library is well implemented and a good sample for the
flexibility and extensibility of C++. There are only a few languages that
could define a state_cast<> operator for example.
But there are also some drawbacks. States are modelled as separate
structs/classes. Dont't understand me wrong. This is a bright idea. But
programming would be easier when states could be a property of the objects.
With other words, it would be nice, if states would be supported direct by
the language.
Some time ago, I have dealt with an experimental language implementation
where states are a part of the signature of a method. I'm afraid this is
beyond the scope of possibilities of a library - even in C++. But who knows!
Thanks for your helpfull answer again.
Sylko!
"Andreas Huber"
Hi
Here is an simple example: Each session object of the server has two states: signed-off and authorized. Receiving a login event leads to a transition to the authorized state. Thats pretty easy. But the login data (stored in the login event) got lost. I can hold this data in the state machine itself (outermost_context()). Or I can use a pseudo event to forward this data to the authorized state. But I'm looking for a nifty way to avoid this overhead.
Your observations are correct. Provided that you absolutely want to transition when such an event arrives, then these are currently the only ways to make the event available to the destination state. Which way is best depends on your situation but in your example I'd probably simply post the incoming event and have it picked up with an in-state reaction in the destination state.
I like this concept of state-local storage very much. But I'm not shure about how to use it.
Maybe you've noticed the existence of BOOST_STATECHART_RELAX_TRANSITION_CONTEXT? Currently, this doesn't help you. However, one way to solve your problem would be to generalize the relaxing concept, so that you can specify any function that happens to accept the right type of event and also is a member of any state in the source or destination configuration. I've tried to implement this before, but abandoned the idea because it makes at lot of heavily templated code much more complex. Maybe I'll try again but don't count on getting this feature soon!
HTH,
-- Andreas Huber
When replying by private email, please remove the words spam and trap from the address shown in the header.
participants (2)
-
Andreas Huber
-
Commander Pirx