python-statemachine
python-statemachine copied to clipboard
Docs: Hierarchical/nested state machine?
- Python State Machine version: 0.7.1
Description
Is it possible to implement hierarchical/nested state machines like this explicit implementation?
What I Did
Nothing tried yet.
Hello @fkromer,
I haven't implemented it, but I had the same question and I think it is possible. There it goes my theorical approach:
1.- Given that you have both classes "State" and "StateMachine", you can implement a new one inheriting from both, which would represent child state machines, or nests.
2.- You implement your parent state machine class just inheriting from StateMachine, and there you declare your states (those from above) and transitions. Also, you can declare your "common" states.
3.- The library allows you to trigger an event when entering into a new state with on_enter_state, a function located in StateMachineClass, which take destination (a state object) as argument.
You would check if this particular destination state also inherits from StateMachine (isinstance(destination, StateMachine)). If true, you handle the flow through it.
from statemachine import StateMachine, State
class NestedStateMachine(StateMachine, State):
[...]
class MyStateMachine(StateMachine):
# states
nested_state = NestedStateMachine(...)
usual_state = State(...)
# transitions
[...]
def on_enter_state(self, destination):
if isinstance(destination, StateMachine):
# handle
Hi @fkromer , thanks for pointing out this requirement. At this point, I think that hierarchical/nested state machines are not supported. I've never tried. So any complete example should be nice and we can publish on the docs!
Let me share what I'm thinking about.
I'm reading about statecharts since SCXML (Statechart XML), is a W3C standard and it defines a lot of the semantics and specifies how to deal with certain edge cases. A statechart is essentially a state machine that allows any state to include more machines, in a hierarchical fashion. This is exactly that you're asking for.
With that in mind, my goal to this library may turn to be as much as possible compatible with statecharts but using a different syntax. A pythonic interface to statecharts.
Hi @claverru, thanks for your contribution. Feel free to elaborate a complete example and I'll be happy to publish on the docs. We've a lot of implemented features that are not covered on docs.
From my perspective, a state with nested state machine (SM) Is possible. The logic with an example follows. Imagine a state machine composed of two states which one has an embedded SM. If a state transition from an upper level SM is triggered, the lower level SM resets. As mentioned by @claverru, the state's state machine state is also accessible by sm.nsm.State() ,
If I am not mistaken, take a look on the end of README of repository homepage. It shows a Mixin explanation which allows nested state machine as you wish.
@brunolnetto you definitely should expand on your comment, I took a careful look at Mixin and does not see how it squares with nested state machine, maybe I am missing something but your comment could use some example or more details.
@Drachenfels Mr. Dragonfield, my comment relates to an abstraction of a to-be state machine. I take the liberty to compose state machine visions, regardless of the status quo of the current implementation. I should not bring opinion annotations about my view outside the issue scope, but I have such an annoying profile feature.
@fgmacedo I suppose you grasp my state-machine-ception idea as a potential feature. May your feasibility account make aware current and future users?
Closing as it's not a documentation issue. We need to add support for composed/parallel states to achieve this.