SlideShare une entreprise Scribd logo
1  sur  54
Finite State Machine

    Osama Hussain
Content
•   Concept
•   Mealy & Moore Machines
•   UML State Machines
•   UML State Diagrams
•   Examples
•   Implementation
Concept


A finite state machine (hereinafter called FSM) is
a mathematical model which abstracts the
representation of behavior exhibited by some
systems
Mathematical Model




  Check it by yourself 
System & Behavior

System
System                 Abstraction

            Behavior                 FSM
System
  …
Examples
• Traffic Light
   – Switching lights on and off
• Text Parsers
   – Detection of certain phrase or word
• Washing Machine
   – Washing, Rinsing, Spinning …
• Mario in Super Mario Game
   – Walk, Run, Jump …
Remarks
• Not all systems can be modeled in FSM
  – Real-time systems for example
  – Memory limitations due to huge number of states
• Not all systems can be easily modeled in FSM
  – Some behaviors are hard to describe in terms of
    states
• Proper FSM design can lead to easier system
  development
Within Game Dev.
• Some Game Developers think
  – FSM is only considered for AI field
  – FSM is only used in abstracting character behavior
  – FSM is only considered for Gameplay
    Programmers
Misconception
• Some Game Developers think
  – FSM is only considered for AI field
  – FSM is only used in abstracting character behavior
  – FSM is only considered for Gameplay
    Programmers

                          
Composition
• FSM consists of several states
• In Programming, state is a technical term for
  all the stored information, at a given point in
  time, which the program has access to.
  – Spinning in Washing Machine
  – C++ parser detects “int” keyword CPP file
  – Mario Jumping inside a Super Mario game
  – Red Light in Traffic Light
Composition
• Inputs into the machine are combined with
  the current state of the machine to determine
  the new state or the next state of the machine


    Current State                  Next State




            Input
State diagram




Donate a graphical representation of an FSM


           UML State diagrams
“Hello World” state diagram
                                  Initial Transition


                                              States

Indicator of Initial Transition

                                                       Final State


                  Transitions         Initial State
Composition
• Output … ?
Mealy Machine



Output determined by state and input
Traffic Light – Concept
• We got 3 lights that can be switched on and off individually,
  they are: Red, Yellow, and Green
• Traffic light contains a timer
• The traffic light starts by turning on the red light and turn
  off the rest
• After timer completes, switch on the green light and turn
  off the rest
• After timer completes, switch on the yellow light and turn
  off the rest
• After timer completes, switch on the red light and turn off
  the rest
• Restart again
Traffic Light – Mealy Machine
Quote Parser – Concept
• For a whole string of characters, print
  characters which are within double quotations
• Example
  – Input: “Hello ”people, what a nice “world!”
  – Output: Hello world!
Quote Parser – Mealy Machine
Moore Machine




Output determined by state and output
Traffic Light – Moore Machine
Traffic Light - Comparison
Mealy Machine     Moore Machine
Quote Parser – Moore Machine
UML State Machine
Significantly enhanced realization of the
mathematical concept of a finite automaton in
Computer Science applications as expressed in
the Unified Modeling Language notation
  – Object-based variant of Harel statechart (the
    concept of nested states)
  – Combines both Mealy and Moore machines with
    further addition of features
State Structure
Think of your states as objects where you might
need to
• Initialize it through Entry actions
• Update it through Do actions
• Finalize it through Exit actions
State Structure
Advantages
• Less states
• Default control mechanism
• Object oriented style
Hierarchically Nested States

 Arranging states in a structural way
Hierarchically Nested States
• The most important innovation of UML state
  machines over the traditional FSMs
• State nesting is not limited to one level only
• TOP state
  – Exist in every state machine
  – Contains all the other elements of the entire state
    machine
  – Optionally to depict it in the diagram
Traffic Light
• Based on previous traffic light description
• Add to that a switch
• You can turn the traffic light on and off at any
  time
• If the traffic light is switched on, then it
  operates normally
• Otherwise, all the lights will be turned off
• By default the traffic light is turned off
Traffic Light
Traffic Light
Gain
• Less transitions
• Less states
• Structural behavior
  – Zoom out: Hide complexity of the system
  – Zoom in: View the details of sub behavior in
    meaningful way
History




A facility to return back to the previous state
Space Counter
Space Counter
Washing Machine
Washing Machine




                                  Junction Point




