[Qt-interest] Going 'back' in a state machine?
K. Frank
kfrank29.c at gmail.com
Fri Nov 5 16:30:18 CET 2010
Hello Steve -
(Uh-oh! Here comes more philosophy!)
On Fri, Nov 5, 2010 at 9:44 AM, Stephen Kelly <steveire at gmail.com> wrote:
>
> Hi,
>
> I have a state machine which I transition through, and I have the
> requirement that it must be possible to 'go back' through states. I thought
> about using QHistoryState for that, but I don't think it's meant for that
> because it only records transitions the state when a parent is left, so when
> I transition between siblings, the QHistoryState doesn't bring me anything.
I guess the first question is "Why?"
Off the cuff, I would look at ways people implement "undo / redo" stacks,
such as in text editors.
This is not a natural (to me) part of state machines, and I'm not aware of
this kind of functionality being built in to Qt's state-machine framework.
To me, the power of state machines is that the state records everything
you need to know about the state of your system, and let's you explicitly
forget the path by which you got there. This is a big benefit, not a
limitation.
(If you replace a state machine with nested if-then statements in procedural
code, your state is now a combination of the line of code that you are
executing,
together with the values of certain, not necessarily well-specified variables.
The point is that in terms of your ideal program logic, you can be in the same
state even though you are at a different branch in your if-then code:
the line of
code you're executing retains partial, irrelevant memory of the path by which
you got to that state, and, in practice, this turns out to be a big
complication.)
> Has anyone else solved this problem already? I'm thinking I probably need to
> implement QAbstractState to do some magic.
>From my "purist's" point of view, I would look to implementing your go-back
functionality as a separate layer on top of your state machine, rather than
try to build it into your state machine, or into the state-machine framework.
(But it really depends on what you are trying to do.)
(And yes, this is not what the history pseudo-state in hierarchical state
machines (HSM's, "statecharts") does or is meant for.)
> All the best,
>
> Steve.
Best to you.
K. Frank
More information about the Qt-interest-old
mailing list