pgularski / pysm

Versatile and flexible Python State Machine library
http://pysm.readthedocs.io/
MIT License
73 stars 11 forks source link

store/restore of the state stack. #8

Open nevercast opened 4 years ago

nevercast commented 4 years ago

I'm working on an implementation where I store and restore the state stacks so that I can put my Microcontroller to sleep and on boot, reinitialize the state machine, then set the states back to what they were.

I tried to find if any of the states had persistent data, and it doesn't seem to be the case (unless I extend State myself and add it there). So it seems I would only need to restore:

        self.state_stack = Stack(maxlen=StateMachine.STACK_SIZE)
        self.leaf_state_stack = Stack(maxlen=StateMachine.STACK_SIZE)
        self.stack = Stack()
        self._leaf_state = None

The values of those attributes.

Can you please let me know if I'm missing any extra state that is important for restoring the state machine, and further, what is the difference between state_stack, leaf_state_stack and stack?

pgularski commented 4 years ago

Ah yes, serialisation. I've been thinking about it for quite a while alright.

I think that apart from the state stacks you'd have to store the current state in each state machine in the states hierarchy and the _leaf_state. And I guess that'd be it.

Regarding the state_stack, leaf_state_stack and stack - after the docs (with some further explanation):

stack:
    Stack that can be used if the `Pushdown Automaton (PDA)` functionality
    is needed.

   (It's an auxiliary data structure that might or might not be used by the pysm user.
    It's not used internally for some kind of data flow.
    See the "test_rpn.py" file for an example of usage.)

state_stack

    Stack of previous local states in a state machine. With every
    transition, a previous state (instance of |State|) is pushed to the
    `state_stack`. Only StateMachine.STACK_SIZE (32
    by default) are stored and old values are removed from the stack.

   (Pysm can (but doesn't have to) be used as a HSM, a Hierarchically State Machine.
    In which case some states have to be instantiated
    as state machines (instances of the StateMachine).
    Each of these hierarchical state machines have their own transition history
    which is kept in the state_stack - every single
    StateMachine instance has its own state_stack.)

leaf_state_stack

    Stack of previous leaf states in a state machine. With every
    transition, a previous leaf state (instance of |State|) is pushed
    to the `leaf_state_stack`. Only
    StateMachine.STACK_SIZE (32 by default) are
    stored and old valuesm are removed from the stack.

   (Again, since pysm can be used as a HSM, the leaf_state_stack
    is a stack of previous states seen from the root state machine in the
    states hierarchy. It only makes sense to look at the leaf_state_stack
    from the root state machine. Go ahead and play with
    the test_pysm.py to understand it better)

Hope it helps.

nevercast commented 4 years ago

That gives me a good place to start! Thanks for the explaination of stacks