|
| 1 | +### Issue 308 |
| 2 | + |
| 3 | +A StateMachine that exercices the example given on issue |
| 4 | +#[308](https://github.com/fgmacedo/python-statemachine/issues/308). |
| 5 | + |
| 6 | +On this example, we share the transitions list between events. |
| 7 | + |
| 8 | +```py |
| 9 | +>>> from statemachine import StateMachine, State |
| 10 | + |
| 11 | +>>> class TestSM(StateMachine): |
| 12 | +... state1 = State('s1', initial=True) |
| 13 | +... state2 = State('s2') |
| 14 | +... state3 = State('s3') |
| 15 | +... state4 = State('s4', final=True) |
| 16 | +... |
| 17 | +... trans12 = state1.to(state2) |
| 18 | +... trans23 = state2.to(state3) |
| 19 | +... trans34 = state3.to(state4) |
| 20 | +... |
| 21 | +... # cycle = state1.to(state2) | state2.to(state3) | state3.to(state4) |
| 22 | +... cycle = trans12 | trans23 | trans34 |
| 23 | +... |
| 24 | +... def before_cycle(self): |
| 25 | +... print("before cycle") |
| 26 | +... |
| 27 | +... def on_cycle(self): |
| 28 | +... print("on cycle") |
| 29 | +... |
| 30 | +... def after_cycle(self): |
| 31 | +... print("after cycle") |
| 32 | +... |
| 33 | +... def on_enter_state1(self): |
| 34 | +... print('enter state1') |
| 35 | +... |
| 36 | +... def on_exit_state1(self): |
| 37 | +... print('exit state1') |
| 38 | +... |
| 39 | +... def on_enter_state2(self): |
| 40 | +... print('enter state2') |
| 41 | +... |
| 42 | +... def on_exit_state2(self): |
| 43 | +... print('exit state2') |
| 44 | +... |
| 45 | +... def on_enter_state3(self): |
| 46 | +... print('enter state3') |
| 47 | +... |
| 48 | +... def on_exit_state3(self): |
| 49 | +... print('exit state3') |
| 50 | +... |
| 51 | +... def on_enter_state4(self): |
| 52 | +... print('enter state4') |
| 53 | +... |
| 54 | +... def on_exit_state4(self): |
| 55 | +... print('exit state4') |
| 56 | +... |
| 57 | +... def before_trans12(self): |
| 58 | +... print('before trans12') |
| 59 | +... |
| 60 | +... def on_trans12(self): |
| 61 | +... print('on trans12') |
| 62 | +... |
| 63 | +... def after_trans12(self): |
| 64 | +... print('after trans12') |
| 65 | +... |
| 66 | +... def before_trans23(self): |
| 67 | +... print('before trans23') |
| 68 | +... |
| 69 | +... def on_trans23(self): |
| 70 | +... print('on trans23') |
| 71 | +... |
| 72 | +... def after_trans23(self): |
| 73 | +... print('after trans23') |
| 74 | +... |
| 75 | +... def before_trans34(self): |
| 76 | +... print('before trans34') |
| 77 | +... |
| 78 | +... def on_trans34(self): |
| 79 | +... print('on trans34') |
| 80 | +... |
| 81 | +... def after_trans34(self): |
| 82 | +... print('after trans34') |
| 83 | +... |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +Example given: |
| 88 | + |
| 89 | +```py |
| 90 | + |
| 91 | +>>> m = TestSM() |
| 92 | +enter state1 |
| 93 | + |
| 94 | +>>> m.is_state1, m.is_state2, m.is_state3, m.is_state4, m.current_state ; _ = m.cycle() |
| 95 | +(True, False, False, False, State('s1', id='state1', value='state1', initial=True, final=False)) |
| 96 | +before cycle |
| 97 | +before trans12 |
| 98 | +exit state1 |
| 99 | +on cycle |
| 100 | +on trans12 |
| 101 | +enter state2 |
| 102 | +after cycle |
| 103 | +after trans12 |
| 104 | + |
| 105 | +>>> m.is_state1, m.is_state2, m.is_state3, m.is_state4, m.current_state ; _ = m.cycle() |
| 106 | +(False, True, False, False, State('s2', id='state2', value='state2', initial=False, final=False)) |
| 107 | +before cycle |
| 108 | +before trans23 |
| 109 | +exit state2 |
| 110 | +on cycle |
| 111 | +on trans23 |
| 112 | +enter state3 |
| 113 | +after cycle |
| 114 | +after trans23 |
| 115 | + |
| 116 | +>>> m.is_state1, m.is_state2, m.is_state3, m.is_state4, m.current_state ; _ = m.cycle() |
| 117 | +(False, False, True, False, State('s3', id='state3', value='state3', initial=False, final=False)) |
| 118 | +before cycle |
| 119 | +before trans34 |
| 120 | +exit state3 |
| 121 | +on cycle |
| 122 | +on trans34 |
| 123 | +enter state4 |
| 124 | +after cycle |
| 125 | +after trans34 |
| 126 | + |
| 127 | +>>> m.is_state1, m.is_state2, m.is_state3, m.is_state4, m.current_state |
| 128 | +(False, False, False, True, State('s4', id='state4', value='state4', initial=False, final=True)) |
| 129 | + |
| 130 | +``` |
0 commit comments