A.K.A. Shallow History
Deep History



Recall the state of every nested substate of the
enclosing substate, down to any level of nesting
Washing Machine
Orthogonal Regions



A state can contain two or more independent
regions runs concurrently
Keyboard
Counter
• Imagine designing a FSM for 32-bit counter
  – Input: an external trigger
  – Output: once reaches 2^32 – 1, the system peeps!
• More than 4 billion different states!
Extended states




State machines supplemented with variables
Extended states
• Program variables are commonly dissociated
  from states
• the complete condition of the system (called
  the extended state) is the combination of a
  qualitative aspect (the state) and the
  quantitative aspects (the extended state
  variables)
• UML state machines belong to this category
Counter




               Guard Conditions




Choice Point
Guard Conditions
• Boolean expressions evaluated dynamically
  based on the value of extended state variables
  and event parameters
• Enable actions or transitions only when they
  evaluate to TRUE and disabling them when
  they evaluate to FALSE
• Shown in square brackets “[]”
Guard Conditions
• Good for the design, making it further simpler
• However, do not use it to eliminate states that
  you actually start used to eliminate IF ELSE
  statements!
• If you do … spaghetti code
State Diagrams' Limitations
• Any nontrivial state machine requires a large
  amount of textual information. For example,
  actions
• Depend heavily on the specific programming
  language
• Poorly represent the sequence of processing
• Require a lot of plumbing gear (junction points,
  choice points, etc.) to represent the flow of
  control graphically
• several complementary views of the same state
  machine
Commercial Tools
• Rational Rose
  – Well known tool
  – Bought by IBM in 2003
• Bouml
  – Cross-platform
  – Was free at certain point of time 
Open Source Tools
• StarUML
  – Windows only
  – Have most of the features required for State
    diagrams with only exception of orthogonal
    regions
  – No longer being in development 
  – Many initiatives to re-launch the project again
Questions?
Thank you

Contenu connexe

Tendances (12)

19 moore mealy
19 moore mealy19 moore mealy
19 moore mealy
 
State Machine Diagram
State Machine DiagramState Machine Diagram
State Machine Diagram
 
Finite State Machines with Output
Finite State Machines with OutputFinite State Machines with Output
Finite State Machines with Output
 
Slide 6 Activity Diagram
Slide 6 Activity DiagramSlide 6 Activity Diagram
Slide 6 Activity Diagram
 
ALGORITHMIC STATE MACHINES
ALGORITHMIC STATE MACHINESALGORITHMIC STATE MACHINES
ALGORITHMIC STATE MACHINES
 
18th & 19th Lecture
18th & 19th Lecture18th & 19th Lecture
18th & 19th Lecture
 
ALGORITHMIC STATE MACHINE
ALGORITHMIC STATE MACHINEALGORITHMIC STATE MACHINE
ALGORITHMIC STATE MACHINE
 
FSM and ASM
FSM and ASMFSM and ASM
FSM and ASM
 
Unit IV robotics-- Kinematics
Unit IV robotics-- KinematicsUnit IV robotics-- Kinematics
Unit IV robotics-- Kinematics
 
State Diagrams
State DiagramsState Diagrams
State Diagrams
 
Unit iv robot programming
Unit iv robot programmingUnit iv robot programming
Unit iv robot programming
 
Programming models for event controlled programs
Programming models for event controlled programsProgramming models for event controlled programs
Programming models for event controlled programs
 

En vedette

Finite state machines
Finite state machinesFinite state machines
Finite state machinesdennis gookyi
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machineslavishka_anuj
 
Mealy state machine
Mealy state machineMealy state machine
Mealy state machineArif Siyal
 
Software Verification, Validation and Testing
Software Verification, Validation and TestingSoftware Verification, Validation and Testing
Software Verification, Validation and TestingDr Sukhpal Singh Gill
 
Requirement specification
Requirement specificationRequirement specification
Requirement specificationAbdul Basit
 
design of FPGA based traffic light controller system
design of FPGA based traffic light controller systemdesign of FPGA based traffic light controller system
design of FPGA based traffic light controller systemVinny Chweety
 
Model based software testing
Model based software testingModel based software testing
Model based software testingSachin MK
 
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTINGA WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTINGJournal For Research
 
Hospital Management System
Hospital Management SystemHospital Management System
Hospital Management Systemidowume
 
