pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.68k stars 530 forks source link

Support parallel nested states or raise a proper error message #504

Closed thedrow closed 3 years ago

thedrow commented 3 years ago
>>> m = HierarchicalMachine(states=['a', {'name': 'b', 'children': [{'name': 'c', 'children': ['init', 'running']}, {'name': 'd', 'children': ['init', 'running']}], 'parallel': ['c_init', 'd_init']}])
>>> m.to('b')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/thedrow/.cache/pypoetry/virtualenvs/jumpstarter-q-gDbjwh-py3.9/lib/python3.9/site-packages/transitions/extensions/nesting.py", line 777, in to_state
    self._create_transition(event.source_name, state_name).execute(event)
  File "/home/thedrow/.cache/pypoetry/virtualenvs/jumpstarter-q-gDbjwh-py3.9/lib/python3.9/site-packages/transitions/core.py", line 272, in execute
    self._change_state(event_data)
  File "/home/thedrow/.cache/pypoetry/virtualenvs/jumpstarter-q-gDbjwh-py3.9/lib/python3.9/site-packages/transitions/extensions/nesting.py", line 262, in _change_state
    state_tree, exit_partials, enter_partials = self._resolve_transition(event_data)
  File "/home/thedrow/.cache/pypoetry/virtualenvs/jumpstarter-q-gDbjwh-py3.9/lib/python3.9/site-packages/transitions/extensions/nesting.py", line 250, in _resolve_transition
    new_states, enter_partials = self._enter_nested(root, dst_name_path, scope + root, event_data)
  File "/home/thedrow/.cache/pypoetry/virtualenvs/jumpstarter-q-gDbjwh-py3.9/lib/python3.9/site-packages/transitions/extensions/nesting.py", line 278, in _enter_nested
    new_states[state_name], new_enter = self._enter_nested([], dest, prefix_path + [state_name], event_data)
  File "/home/thedrow/.cache/pypoetry/virtualenvs/jumpstarter-q-gDbjwh-py3.9/lib/python3.9/site-packages/transitions/extensions/nesting.py", line 288, in _enter_nested
    initial_states = [event_data.machine.scoped.states[n] for n in initial_names]
  File "/home/thedrow/.cache/pypoetry/virtualenvs/jumpstarter-q-gDbjwh-py3.9/lib/python3.9/site-packages/transitions/extensions/nesting.py", line 288, in <listcomp>
    initial_states = [event_data.machine.scoped.states[n] for n in initial_names]
KeyError: 'c_init'
  1. If this should never be supported, a proper error message should be raised.
  2. If this should be supported, it shouldn't raise an exception and transition to b, c & then to c_init.
aleneum commented 3 years ago

This is currently not supported. It seems like the documentation deteriorated quite a bit. I once configured Scrutinizer for linting but stopped paying attention as reports became unreliable and I had no time to investigate. I guess it's time to improve code quality again. I create an issue for that.

aleneum commented 3 years ago

Just for the record: The documentation states that

Which substate to enter is defined by initial which should always point to a direct substate.

aleneum commented 3 years ago

closing this since this issue is covered in #505 and #507.