State and Sequence Diagrams Modelling dynamic information So far we have seen: Use Case Diagrams – requirements capture, interface modelling Use Case Scripts – more detailed and with alternate, less common cases noted Class Diagrams – description of the structure of a class and possibly its relationship with other classes These are static descriptions of software n.a.shulver@staffs.ac.uk State and Sequence Diagrams Dynamic Modelling In order to model the dynamic (run-time) relationships we need different tools The UML provides us with State Diagrams and Object Sequence Diagrams (OSD) There are others... n.a.shulver@staffs.ac.uk State and Sequence Diagrams State Diagrams What do state diagrams describe? When things happen The life-cycle of an instance (object) How useful are they? Often but not always used Logically links a Class Diagram to a State Diagram – events are operations on classes in the Class Diagram Clarifies the internal state and external behaviour of an object n.a.shulver@staffs.ac.uk State and Sequence Diagrams State Diagram Elements start point states end point(s) transitions (labeled with events) create invoice Unpaid pay Paid destroy invoice n.a.shulver@staffs.ac.uk State and Sequence Diagrams State Diagram examples mate Black wins White's turn stalemate move move Black's turn Draw stalemate mate White wins A chess game class White takes the first turn, then turns alternate After any move “mate” or “stalemate” may occur n.a.shulver@staffs.ac.uk State and Sequence Diagrams State Change Events A state describes a particular condition of an object for a period of time – it waits for an event to change state or to “fire” Events can be of the following types: Change – occurs when a condition becomes true i.e. a boolean Call/Signal – occurs when an object receives a call for one of its operations (from another object or from itself) Time trigger n.a.shulver@staffs.ac.uk State and Sequence Diagrams Guards Known as a guard condition It guards the transition i.e. the transition can only take place if the guard condition evaluates to true Guards are constraints on behaviour Example in Visio n.a.shulver@staffs.ac.uk State and Sequence Diagrams Event Types Kinds of events receipt of a call on an operation - a message receipt of an explicit signal from another object - a message a condition becoming true passage of a designated period of time Basic semantics about event labelled transitions events are triggers that activate state transitions if an event occurs and guard is false then the event is ignored (events are not stored) n.a.shulver@staffs.ac.uk State and Sequence Diagrams Visio Support – State Chart n.a.shulver@staffs.ac.uk State and Sequence Diagrams Inside States Three kinds of internal content for a state These represent the internal activities or state activities that are executed in that state Entry actions Exit actions Internal transitions - do not cause a state change All the above represent the operations for the class that is being modelled n.a.shulver@staffs.ac.uk State and Sequence Diagrams How to find States A state chart captures all the possible responses of a single object to all the Use Cases in which it is involved So a cross reference of which of the Use Cases a class is involved in will give you possible states for an object of that class The state chart can be seen as a description of all the possible life-cycles for an object Provides a dynamic view of a class n.a.shulver@staffs.ac.uk State and Sequence Diagrams Object Sequence Diagrams OSDs depict a Use Case Script visually but also show the classes (objects) used in the script They show the sequence of object interaction Read the Visio tutorials for detailed information on the Sequence Chart The next slides show example Sequence Charts n.a.shulver@staffs.ac.uk State and Sequence Diagrams Simple Example M ary: Hotel Guest 1: leaving M aryX: Guest 324: Room fred: Cleaner hilton: Hotel FredX: CleanerX 2: I amvacating 3: cleanm e 4: cleanroom324 5: donerom m324 6: youareclean 7: I amavailable n.a.shulver@staffs.ac.uk State and Sequence Diagrams n.a.shulver@staffs.ac.uk State and Sequence Diagrams Sequence Diagrams Purpose Captures the behaviour of a single use case - how messages are sent within a use case Shows how objects interact with each other - show how they send and receive messages Focuses on time Syntax Object Name : Class Time message n.a.shulver@staffs.ac.uk State and Sequence Diagrams Sequence Diagram Caller : Person Example Nokia : Telephone System Receiver : Person 1: pick up 2: dial tone 3: dial 5: ring notification 4: ring 6: pick up n.a.shulver@staffs.ac.uk State and Sequence Diagrams Sequence Diagram Types of messages simple flat flow of control - control is passed without describing any details synchronous nested flow of control - operation call handling of the message is completed before the caller resumes execution asynchronous there is no explicit return to the caller the sender continues to execute after sending the message without waiting for it to be handled n.a.shulver@staffs.ac.uk State and Sequence Diagrams Sequence Diagram Activation :A Time during which object performs an action either directly or through another object (subcontractor) Return :B 1: :A :B Implicit Explicit n.a.shulver@staffs.ac.uk State and Sequence Diagrams Drawing an OSD Select a Use Case for which you have a Script Make a list of classes which appear on that script (you may have done this already if you have created a Class Diagram) Use Visio to create a new Sequence Diagram Do the simple things first and build it up Place the objects (classes) on the diagram Think about the order that you put them in n.a.shulver@staffs.ac.uk State and Sequence Diagrams Drawing an OSD When creating messages, for the first draft just use the simple message call shape Refine to synchronous or asynchronous at the 2nd draft Finally, put the rectangle duration box (operation life) overlay onto the timeline for each object Then review it and do a 2nd draft n.a.shulver@staffs.ac.uk State and Sequence Diagrams Self-paced tutorial Create a State Diagram in Visio for the third version of the example Soup Menu application Hints: states include ingredient choice (three types) and action choice What kind of transitions are there? - write down a list Hints: remember that each transition is a flow from one state to another Finally, what events/actions are there for the transitions in your list? n.a.shulver@staffs.ac.uk