Download presentation
Presentation is loading. Please wait.
Published byCecil Hodge Modified over 9 years ago
1
State Machines
2
What are they? Sometimes called a “Finite State Machine” Depicted as a “state diagram” It’s a tool for specifying a system’s behavior Used in many disciplines –Language parsing –Software design –Systems design –Hardware design –Giving directions and other “every day life things”
3
Example Consider a subway turnstile system –The gate is either locked or unlocked –If locked, the patron inserts a coin to unlock it –If unlocked, the patron passes through This “wordy” definition is somewhat awkward although it does get the message across
4
A Better Description Through Symbols Subway turnstile as a state machine LockedUnlocked Insert coin Pass thru States Transitions Triggers (events) /Unlock gate /Lock gate Actions (outputs)
5
Example Subway turnstile as a state machine –There 2 “states” that the gate can be in Unlocked Locked These are represented as circles in the diagram –There are 2 events that can change the state of the system Insert coin Pass thru These are represented as state-to-state lines with text designators (before the “/” character) –There are 2 outputs/actions from the system Lock gate Unlock gate These are represented as state-to-state lines with text designators (after the “/” character)
6
Symbology (notation) The start state LockedUnlocked Insert coin/Unlock gate Pass thru/Lock gate
7
Symbology (notation) System is always in exactly one state –The solid circle marks the start state –When the system starts up this is the first state visited –The link out of the start state has no event associated with it –There are no links into the start state –This is called an unconditional transition and always happens
8
Symbology (notation) LockedUnlocked Insert coin/Unlock gate Pass thru/Lock gate Close down The final (end) state
9
Symbology (notation) The system may have zero or more final states –The solid circle with the ring around it –Anytime the system transitions into one of these states, this diagram is completed The system may shut down The system may transition to another state machine (up in the hierarchy – more later)
10
Better than words? In this case the “wordy” version wasn’t too bad But what if we need to add more, unforeseen stuff?
11
Example Let’s add some more options –If the gate is locked and someone passes through, set off an alarm –If the gate us unlocked and someone inserts money, thank them for the “donation” The “wordiness” just doubled
12
Example lockedunlocked Coin/unlock Pass/lock Coin/”thank you”Pass/sound alarm close But the picture still isn’t overly complicated
13
Additions Changing things “midstream” isn’t even that hard –Instead of sounding an alarm on a dead- beat perhaps we might want to do more
14
Example lockedunlocked Coin/unlock Pass/lock Coin/”thank you” Pass/sound alarm close violation reset Addition of states/transitions/actions
15
Additions We could go on and on, but we won’t
16
Implementation The software implementation is basically a switch statement in Java (or the equivalent if/else if/else) switch (state) { case locked:… case unlocked:… case violation:… }
17
Implementation The implementation in hardware is through sequential circuits with flip-flops (memory elements) and gates
18
And so on… There are a lot more features related to state machines –They can be “Hierarchical” One “super state” can contain many “sub states”
19
Hierarchical AB Enters state C at start state Enters state C at sub-state B C
20
Parallel Two state machines may run in parallel, interacting with one another
21
Parallel A B C D event_1/action_1 action_1/action_2
22
And so on… They can contain “conditional connectors” –Can put “if statements” on the transitions
23
Conditional connectors AB C eventcase x case y
24
And so on… Guarded transitions –Like a conditional transition but with just one “case”
25
Guarded Transitions AB event [case x]
26
And so on… States can have –Entry actions These are things that occur as soon as the state is entered –Transition actions These are things that occur on the transition to/from the state –Exit actions These are things that occur as a state is being exited
27
Summary They can be as simple or as complicated as necessary. But, if they get too complicated they are not serving their stated purpose and should be broken up into multiple state machines
28
Assignment Create a state-diagram describing the operation of the “4 Button Digital Watch” –Be neat! If you must draw it by hand, make sure it is clean, clear, and readable
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.