Black box testing or behavioral testing
Black box testing or behavioral testingBlack box testing or behavioral testing
Black box testing or behavioral testingSlideshare
 
Requirement Analysis
Requirement AnalysisRequirement Analysis
Requirement Analysissslovepk
 
PART -1 TRAFFIC LIGHT CONTROL USING 8085
PART -1 TRAFFIC LIGHT CONTROL USING 8085PART -1 TRAFFIC LIGHT CONTROL USING 8085
PART -1 TRAFFIC LIGHT CONTROL USING 8085Subash Sambath Kumar
 
Requirement analysis and specification
Requirement analysis and specificationRequirement analysis and specification
Requirement analysis and specificationM.E. at GTU- PG School
 

En vedette (18)

Finite state machines
Finite state machinesFinite state machines
Finite state machines
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machines
 
Mealy machine
Mealy machineMealy machine
Mealy machine
 
Mealy state machine
Mealy state machineMealy state machine
Mealy state machine
 
Cldch8
Cldch8Cldch8
Cldch8
 
A&D - UML
A&D - UMLA&D - UML
A&D - UML
 
Software Verification, Validation and Testing
Software Verification, Validation and TestingSoftware Verification, Validation and Testing
Software Verification, Validation and Testing
 
Requirement specification
Requirement specificationRequirement specification
Requirement specification
 
design of FPGA based traffic light controller system
design of FPGA based traffic light controller systemdesign of FPGA based traffic light controller system
design of FPGA based traffic light controller system
 
Model based software testing
Model based software testingModel based software testing
Model based software testing
 
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTINGA WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
 
Blackbox
BlackboxBlackbox
Blackbox
 
Hospital Management System
Hospital Management SystemHospital Management System
Hospital Management System
 
Black box testing or behavioral testing
Black box testing or behavioral testingBlack box testing or behavioral testing
Black box testing or behavioral testing
 
Requirement Analysis
Requirement AnalysisRequirement Analysis
Requirement Analysis
 
PART -1 TRAFFIC LIGHT CONTROL USING 8085
PART -1 TRAFFIC LIGHT CONTROL USING 8085PART -1 TRAFFIC LIGHT CONTROL USING 8085
PART -1 TRAFFIC LIGHT CONTROL USING 8085
 
Introduction to White box testing
Introduction to White box testingIntroduction to White box testing
Introduction to White box testing
 
Requirement analysis and specification
Requirement analysis and specificationRequirement analysis and specification
Requirement analysis and specification
 

Similaire à Mgd finite statemachine

Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptxSKUP1
 
Simulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in CapellaSimulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in CapellaObeo
 
VIRA_Basics_of_Robot_Level_1.pptx
VIRA_Basics_of_Robot_Level_1.pptxVIRA_Basics_of_Robot_Level_1.pptx
VIRA_Basics_of_Robot_Level_1.pptxSarafrajBeg1
 
States, state graphs and transition testing
States, state graphs and transition testingStates, state graphs and transition testing
States, state graphs and transition testingABHISHEK KUMAR
 
Es_module2ppt.pptx
Es_module2ppt.pptxEs_module2ppt.pptx
Es_module2ppt.pptxRohanAM1
 
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 programmingLeahRachael
 
introduction to embedded system presentation
introduction to embedded system presentationintroduction to embedded system presentation
introduction to embedded system presentationAmr Rashed
 
PDA and Turing Machine (1).ppt
PDA and Turing Machine (1).pptPDA and Turing Machine (1).ppt
PDA and Turing Machine (1).pptAayushSingh233965
 
introduction to robotics and embedded systems
introduction to robotics and embedded systemsintroduction to robotics and embedded systems
introduction to robotics and embedded systemsDirectorCentreforEne
 
State Machines and PLC Programming.pptx
State Machines and PLC Programming.pptxState Machines and PLC Programming.pptx
State Machines and PLC Programming.pptxTobey Houston
 
Robot And it configuration
Robot And it configurationRobot And it configuration
Robot And it configurationDaniel raj
 
Seminar Presentation on FSM based vending machine
Seminar Presentation on FSM based vending machine Seminar Presentation on FSM based vending machine
Seminar Presentation on FSM based vending machine Pratik Patil
 

Similaire à Mgd finite statemachine (20)

Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptx
 
Simulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in CapellaSimulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in Capella
 
