SlideShare a Scribd company logo
1 of 73
Download to read offline
Erlang / Elixir meetup
Introduction to gen_statem
Code |> Tests |> Demo
A gen_server with extra-power
to build Finite state machines
@antoine_reyt
gen_statem vs gen_server ?
gen_statem is an erlang behaviour (just as gen_server) with additional features:
- separation of state and data
- better handling of timers/timeout
- ability to postpone events
- ability to insert Internal events
It was introduced in OTP 19, and deprecates gen_fsm.
It’s a behaviour designed to build finite state machine.
Plan
● Definition of Finite State machine
● List of features
● Learning the basic syntax
● Overview of the features with code & tests
● Interactive demos via Liveview:
○ Light: basic example to introduce the syntax/concepts.
○ Payphone: play with timeouts and absolute time.
○ TrafficLight: coordinate many FSMs.
● Questions
A Finite-state Machine is an abstract machine
that can be in exactly one of a finite number of
states at any given time.
Wikipedia
A Finite-state Machine is an abstract machine
that can be in exactly one of a finite number of
states at any given time.
Wikipedia
A Finite-state Machine is an abstract machine
that can be in exactly one of a finite number of
states at any given time.
Let’s go graphical: use state-charts
states
states
transitions
states
transitions
Happy path
Exit path
Properties
● starts in its initial state
● can only be in a fixed set of states
● can only be in one state at a time
● changing from one state to another is called a transition
Summary
An FSM is composed of:
- States (including the initial state)
- Transitions
Benefits:
- Very simple to reason about using statechart
- Iterate with non technical people
Features
Features
● Timeouts
● state_enter
● Postpone
● Internal actions
Features - Timeouts
State Time-Out
There is one State Time-Out that is automatically cancelled by a state change.
Generic Time-Outs
There are any number of Generic Time-Outs differing by their Name. They have no
automatic cancelling.
Event Time-Out
There is one Event Time-Out that is automatically cancelled by any event. Note
that postponed and inserted events cancel this time-out just as external events.
Features - state_enter
When enabled, it’s called each time the state changes.
Depending on how your state machine is specified, this can be a very
useful feature: factorize code.
You can also repeat a state: when entering the same state, you can
explicitly say that you want to trigger the state_enter function again .
Features - postpone
If you decide to postpone an event, then it will not be tried again while in
the current state.
After a state change, the queue restarts with the previously postponed
events, and then continues with the new external event.
Features - internal_event
Internal events can only be generated by the state machine itself.
You can schedule many internal events for one incoming external event.
They are inserted in the queue as the next event to process before any
already queued events.
Features - summary
The FSM reacts to:
● External events
● Internal events
● Timeouts
You can postpone events.
Message priority:
1. Internal
2. Postponed
3. External
Basics
Base
Public API
:state_function - Callbacks
:handle_event_function - Callbacks
Tests
:sys.get_state | :sys.trace - Tests
Demos
state_timeout
Rules:
- Light is turned off by
default
- When the “on” button is
pressed, light is turned on
- When the “off” button is
pressed, light is turned off
- After a given time, the
light turns off itself
Rules:
- Light is turned off by
default
- When the “on” button is
pressed, light is turned on
- When the “off” button is
pressed, light is turned off
- After a given time, the
light turns off itself
Basic
transitions
state_timeout
:state_timeout
Tests
:timer.sleep()
generic_timeout
Sequence:
- pick up the phone
- add some credit
- dial a number
- speak
- hang up the phone
Rules:
- User can add credit when
both dialing and calling.
- User can hang up at any
time.
- When user runs out of
credit, the phone hangs
up itself.
- When the credit is low,
the user gets a voice
notification
Sequence:
- pick up the phone
- add some credit
- dial a number
- speak
- hang up the phone
Rules:
- User can add credit when
both dialing and calling.
- User can hang up at any
time.
- When user runs out of
credit, the phone hangs
up itself.
- When the credit is low,
the user gets a voice
notification
Basic
transitions
state_timeout
?
Sequence:
- pick up the phone
- add some credit
- dial a number
- speak
- hang up the phone
Rules:
- User can add credit when
both dialing and calling.
- User can hang up at any
time.
- When user runs out of
credit, the phone hangs
up itself.
- When the credit is low,
the user gets a voice
notification
Basic
transitions
state_timeout
?
You can only have one
state_timeout.
So we will use generic_timeout
Sequence:
- pick up the phone
- add some credit
- dial a number
- speak
- hang up the phone
Rules:
- User can add credit when
both dialing and calling.
- User can hang up at any
time.
- When user runs out of
credit, the phone hangs
up itself.
- When the credit is low,
the user gets a voice
notification
Basic
transitions
state_timeout
?
You can only have one
state_timeout.
So we will use generic_timeout
To avoid ticking every second,
we will use absolute time
Tests
assert_receive / refute_receive
state_enter
:handle_event_function - State enter
:state_function - State enter
Internal_event
&
postpone
Synchronize
TrafficLight.PeopleFsm TrafficLight.CarFsm
?
TrafficLight.PeopleFsm TrafficLight.CarFsm
TrafficLight.ManagerFsm
?
Goal:
Crossing the road.
Sequence:
- By default, the traffic lights
let the cars pass.
- Occasionally, people ask to
cross the road using a
button.
- Traffic light color changes to
let the people pass.
- After a given time, traffic
lights let the cars pass again
?
Goal:
Crossing the road.
Sequence:
- By default, the traffic lights
let the cars pass.
- Occasionally, people ask to
cross the road using a
button.
- Traffic light color changes to
let the people pass.
- After a given time, traffic
lights let the cars pass again
Goal:
Crossing the road.
Sequence:
- By default, the traffic lights
let the cars pass.
- Occasionally, people ask to
cross the road using a
button.
- Traffic light color changes to
let the people pass.
- After a given time, traffic
lights let the cars pass again
Goal:
People want to cross the road.
Sequence:
- By default, the traffic lights
let the car pass.
- Occasionally, people ask to
cross the road using a
button.
- Traffic light color change to
let the people pass.
- After a given time, traffic
light go back to let the car
pass.
Oops! we forgot this guy
ManagerFsm could call CarFsm and wait
for it synchronously.
We do not want to block the
ManagerFsm though.
Lets haveCarFsm notify ManagerFsm
when it’s done.
ManagerFsm call FsmCar.turn_red
CarFsm setup an internal_action
CarFsm postpone the previous internal_action (callback)
until it’s in the red state
ManagerFsm receive the callback from CarFsm
Links
● https://potatosalad.io/2017/10/13/time-out-elixir-state-machines-versu
s-servers
● https://andrealeopardi.com/posts/connection-managers-with-gen_s
tatem/
● https://hex.pm/packages/gen_state_machine
● https://ericent.in/elixir/erlang/gen_statem/gen_state_machine/2016/
07/27/stately-machines.html
https://github.com/antoinereyt/gen_statem_meetup
source code
Questions ?

