|
| 1 | +""" |
| 2 | +Microwave machine |
| 3 | +================= |
| 4 | +
|
| 5 | +Example that exercises the Compound and Parallel states. |
| 6 | +
|
| 7 | +Compound |
| 8 | +-------- |
| 9 | +
|
| 10 | +If there are more than one substates, one of them is usually designated as the initial state of |
| 11 | +that compound state. |
| 12 | +
|
| 13 | +When a compound state is active, its substates behave as though they were an active state machine: |
| 14 | + Exactly one child state must also be active. This means that: |
| 15 | +
|
| 16 | +When a compound state is entered, it must also enter exactly one of its substates, usually its |
| 17 | +initial state. |
| 18 | +When an event happens, the substates have priority when it comes to selecting which transition to |
| 19 | +follow. If a substate happens to handles an event, the event is consumed, it isn’t passed to the |
| 20 | +parent compound state. |
| 21 | +When a substate transitions to another substate, both “inside” the compound state, the compound |
| 22 | +state does not exit or enter; it remains active. |
| 23 | +When a compound state exits, its substate is simultaneously exited too. (Technically, the substate |
| 24 | +exits first, then its parent.) |
| 25 | +Compound states may be nested, or include parallel states. |
| 26 | +
|
| 27 | +The opposite of a compound state is an atomic state, which is a state with no substates. |
| 28 | +
|
| 29 | +A compound state is allowed to define transitions to its child states. Normally, when a transition |
| 30 | +leads from a state, it causes that state to be exited. For transitions from a compound state to |
| 31 | +one of its descendants, it is possible to define a transition that avoids exiting and entering |
| 32 | +the compound state itself, such transitions are called local transitions. |
| 33 | +
|
| 34 | +
|
| 35 | +""" |
| 36 | +from statemachine import State |
| 37 | +from statemachine import StateMachine |
| 38 | + |
| 39 | + |
| 40 | +class MicroWave(StateMachine): |
| 41 | + class oven(State.Builder, name="Oven", initial=True): |
| 42 | + class engine(State.Builder, name="Engine"): |
| 43 | + off = State("Off", initial=True) |
| 44 | + |
| 45 | + class on(State.Builder, name="On"): |
| 46 | + idle = State("Idle", initial=True) |
| 47 | + cooking = State("Cooking") |
| 48 | + |
| 49 | + idle.to(cooking, cond="closed.is_active") |
| 50 | + cooking.to(idle, cond="open.is_active") |
| 51 | + cooking.to.itself(internal=True, on="increment_timer") |
| 52 | + |
| 53 | + turn_off = on.to(off) |
| 54 | + turn_on = off.to(on) |
| 55 | + on.to(off, cond="cook_time_is_over") # eventless transition |
| 56 | + |
| 57 | + class door(State.Builder, name="Door"): |
| 58 | + closed = State("Closed", initial=True) |
| 59 | + open = State("Open") |
| 60 | + |
| 61 | + door_open = closed.to(open) |
| 62 | + door_close = open.to(closed) |
| 63 | + |
| 64 | + def __init__(self): |
| 65 | + self.cook_time = 5 |
| 66 | + self.door_closed = True |
| 67 | + self.timer = 0 |
| 68 | + super().__init__() |
0 commit comments