VIRA_Basics_of_Robot_Level_1.pptx
VIRA_Basics_of_Robot_Level_1.pptxVIRA_Basics_of_Robot_Level_1.pptx
VIRA_Basics_of_Robot_Level_1.pptx
 
States, state graphs and transition testing
States, state graphs and transition testingStates, state graphs and transition testing
States, state graphs and transition testing
 
Dss6 7
Dss6 7Dss6 7
Dss6 7
 
Es_module2ppt.pptx
Es_module2ppt.pptxEs_module2ppt.pptx
Es_module2ppt.pptx
 
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
 
introduction to embedded system presentation
introduction to embedded system presentationintroduction to embedded system presentation
introduction to embedded system presentation
 
Hypervisors
HypervisorsHypervisors
Hypervisors
 
12997233.ppt
12997233.ppt12997233.ppt
12997233.ppt
 
PDA and Turing Machine (1).ppt
PDA and Turing Machine (1).pptPDA and Turing Machine (1).ppt
PDA and Turing Machine (1).ppt
 
ROBOTIC - Introduction to Robotics
ROBOTIC - Introduction to RoboticsROBOTIC - Introduction to Robotics
ROBOTIC - Introduction to Robotics
 
Robotics1.ppt
Robotics1.pptRobotics1.ppt
Robotics1.ppt
 
introduction to robotics and embedded systems
introduction to robotics and embedded systemsintroduction to robotics and embedded systems
introduction to robotics and embedded systems
 
ie450pp8.ppt
ie450pp8.pptie450pp8.ppt
ie450pp8.ppt
 
State Machines and PLC Programming.pptx
State Machines and PLC Programming.pptxState Machines and PLC Programming.pptx
State Machines and PLC Programming.pptx
 
Ie450pp8
Ie450pp8Ie450pp8
Ie450pp8
 
Ie450pp8
Ie450pp8Ie450pp8
Ie450pp8
 
Robot And it configuration
Robot And it configurationRobot And it configuration
Robot And it configuration
 
Seminar Presentation on FSM based vending machine
Seminar Presentation on FSM based vending machine Seminar Presentation on FSM based vending machine
Seminar Presentation on FSM based vending machine
 

Plus de Moroccan game developers (7)

Global Game Jam Morocco
Global Game Jam MoroccoGlobal Game Jam Morocco
Global Game Jam Morocco
 
MGD Html5 pres fr
MGD Html5 pres frMGD Html5 pres fr
MGD Html5 pres fr
 
Mgd gamification
Mgd gamificationMgd gamification
Mgd gamification
 
Mgd crowfundinginvideogames
Mgd crowfundinginvideogamesMgd crowfundinginvideogames
Mgd crowfundinginvideogames
 
Mgd unity interface
Mgd unity interfaceMgd unity interface
Mgd unity interface
 
Mgd game devprocess
Mgd game devprocessMgd game devprocess
Mgd game devprocess
 
Mgd scripting
Mgd scriptingMgd scripting
Mgd scripting
 