More Related Content

What's hot

Artificial intelligence- Logic Agents
Artificial intelligence- Logic AgentsArtificial intelligence- Logic Agents
Artificial intelligence- Logic Agents
Nuruzzaman Milon
 

What's hot (19)

IHC in breast pathology
IHC in breast pathologyIHC in breast pathology
IHC in breast pathology
 
Molecular Subtypes of Breast Cancer
Molecular Subtypes of Breast CancerMolecular Subtypes of Breast Cancer
Molecular Subtypes of Breast Cancer
 
Endometrial cancer recommendations
Endometrial cancer recommendationsEndometrial cancer recommendations
Endometrial cancer recommendations
 
markov chain.ppt
markov chain.pptmarkov chain.ppt
markov chain.ppt
 
History, Politics and economic Review of Ethiopia
History, Politics and economic Review of EthiopiaHistory, Politics and economic Review of Ethiopia
History, Politics and economic Review of Ethiopia
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introduction
 
Introduction to Agents and Multi-agent Systems (lecture slides)
Introduction to Agents and Multi-agent Systems (lecture slides)Introduction to Agents and Multi-agent Systems (lecture slides)
Introduction to Agents and Multi-agent Systems (lecture slides)
 
Neonatal hematologic conditions
Neonatal hematologic conditionsNeonatal hematologic conditions
Neonatal hematologic conditions
 
