Las maquinas de estado y los flujos de trabajo no son nada nuevo, pero sí quizás algo de lo que has oído hablar pero no te has lanzado a implementar.
Cada vez más, esta empezando a formar una parte muy importante de la evolución tecnológica de muchas empresas.
Nos pueden servir para solucionar algunos comportamientos en nuestro pequeño proyecto o ser la solución idónea cuando necesitamos aclarar muchos de los procesos de negocio que podemos encontrar en un proyecto muy grande y quizás muy monolítico.
Tecnológicamente hablando, es una muy buena herramienta para encapsular una lógica de negocio que suele ser compleja y tediosa.
Con esta charla me gustaría compartir mi experiencia trabajando con dichos procesos, ayudaros a entender dichos conceptos y saber diferenciar cuando usar uno u el otro, algo muy importante.
Por supuesto, veremos y comentaremos implementaciones en PHP y algunas de las librerías que nos pueden ayudar a agilizar aún más la implementación de estos modelos en nuestros proyectos.
URL del evento: http://www.meetup.com/es-ES/PHPMad/events/234585802/
2. STATE MACHINES AND WORKFLOW
NETS ON YOUR PHP PROJECTS
DANIEL ABAD CAMBRONERO
2
3. PRESENTATION
▸ Daniel Abad Cambronero
▸ https://github.com/ruudy-es
▸ @Ruudy_es
▸ Senior Software Developer
WHO I IM
3
4. INTRODUCTION
WHY?
▸ What are the three most important ideas in programming?
1. Abstraction
2. Abstraction
3. Abstraction
4
5. STATE MACHINES
DEFINITION
▸ Is a computational model used to design both computer
programs and sequential logic circuits
▸ It is conceived as an abstract machine that can be in one of
a finite number of states
▸ No ambiguity is allowed, the machine does not have
human intuition and must follow instructions
▸ It can change from one state to another when triggered by
an event or condition
5
7. STATE MACHINES
THEY ARE AMONG US
This is not brand new, have decades of research behind them,
analyzing and simplifying them.
▸ Electronic design automation
▸ Communication protocol design
▸ Language parsing
▸ To describe neurological systems (biology)
▸ To describe simple parts of the grammars of natural language
(linguistics)
7
9. STATE MACHINES
ALSO…
▸ It really helps the design process
▸ Just with a look at the drawing, most of the people will
understand what is happening there
▸ It’ll make debugging easier and help us find where the
points of failure are
9
11. WORKFLOW NETS
DEFINITION
▸ Try to represent a complex human task with an steps
process
▸ Is a subclass of Petri nets intending to model the workflow
of process activities.
Wait…..
11
12. WOKFLOW NETS
PETRI NET?
▸ Place / transition net, one of the several mathematical
modelling languages for the description of distributed
systems.
▸ Arcs run from a place to a transition or vice versa, never
between places or between transitions
▸ The model never gets stuck in a transition
12
14. WORKFLOW NETS
GO BACK TO THE DEFINITION
▸ Transitions are assigned to tasks or activities
▸ Places are assigned to the pre/post conditions
▸ Have the addition of a single input (source) place with no
previous transitions, and output place (sink) with no
following transitions.
14
16. WORKFLOW NETS
BENEFITS
▸ Can represent an orchestrated and repeatable pattern of business
activity
▸ It can be depicted as a sequence of operations
▸ Strongly related with how people actually work and how
organizations handle information
▸ A newcomer can easily understand a program just looking at the
Workflow Definition Graph.
▸ Even more easy to debug and notice points of failure than State
Machine
16
17. STATE MACHINES VS WORKFLOW NETS
SUMMARISING
STATE MACHINE
‣ Must be only on one state
‣ Has only an exclusive
gateway
‣ Can be cyclic (loop)
‣ Could have transitions
with same name
WORKFLOW NET
‣ Marking could be composed
by multiple places
‣ Has a parallel gateway,
inclusive gateway and
exclusive gateway
‣ Are usually acyclic (start to
finish)
‣ Transitions name must be
unique
17
19. MAKE THIS USABLE
RECOGNIZING IT
▸ Our model have a state or status field
▸ We are using boolean fields (published, paid)
▸ We are using nullable timestamps (published_at, paid_at)
▸ Having records that are only valid for a given period in
time (subscriptions with start and end date)
▸ Approved / Rejected process depending of multiple
checks
19
20. MAKE THIS USABLE
HAVE YOU EVER SEEN THIS?
20
▸ You ever thought or see something like this?
22. MAKE THIS USABLE
HOW TO (PHP)
▸ Encapsulate the graph logic
▸ Design a good architecture around:
▸ Models
▸ Transition conditions
▸ Movements Logic
▸ Events / Automatization
▸ Tests / Documentation
22
23. ENCAPSULATE THE GRAPH LOGIC
STATE MACHINES LIBRARIES
▸ https://github.com/winzou/state-machine
▸ https://github.com/winzou/StateMachineBundle
▸ https://github.com/yohang/Finite
▸ https://github.com/Metabor/Statemachine
▸ https://github.com/sebastianbergmann/state
23
30. EXAMPLE - DESIGN A GOOD ARCHITECTURE AROUND
MODELS
‣ Not type tables (for SM and WF)
▸ Not enum columns
▸ Easy as VARCHAR or LONGTEXT
▸ Do not try to replicate the SM / WF logic in your model
▸ SM & WF will use attributes for your model
30
35. DESIGN A GOOD ARCHITECTURE AROUND
EVENTS / AUTOMATIZATION
▸ Crons
▸ Model changes
▸ Transitions
▸ Places / States reach / leave
▸ Logs
35
36. DESIGN A GOOD ARCHITECTURE AROUND
TEST & DOCUMENTATION
Is almost impossible to cover all the cases from start.
▸ What happens if an enemy die?
▸ What happens if meanwhile im evading, enemy move to
another zone?
36
37. FINAL TIPS
MIX IT!
37
▸ A state machine could be part of a transition in a Workflow
net
▸ The final place of a Workflow could be one of the
requirements in a State machine transaction
▸ The enemy state could be another state machine with
different reactions (kind of AI), or a second instance of the
current one, interacting each other.