Mgd finite statemachine

  • 1. Finite State Machine Osama Hussain
  • 2. Content • Concept • Mealy & Moore Machines • UML State Machines • UML State Diagrams • Examples • Implementation
  • 3. Concept A finite state machine (hereinafter called FSM) is a mathematical model which abstracts the representation of behavior exhibited by some systems
  • 4. Mathematical Model Check it by yourself 
  • 5. System & Behavior System System Abstraction Behavior FSM System …
  • 6. Examples • Traffic Light – Switching lights on and off • Text Parsers – Detection of certain phrase or word • Washing Machine – Washing, Rinsing, Spinning … • Mario in Super Mario Game – Walk, Run, Jump …
  • 7. Remarks • Not all systems can be modeled in FSM – Real-time systems for example – Memory limitations due to huge number of states • Not all systems can be easily modeled in FSM – Some behaviors are hard to describe in terms of states • Proper FSM design can lead to easier system development
  • 8. Within Game Dev. • Some Game Developers think – FSM is only considered for AI field – FSM is only used in abstracting character behavior – FSM is only considered for Gameplay Programmers
  • 9. Misconception • Some Game Developers think – FSM is only considered for AI field – FSM is only used in abstracting character behavior – FSM is only considered for Gameplay Programmers 
  • 10. Composition • FSM consists of several states • In Programming, state is a technical term for all the stored information, at a given point in time, which the program has access to. – Spinning in Washing Machine – C++ parser detects “int” keyword CPP file – Mario Jumping inside a Super Mario game – Red Light in Traffic Light
  • 11. Composition • Inputs into the machine are combined with the current state of the machine to determine the new state or the next state of the machine Current State Next State Input
  • 12. State diagram Donate a graphical representation of an FSM UML State diagrams
  • 13. “Hello World” state diagram Initial Transition States Indicator of Initial Transition Final State Transitions Initial State
  • 15. Mealy Machine Output determined by state and input
  • 16. Traffic Light – Concept • We got 3 lights that can be switched on and off individually, they are: Red, Yellow, and Green • Traffic light contains a timer • The traffic light starts by turning on the red light and turn off the rest • After timer completes, switch on the green light and turn off the rest • After timer completes, switch on the yellow light and turn off the rest • After timer completes, switch on the red light and turn off the rest • Restart again
  • 17. Traffic Light – Mealy Machine
  • 18. Quote Parser – Concept • For a whole string of characters, print characters which are within double quotations • Example – Input: “Hello ”people, what a nice “world!” – Output: Hello world!
  • 19. Quote Parser – Mealy Machine
  • 20. Moore Machine Output determined by state and output
  • 21. Traffic Light – Moore Machine
  • 22. Traffic Light - Comparison Mealy Machine Moore Machine
  • 23. Quote Parser – Moore Machine
  • 24. UML State Machine Significantly enhanced realization of the mathematical concept of a finite automaton in Computer Science applications as expressed in the Unified Modeling Language notation – Object-based variant of Harel statechart (the concept of nested states) – Combines both Mealy and Moore machines with further addition of features
  • 25. State Structure Think of your states as objects where you might need to • Initialize it through Entry actions • Update it through Do actions • Finalize it through Exit actions
  • 27. Advantages • Less states • Default control mechanism • Object oriented style
  • 28. Hierarchically Nested States Arranging states in a structural way
  • 29. Hierarchically Nested States • The most important innovation of UML state machines over the traditional FSMs • State nesting is not limited to one level only • TOP state – Exist in every state machine – Contains all the other elements of the entire state machine – Optionally to depict it in the diagram
  • 30. Traffic Light • Based on previous traffic light description • Add to that a switch • You can turn the traffic light on and off at any time • If the traffic light is switched on, then it operates normally • Otherwise, all the lights will be turned off • By default the traffic light is turned off
  • 33. Gain • Less transitions • Less states • Structural behavior – Zoom out: Hide complexity of the system – Zoom in: View the details of sub behavior in meaningful way
  • 34. History A facility to return back to the previous state
  • 38. Washing Machine Junction Point A.K.A. Shallow History
  • 39. Deep History Recall the state of every nested substate of the enclosing substate, down to any level of nesting
  • 41. Orthogonal Regions A state can contain two or more independent regions runs concurrently
  • 43. Counter • Imagine designing a FSM for 32-bit counter – Input: an external trigger – Output: once reaches 2^32 – 1, the system peeps! • More than 4 billion different states!
  • 44.
  • 45. Extended states State machines supplemented with variables
  • 46. Extended states • Program variables are commonly dissociated from states • the complete condition of the system (called the extended state) is the combination of a qualitative aspect (the state) and the quantitative aspects (the extended state variables) • UML state machines belong to this category
  • 47. Counter Guard Conditions Choice Point
  • 48. Guard Conditions • Boolean expressions evaluated dynamically based on the value of extended state variables and event parameters • Enable actions or transitions only when they evaluate to TRUE and disabling them when they evaluate to FALSE • Shown in square brackets “[]”
  • 49. Guard Conditions • Good for the design, making it further simpler • However, do not use it to eliminate states that you actually start used to eliminate IF ELSE statements! • If you do … spaghetti code
  • 50. State Diagrams' Limitations • Any nontrivial state machine requires a large amount of textual information. For example, actions • Depend heavily on the specific programming language • Poorly represent the sequence of processing • Require a lot of plumbing gear (junction points, choice points, etc.) to represent the flow of control graphically • several complementary views of the same state machine
  • 51. Commercial Tools • Rational Rose – Well known tool – Bought by IBM in 2003 • Bouml – Cross-platform – Was free at certain point of time 
  • 52. Open Source Tools • StarUML – Windows only – Have most of the features required for State diagrams with only exception of orthogonal regions – No longer being in development  – Many initiatives to re-launch the project again