FIGO 2014 Staging of Cancer Ovary
FIGO 2014 Staging of Cancer OvaryFIGO 2014 Staging of Cancer Ovary
FIGO 2014 Staging of Cancer Ovary
 
Artificial intelligence- Logic Agents
Artificial intelligence- Logic AgentsArtificial intelligence- Logic Agents
Artificial intelligence- Logic Agents
 
Management of the axilla after neoadjuvant chemotherapy
Management of the axilla after neoadjuvant chemotherapyManagement of the axilla after neoadjuvant chemotherapy
Management of the axilla after neoadjuvant chemotherapy
 
decision tree regression
decision tree regressiondecision tree regression
decision tree regression
 
Immunohistochemistry in Gynaecological Cancers
Immunohistochemistry in Gynaecological CancersImmunohistochemistry in Gynaecological Cancers
Immunohistochemistry in Gynaecological Cancers
 
Artificial bee colony algorithm
Artificial bee colony algorithmArtificial bee colony algorithm
Artificial bee colony algorithm
 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithms
 
Changepoint Detection with Bayesian Inference
Changepoint Detection with Bayesian InferenceChangepoint Detection with Bayesian Inference
Changepoint Detection with Bayesian Inference
 
postpartum hemorrhage
postpartum hemorrhagepostpartum hemorrhage
postpartum hemorrhage
 
AI - Local Search - Hill Climbing
AI - Local Search - Hill ClimbingAI - Local Search - Hill Climbing
AI - Local Search - Hill Climbing
 
ovarian tumor
ovarian tumorovarian tumor
ovarian tumor
 

Similar to Discover finite state machines with gen_statem in Erlang /Elixir

AISEC 12 april 2012 WP 7.1.1
AISEC 12 april 2012  WP 7.1.1AISEC 12 april 2012  WP 7.1.1
AISEC 12 april 2012 WP 7.1.1
Catalin Gheorghiu
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 

Similar to Discover finite state machines with gen_statem in Erlang /Elixir (20)

Unit 4- State Machine in mobile programming
Unit 4- State Machine in mobile programmingUnit 4- State Machine in mobile programming
Unit 4- State Machine in mobile programming
 
State Machines and PLC Programming.pptx
State Machines and PLC Programming.pptxState Machines and PLC Programming.pptx
State Machines and PLC Programming.pptx
 
Design Summit - Intro to State Machines - John Hardy
Design Summit - Intro to State Machines - John HardyDesign Summit - Intro to State Machines - John Hardy
Design Summit - Intro to State Machines - John Hardy
 
review-1
review-1review-1
review-1
 
Gordon morrison temporalengineering-delphi-v3
Gordon morrison temporalengineering-delphi-v3Gordon morrison temporalengineering-delphi-v3
Gordon morrison temporalengineering-delphi-v3
 
System Modeling and Hardware Software Co-Design
System Modeling and Hardware Software Co-DesignSystem Modeling and Hardware Software Co-Design
System Modeling and Hardware Software Co-Design
 
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
 
Exploration and Coverage for Autonomous Vehicles
Exploration and Coverage for Autonomous Vehicles Exploration and Coverage for Autonomous Vehicles
Exploration and Coverage for Autonomous Vehicles
 
Power Management
Power ManagementPower Management
Power Management
 
Angular change detection - Dakar Promise JS meetup
Angular change  detection - Dakar Promise JS meetupAngular change  detection - Dakar Promise JS meetup
Angular change detection - Dakar Promise JS meetup
 
