280 likes | 457 Views
State Machines. 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
E N D
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”
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
A Better Description Through Symbols • Subway turnstile as a state machine Actions (outputs) Insert coin /Unlock gate Locked Unlocked States Pass thru /Lock gate Transitions Triggers (events)
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)
Symbology (notation) • The start state Insert coin/Unlock gate Locked Unlocked Pass thru/Lock gate
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
Symbology (notation) • The final (end) state Insert coin/Unlock gate Locked Unlocked Close down Close down Pass thru/Lock gate
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)
Better than words? • In this case the “wordy” version wasn’t too bad • But what if we need to add more, unforeseen stuff?
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
Example • But the picture still isn’t overly complicated close Coin/unlock locked unlocked Pass/sound alarm Coin/”thank you” Pass/lock close
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
Example • Addition of states/transitions/actions violation close reset Coin/unlock locked unlocked Pass/sound alarm Coin/”thank you” Pass/lock close
Additions • We could go on and on, but we won’t
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:… }
Implementation • The implementation in hardware is through sequential circuits with flip-flops (memory elements) and gates
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”
Hierarchical Enters state C at sub-state B A B Enters state C at start state C
Parallel • Two state machines may run in parallel, interacting with one another
Parallel A C event_1/action_1 B action_1/action_2 D
And so on… • They can contain “conditional connectors” • Can put “if statements” on the transitions
Conditional connectors A event case x B case y C
And so on… • Guarded transitions • Like a conditional transition but with just one “case”
Guarded Transitions A event [case x] B
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
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
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