States¶
State, as the name says, holds the representation of a state in a StateMachine.
Initial state¶
A StateMachine should have one and only one initial
State.
The initial State is entered when the machine starts and the corresponding entering state Actions are called if defined.
Final state¶
You can explicitly set final states. Transitions from these states are not allowed and will raise exception.
>>> from statemachine import StateMachine, State
>>> class CampaignMachine(StateMachine):
... "A workflow machine"
... draft = State('Draft', initial=True, value=1)
... producing = State('Being produced', value=2)
... closed = State('Closed', final=True, value=3)
...
... add_job = draft.to.itself() | producing.to.itself() | closed.to(producing)
... produce = draft.to(producing)
... deliver = producing.to(closed)
Traceback (most recent call last):
...
InvalidDefinition: Cannot declare transitions from final state. Invalid state(s): ['closed']
You can retrieve all final states.
>>> class CampaignMachine(StateMachine):
... "A workflow machine"
... draft = State('Draft', initial=True, value=1)
... producing = State('Being produced', value=2)
... closed = State('Closed', final=True, value=3)
...
... add_job = draft.to.itself() | producing.to.itself()
... produce = draft.to(producing)
... deliver = producing.to(closed)
>>> machine = CampaignMachine()
>>> machine.final_states
[State('Closed', id='closed', value=3, initial=False, final=True)]