Transactional Streaming: If you can compute it, you can probably stream it.
Transactional Streaming: If you can compute it, you can probably stream it.Transactional Streaming: If you can compute it, you can probably stream it.
Transactional Streaming: If you can compute it, you can probably stream it.
 
Automated Irrigation
Automated IrrigationAutomated Irrigation
Automated Irrigation
 
Rts
RtsRts
Rts
 
AISEC 12 april 2012 WP 7.1.1
AISEC 12 april 2012  WP 7.1.1AISEC 12 april 2012  WP 7.1.1
AISEC 12 april 2012 WP 7.1.1
 
Advanced features for LS-GSM-215 IOS/Android Touch LCD GSM alarm system
Advanced features for LS-GSM-215 IOS/Android Touch LCD GSM alarm systemAdvanced features for LS-GSM-215 IOS/Android Touch LCD GSM alarm system
Advanced features for LS-GSM-215 IOS/Android Touch LCD GSM alarm system
 
DSEF CON 27 - WILL C - phreaking elevators
DSEF CON 27 - WILL C - phreaking elevatorsDSEF CON 27 - WILL C - phreaking elevators
DSEF CON 27 - WILL C - phreaking elevators
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Mgd finite statemachine
Mgd finite statemachineMgd finite statemachine
Mgd finite statemachine
 
DTMF based Home Automation System
DTMF based Home Automation SystemDTMF based Home Automation System
DTMF based Home Automation System
 
Introduction to state machines in Embedded Software Design
Introduction to state machines in Embedded Software DesignIntroduction to state machines in Embedded Software Design
Introduction to state machines in Embedded Software Design
 

Recently uploaded

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

