alysivji / alysivji/finite-state-machine
Different source and target for same event
- 主要言語
- Python
- スター
- 113
- フォーク
- 11
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
Hello!
Let's assume the following state machine:
[](https://mermaid.live/edit#eyJjb2RlIjoic3RhdGVEaWFncmFtLXYyXG4gICAgWypdIC0tPiBBXG4gICAgQSAtLT4gQjogZXZlbnRfMlxuICAgIEEgLS0-IEM6IGV2ZW50XzNcbiAgICBCIC0tPiBBOiBldmVudF8xXG4gICAgQiAtLT4gQzogZXZlbnRfNFxuICAgIEMgLS0-IEQ6IGV2ZW50XzRcbiAgICBEIC0tPiBBOiBldmVudF8xXG4iLCJtZXJtYWlkIjoie1xuICBcInRoZW1lXCI6IFwiZGVmYXVsdFwiXG59IiwidXBkYXRlRWRpdG9yIjpmYWxzZSwiYXV0b1N5bmMiOnRydWUsInVwZGF0ZURpYWdyYW0iOmZhbHNlfQ)
```mermaid
stateDiagram-v2
[*] --> A
A --> B: event_2
A --> C: event_3
B --> A: event_1
B --> C: event_4
C --> D: event_4
D --> A: event_1
```
So ```event_1``` triggers the transition from:
- B --> A
- D --> A
But ```event_4``` triggers the transition from (different source and target in each case):
- B --> C
- C --> D
If I understood correctly this would result in the following code:
```python
from finite_state_machine import StateMachine, transition
class ExampleStateMachine(StateMachine):
initial_state = "A"
def __init__(self):
self.state = self.initial_state
super().__init__()
@transition(source=["B", "D"], target="A")
def event_1(self):
print("Transitioning to A by event_1")
@transition(source="A", target="B")
def event_2(self):
print("Transitioning to B by event_2")
@transition(source="A", target="C")
def event_3(self):
print("Transitioning to C by event_3")
@transition(source="B", target="C")
@transition(source="C", target="D")
def event_4(self):
pass
```
The problem is with event_4 since it would require two different transitions
```python
@transition(source="C", target="D")
@transition(source="B", target="C")
def event_4(self):
pass
```
But when I try to run this machine, I get the following error message:
```python
fsm = ExampleStateMachine()
fsm.event_2()
fsm.event_1()
fsm.event_2()
fsm.event_4() # <- InvalidStartState exception raised here
fsm.event_4()
fsm.event_1()
fsm.event_3()
fsm.event_4()
fsm.event_1()
```
```python
finite_state_machine.exceptions.InvalidStartState: Current state is B. event_4 allows transitions from ['C'].
```
Now I am not sure where my thinking error is. Is my code wrong or are multiple transitions really not supported?
Thanks a lot!
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
評価
この issue はまだ評価されていません。