Skip to content

Commit 79546a5

Browse files
committed
chore: Example of issue #434
1 parent 997211a commit 79546a5

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

tests/testcases/issue434.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
### Issue 434
2+
3+
A StateMachine that exercises the example given on issue
4+
#[434](https://github.com/fgmacedo/python-statemachine/issues/434).
5+
6+
7+
```py
8+
>>> from time import sleep
9+
>>> from statemachine import StateMachine, State
10+
11+
>>> class Model:
12+
... def __init__(self, data: dict):
13+
... self.data = data
14+
15+
>>> class DataCheckerMachine(StateMachine):
16+
... check_data = State(initial=True)
17+
... data_good = State(final=True)
18+
... data_bad = State(final=True)
19+
...
20+
... MAX_CYCLE_COUNT = 10
21+
... cycle_count = 0
22+
...
23+
... cycle = (
24+
... check_data.to(data_good, cond="data_looks_good")
25+
... | check_data.to(data_bad, cond="max_cycle_reached")
26+
... | check_data.to.itself(internal=True)
27+
... )
28+
...
29+
... def data_looks_good(self):
30+
... return self.model.data.get("value") > 10.0
31+
...
32+
... def max_cycle_reached(self):
33+
... return self.cycle_count > self.MAX_CYCLE_COUNT
34+
...
35+
... def after_cycle(self, event: str, source: State, target: State):
36+
... print(f'Running {event} {self.cycle_count} from {source!s} to {target!s}.')
37+
... self.cycle_count += 1
38+
...
39+
40+
```
41+
42+
Run until we reach the max cycle without success:
43+
44+
```py
45+
>>> data = {"value": 1}
46+
>>> sm1 = DataCheckerMachine(Model(data))
47+
>>> cycle_rate = 0.1
48+
>>> while not sm1.current_state.final:
49+
... sm1.cycle()
50+
... sleep(cycle_rate)
51+
Running cycle 0 from Check data to Check data.
52+
Running cycle 1 from Check data to Check data.
53+
Running cycle 2 from Check data to Check data.
54+
Running cycle 3 from Check data to Check data.
55+
Running cycle 4 from Check data to Check data.
56+
Running cycle 5 from Check data to Check data.
57+
Running cycle 6 from Check data to Check data.
58+
Running cycle 7 from Check data to Check data.
59+
Running cycle 8 from Check data to Check data.
60+
Running cycle 9 from Check data to Check data.
61+
Running cycle 10 from Check data to Check data.
62+
Running cycle 11 from Check data to Data bad.
63+
64+
```
65+
66+
67+
Run simulating that the data turns good on the 5th iteration:
68+
69+
```py
70+
>>> data = {"value": 1}
71+
>>> sm2 = DataCheckerMachine(Model(data))
72+
>>> cycle_rate = 0.1
73+
>>> while not sm2.current_state.final:
74+
... sm2.cycle()
75+
... if sm2.cycle_count == 5:
76+
... print("Now data looks good!")
77+
... data["value"] = 20
78+
... sleep(cycle_rate)
79+
Running cycle 0 from Check data to Check data.
80+
Running cycle 1 from Check data to Check data.
81+
Running cycle 2 from Check data to Check data.
82+
Running cycle 3 from Check data to Check data.
83+
Running cycle 4 from Check data to Check data.
84+
Now data looks good!
85+
Running cycle 5 from Check data to Data good.
86+
87+
```

0 commit comments

Comments
 (0)