1 / 28

State Machines

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

shyla
Télécharger la présentation

State Machines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Actions (outputs) Insert coin /Unlock gate Locked Unlocked States Pass thru /Lock gate Transitions Triggers (events)

  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 Insert coin/Unlock gate Locked Unlocked 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) • The final (end) state Insert coin/Unlock gate Locked Unlocked Close down Close down Pass thru/Lock gate

  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 • But the picture still isn’t overly complicated close Coin/unlock locked unlocked Pass/sound alarm Coin/”thank you” Pass/lock close

  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 • Addition of states/transitions/actions violation close reset Coin/unlock locked unlocked Pass/sound alarm Coin/”thank you” Pass/lock close

  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 Enters state C at sub-state B A B Enters state C at start state C

  20. Parallel • Two state machines may run in parallel, interacting with one another

  21. Parallel A C event_1/action_1 B action_1/action_2 D

  22. And so on… • They can contain “conditional connectors” • Can put “if statements” on the transitions

  23. Conditional connectors A event case x B case y C

  24. And so on… • Guarded transitions • Like a conditional transition but with just one “case”

  25. Guarded Transitions A event [case x] B

  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

More Related