Mastering the Unified Modeling Language -- State Transition Diagrams
Total Page:16
File Type:pdf, Size:1020Kb
Mastering the Unified Modeling Language -- State Transition Diagrams -- © Josef Schiefer, IBM Watson Outline nested state diagrams States & Transitions Combinatorial Explosion concurrent state machines Solution: Statecharts Statecharts messages ... UML State Diagrams 12/7/2000 - v2 states Stubbed Transitions transitions Synchronization events Concurrency vs. Orthogonality Diagram Elements UML State Machines actions History Indicator guards Sending Messages conditions Compound Transitions UML State Machines 1 Introduction to State Diagrams Interaction diagrams are very good for communication – between clients and designers – between designers and other designers – between designers and implementors But they are not part of the constructive core – Constructive core means that part from which an actual implementation could be generated The constructive core of the UML is – class diagrams (for the static part) – state diagrams (for the dynamic part) State diagrams can bescribe very complex dynamic behavior UML State Machines Elements of State Diagrams The basic elements of state diagrams are – states – the state in which the object finds itself at any moment – transitions – take the object from one state to another – events – cause the transition from one state to another – actions – take place as a result of a transition In the UML, many other extensions have been added, such as: – generalization/Inheritance among events (i.e. a mouse click and keyboard input as subclasses of user interface events) – hierarchical diagrams for managing complexity (from StateCharts) – guards – conditions on transitions State Diagrams should only be used for objects with significant dynamic behavior UML State Machines 2 Types of Events ChangeEvent (i.e.: when(x>y)) – A change event models an event that occurs when an explicit boolean expression becomes true as a result of a change in value of one or more attributes or associations. – A change event is raised implicitly and is not the result of some explicit change event action. CallEvent (i.e.: conflictsWith(Appointment)) – A call event represents the reception of a request to synchronously invoke a specific operation. – Note that a call event instance is distinct from the call action that caused it. SignalEvent (i.e.: leftButtonDown) – A signal event represents the reception of a particular (asynchronous) signal. TimeEvent (i.e.: after(1 second)) – A TimeEvent models the expiration of a specific deadline. Note that the time of occurrence of a time event instance (i.e., the expiration of the deadline) is the same as the time of its reception. UML State Machines States and Transitions transition state up floor On First Moving arrive at Floor Up first floor Moving to An Elevator up floor First Floor arrive at floor arrive at floor Idle Moving Down down floor time-out UML State Machines 3 Basic UML State Diagram Syntax A state is drawn with a round box, with three Name sometimes compartments for left out when state variables – name empty – state variables (if any) actions – actions to be performed A transition is drawn with an arrow, possibly labeled with AnEvent [guard] / SomeAction – event causing the transaction – guard condition – Action to perform UML State Machines State Diagram for Seminar Registration event action, taken during transition Setup AddParticipant / Set count = 0 Available do/initialize seminar do/initialize seminar cancel [ count = 20 ] seminar cancel seminar guard Full Canceled cancel seminar do/finalize seminar do/refund payments aktivity, carried out while in that state multiple exits UML State Machines 4 Predefined Action Labels “entry/” – identifies an action, specified by the corresponding action expression, which is performed upon entry to the state (entry action) “exit/” – identifies an action, specified by the corresponding action expression, that is performed upon exit from the state (exit action) “do/” – identifies an ongoing activity (“do activity”) that is performed as long as the modeled element is in the state or until the computation specified by the action expression is completed (the latter may result in a completion event being generated). “include/” – is used to identify a submachine invocation. The action expression contains the name of the submachine that is to be invoked. UML State Machines Simple Diagram for Heart Monitor Applications Problem Switch on Startup detected Switch off Off Startup Alarm Complete Switch off Problem detected Operational UML State Machines 5 Full Diagram for Heart Monitor Applications The number of states and transitions has StartupBattery grown quickly StartupMains Off AlarmBattery etc. AlarmMains Why does the OperationalBattery diagram grow so fast? OperationalMains UML State Machines Independent State Components We can explore the Widget phenomenon of rapid state Color (red, blue, green) diagram growth by Mode (Normal, Startup, Demo) examining a typical class Status (Ok, Error) with three attributes SetColor() Each combination of values SetMode() of the attributes of this class SetStatus() constitute a different state for the class Note that the attributes are effectively independent of each other NumberNumber of of States States = = 3 3 x x 3 3 x x 2 2 = = 18!! 18!! – any combination is valid UML State Machines 6 Combinatorial Explosion The number of states and the number of transitions grows very quickly as you model complex state machines ( combinatorial explosion) This is the Achilles Heel of finite state machines – It has prevented the widespread use of state machines in the past UML State Machines Some State Diagram History Traditional state diagrams are called State Diagrams -Mealy Mealy-Moore diagrams after their - Moore “inventors” -? They have small differences – Mealy FSMs allow actions only when a transition is taken – Moore FSMs allow actions only upon state entry But they did not solve the problem of combinatorial explosion – Something new was needed if state diagrams were to survive UML State Machines 7 Statecharts 1983: A new visual formalism extending the language of state diagrams – Statecharts Developed by David Harel – Professor at Weizmann Institute, Israel – Complexity theory, logic, reactive systems engineering, … – Popular educator, author, wrote Algorithmics, the Spirit of Computing – Founded company I-Logix (www.ilogix.com) to create tool support for Statecharts (Statemate, Rhapsody) UML State Machines Features of Statecharts Harel statecharts directly address the problems of traditional state diagrams S1 S Two major features are introduced for 2 controlling complexity and combinatorial Nested State Diagrams explosion – nested state diagrams – concurrent state diagrams Many other features are also added – propagated transitions – broadcast messages – actions on state entry, exit Concurrent State Diagrams – … UML State Machines 8 UML Statechart Diagrams Harel statecharts are the basis for the UML state diagram notation – formally “UML statechart diagrams” in the UML documentation – informally usually just referred to as “state diagrams” Original statechart notation was not object-oriented – based on traditional function-oriented, structured analysis paradigm UML added features to support object orientation – Support of inheritance (of events, transitions, etc.) – Delegation of messages within composite objects – Etc. UML State Machines Nested State Diagrams Hierarchical organization is a classic way to control complexity – of programs – of documentation superstate – of objects – … Even diagrams can be hierarchical – Traditional data flow diagrams are hierarchical Why not state diagrams? – superstates – substates substates UML State Machines 9 Superstates and Substates name of superstate substates terminal state Dialing LiftReceiver LiftReceiver Digit(n) entry/ start dial tone TheNumber: int exit/ stop dial tone entry/ AddDigit self-transition Digit(n) enclosing superstate State model for telephone dialing UML State Machines Stubbed Transitions Nested states may be suppressed Subsumed transitions that do not S E come from an unlabeled final state A C or go to an unlabeled initial D F pseudostate may (but need not) be B shown as coming from or going to may be abstracted as stubs S E A stub is shown as a small A vertical line (bar) drawn inside the F boundary of the enclosing state B UML State Machines 10 Some More UML State Diagram Notation Nested states are show by an enclosing rectangle Login – The name of the superstate is indicated in Login time = Current Time the upper left-hand corner entry/ type 'login' exit/ login(user name, password) A middle compartment can contain do/ get user name do/ get password state variables (if any) help/ display help Entry, exit, do actions may be defined user-defined event – ‘entry’, ‘exit’, ‘do’ are reserved words and action Self-transitions may occur – from the state back to itself – the entry and exit conditions are performed in that case UML State Machines Eliminating Duplicated Transitions Transitions are often duplicated when there is some transition that can happen from every state – “error” Quit – “quit” – “abort” These duplicates can be combined into a single transition Quit – a transition from a superstate is valid for all of its substates! UML State Machines 11 The Heart Monitor Revisited transition to substate Running transition from superstate Switch on Startup Problem detected Off Alarm Switch off Startup Complete Operational The duplicate transitios have been elimiated! UML State Machines