Discover finite state machines with gen_statem in Erlang /Elixir

  • 1. Erlang / Elixir meetup Introduction to gen_statem Code |> Tests |> Demo A gen_server with extra-power to build Finite state machines @antoine_reyt
  • 2. gen_statem vs gen_server ? gen_statem is an erlang behaviour (just as gen_server) with additional features: - separation of state and data - better handling of timers/timeout - ability to postpone events - ability to insert Internal events It was introduced in OTP 19, and deprecates gen_fsm. It’s a behaviour designed to build finite state machine.
  • 3. Plan ● Definition of Finite State machine ● List of features ● Learning the basic syntax ● Overview of the features with code & tests ● Interactive demos via Liveview: ○ Light: basic example to introduce the syntax/concepts. ○ Payphone: play with timeouts and absolute time. ○ TrafficLight: coordinate many FSMs. ● Questions
  • 4. A Finite-state Machine is an abstract machine that can be in exactly one of a finite number of states at any given time. Wikipedia
  • 5. A Finite-state Machine is an abstract machine that can be in exactly one of a finite number of states at any given time. Wikipedia
  • 6. A Finite-state Machine is an abstract machine that can be in exactly one of a finite number of states at any given time. Let’s go graphical: use state-charts
  • 7.
  • 11.
  • 14. Properties ● starts in its initial state ● can only be in a fixed set of states ● can only be in one state at a time ● changing from one state to another is called a transition
  • 15. Summary An FSM is composed of: - States (including the initial state) - Transitions Benefits: - Very simple to reason about using statechart - Iterate with non technical people
  • 17. Features ● Timeouts ● state_enter ● Postpone ● Internal actions
  • 18. Features - Timeouts State Time-Out There is one State Time-Out that is automatically cancelled by a state change. Generic Time-Outs There are any number of Generic Time-Outs differing by their Name. They have no automatic cancelling. Event Time-Out There is one Event Time-Out that is automatically cancelled by any event. Note that postponed and inserted events cancel this time-out just as external events.
  • 19. Features - state_enter When enabled, it’s called each time the state changes. Depending on how your state machine is specified, this can be a very useful feature: factorize code. You can also repeat a state: when entering the same state, you can explicitly say that you want to trigger the state_enter function again .
  • 20. Features - postpone If you decide to postpone an event, then it will not be tried again while in the current state. After a state change, the queue restarts with the previously postponed events, and then continues with the new external event.
  • 21. Features - internal_event Internal events can only be generated by the state machine itself. You can schedule many internal events for one incoming external event. They are inserted in the queue as the next event to process before any already queued events.
  • 22. Features - summary The FSM reacts to: ● External events ● Internal events ● Timeouts You can postpone events. Message priority: 1. Internal 2. Postponed 3. External
  • 24.
  • 25. Base
  • 29.
  • 30. Tests
  • 32. Demos
  • 33.
  • 35.
  • 36. Rules: - Light is turned off by default - When the “on” button is pressed, light is turned on - When the “off” button is pressed, light is turned off - After a given time, the light turns off itself
  • 37. Rules: - Light is turned off by default - When the “on” button is pressed, light is turned on - When the “off” button is pressed, light is turned off - After a given time, the light turns off itself Basic transitions state_timeout
  • 39.
  • 40. Tests
  • 43.
  • 44. Sequence: - pick up the phone - add some credit - dial a number - speak - hang up the phone Rules: - User can add credit when both dialing and calling. - User can hang up at any time. - When user runs out of credit, the phone hangs up itself. - When the credit is low, the user gets a voice notification
  • 45. Sequence: - pick up the phone - add some credit - dial a number - speak - hang up the phone Rules: - User can add credit when both dialing and calling. - User can hang up at any time. - When user runs out of credit, the phone hangs up itself. - When the credit is low, the user gets a voice notification Basic transitions state_timeout ?
  • 46. Sequence: - pick up the phone - add some credit - dial a number - speak - hang up the phone Rules: - User can add credit when both dialing and calling. - User can hang up at any time. - When user runs out of credit, the phone hangs up itself. - When the credit is low, the user gets a voice notification Basic transitions state_timeout ? You can only have one state_timeout. So we will use generic_timeout
  • 47. Sequence: - pick up the phone - add some credit - dial a number - speak - hang up the phone Rules: - User can add credit when both dialing and calling. - User can hang up at any time. - When user runs out of credit, the phone hangs up itself. - When the credit is low, the user gets a voice notification Basic transitions state_timeout ? You can only have one state_timeout. So we will use generic_timeout To avoid ticking every second, we will use absolute time
  • 48.
  • 49. Tests
  • 55.
  • 58. ?
  • 59. Goal: Crossing the road. Sequence: - By default, the traffic lights let the cars pass. - Occasionally, people ask to cross the road using a button. - Traffic light color changes to let the people pass. - After a given time, traffic lights let the cars pass again ?
  • 60. Goal: Crossing the road. Sequence: - By default, the traffic lights let the cars pass. - Occasionally, people ask to cross the road using a button. - Traffic light color changes to let the people pass. - After a given time, traffic lights let the cars pass again
  • 61.
  • 62. Goal: Crossing the road. Sequence: - By default, the traffic lights let the cars pass. - Occasionally, people ask to cross the road using a button. - Traffic light color changes to let the people pass. - After a given time, traffic lights let the cars pass again
  • 63. Goal: People want to cross the road. Sequence: - By default, the traffic lights let the car pass. - Occasionally, people ask to cross the road using a button. - Traffic light color change to let the people pass. - After a given time, traffic light go back to let the car pass. Oops! we forgot this guy
  • 64.
  • 65. ManagerFsm could call CarFsm and wait for it synchronously. We do not want to block the ManagerFsm though. Lets haveCarFsm notify ManagerFsm when it’s done.
  • 67. CarFsm setup an internal_action
  • 68. CarFsm postpone the previous internal_action (callback) until it’s in the red state
  • 69.
  • 70. ManagerFsm receive the callback from CarFsm
  • 71. Links ● https://potatosalad.io/2017/10/13/time-out-elixir-state-machines-versu s-servers ● https://andrealeopardi.com/posts/connection-managers-with-gen_s tatem/ ● https://hex.pm/packages/gen_state_machine ● https://ericent.in/elixir/erlang/gen_statem/gen_state_machine/2016/ 07/27/stately-machines.html