gytis@ .lt
state machines
and
symfony workflow
gytis semėnas
gytis@helis.lt || gytis@semenas.lt
gytis@ .lt
state machines?
gytis@ .lt
symfony workflow?
gytis@ .lt
What to expect?
1. State machines
2. Petri nets
3. Symfony workflow
4. Code smells
gytis@ .lt
finite state machines
(FSM)
gytis@ .lt
old
gytis@ .lt
mathematical model of computation
gytis@ .lt
all among us
gytis@ .lt
(Q, Σ, δ, q0, F)
gytis@ .lt
finite state machine
● list of states
● initial state
● conditions for each transition
● can be in one state at a time
gytis@ .lt
moore
output on state
gytis@ .lt
moore oven
State: OFF
output: no heat
State Heating
output: heat
State Idling
output: no
heat
BAKE
pressed
OFF
pressed
too hot
too cold
OFF
pressed
gytis@ .lt
mealy
output on transition (based on state and input)
gytis@ .lt
mealy oven
Off
Baking
{heat false}
Bake
pressed
{heat on}
OFF
Pressed
{heat off}
too hot
{heat off}
too cold
{heat on}
gytis@ .lt
mealy vs moore
the end result vs the process
gytis@ .lt
why use it
● helps with the design process
● easy to read
● easy to test
● easy to maintain
● easy to onboard
● easy to track
gytis@ .lt
represents real world
gytis@ .lt
catch22
gytis@ .lt
gytis@ .lt
petri nets
gytis@ .lt
bipartite graph
place
transition
place
placeplace
gytis@ .lt
bipartite graph + tokens
place
transition
place
placeplace
gytis@ .lt
state is in the marking
gytis@ .lt
state is in the marking
place
transition
place
placeplace
place
transition
gytis@ .lt
state is in the marking
place
transition
place
placeplace
place
transition
gytis@ .lt
state machine?
transition
place
place
place
transition
gytis@ .lt
workflow net
● subset of petri net.
● a single input (source) place with no
previous transitions
● output place (sink) with no following
transitions.
gytis@ .lt
advanced petri nets
mathematical properties:
- boundedness
- safeness
- deadlock free.
- reachability.
parallelization, buffering, resource sharing
optimization, verification
coloured petri nets, weights...
gytis@ .lt
how are the
workflow net, petri net and state machine
related?
gytis@ .lt
how are they related?
petri net
workflow net
FSM
gytis@ .lt
available implementations
state pattern.
custom solution ;>
https://github.com/yohang/Finite
https://github.com/winzou/StateMachineBundle
https://github.com/florianv/petrinet
https://github.com/alterway/component-workflow
gytis@ .lt
symfony workflow
gytis@ .lt
gytis@ .lt
workflow
gytis@ .lt
workflow
gytis@ .lt
configuration
Example by @lyrixx
gytis@ .lt
configuration
Example by @lyrixx
gytis@ .lt
state machine validation
A transition in StateMachine can only have one output.
A transition in StateMachine can only have one input.
A transition from a place/state must have an unique name.
gytis@ .lt
usage
gytis@ .lt
usage in twig
gytis@ .lt
events workflow.leave
workflow.[exact_workflow].leave
workflow.[exact_workflow].leave.[exact_place]
workflow.transition (can be used as mealy)
workflow.enter
workflow.entered (can be used as moore)
workflow.announce
workflow.guard
gytis@ .lt
demo
gytis@ .lt
code smells
gytis@ .lt
looks familiar?
gytis@ .lt
looks familiar?
gytis@ .lt
looks familiar?
gytis@ .lt
what to look for?
● an attribute named state or
status e.g. $chair->getState()
gytis@ .lt
what to look for?
● an attribute named state or status e.g. $reservation->getStatus()
gytis@ .lt
what to look for?
● methods that return
boolean values
gytis@ .lt
what to look for?
● methods that return boolean values
gytis@ .lt
what to look for?
● records that are only valid for a certain period of time.
e.g. subscription expiry date
● or records with NULLABLE datetime fields paid_at, deleted_at, published_at, etc.
gytis@ .lt
a way of thinking
© Tobias Nyholm
gytis@ .lt
credits
Grégoire Pineau @lyrixx
Tobias Nyholm @tobiasNyholm
gytis@ .lt
kthxbye
gytis@helis.lt || gytis@semenas.lt

State Machines and Symfony Workflow