Software Specification 2IX20 Julien Schmaltz (slides from A. Serebrenik) Lecture 05: Interaction diagrams / Sequence diagrams
This week sources Slides by Site by David Meredith, Aalborg University, DK Marie-Elise Kontro, Tampere University, FI Kirill Fakhroutdinov GE Healthcare, USA 2
Missing link» Use case diagram describes tasks that system must help actors to perform» Class diagram describes classes required to realise use cases and relationships between these classes» Interaction diagrams describe how objects interact to realise the use case scenarios» Interaction diagrams should be consistent with the corresponding class diagrams and use case diagrams / SET / W&I 26/02/15 3
UML diagram types Interactions: Chapter 17 of the UML standard. (Sequence diagrams in Chapter 17.8) 4
Interaction diagrams Sequence Diagram TODAY Communication Diagram Interaction Overview Diagram Timing Diagrams / SET / W&I 26/02/15 5
Sequence diagrams» The most common kind of Interaction Diagrams shows how actors and objects interact to realise a use case scenario focuses on the Message interchange between a number of Lifelines :A :B participants: actors, objects, etc. involved in the use case. time lifelines: represent the lifetime of a participant Notation object:class :Class = anobject:class ttp://i.stack.imgur.com/rqpvd.gif 6
Sequence diagrams» The most common kind of Interaction Diagrams shows how actors and objects interact to realise a use case scenario focuses on the Message interchange between a number of Lifelines :A sends message msg2 to :B and waits for :B s response: synchronous communication / SET / W&I response, return (optional) 26/02/15 Synchronous means that the caller waits for the callee to complete. The response is optional. 7
Sequence diagrams» The most common kind of Interaction Diagrams shows how actors and objects interact to realise a use case scenario focuses on the Message interchange between a number of Lifelines msg1 :A :B msg2 Order of participants = order of the first participation in the interaction Most of the message sends on arrows from left to right response / SET / W&I 26/02/15 8
Sequence diagrams» The most common kind of Interaction Diagrams shows how actors and objects interact to realise a use case scenario focuses on the Message interchange between a number of Lifelines msg1 :A :B msg2 response Intuition: Messages are sent to objects of classes with the corresponding operations. Class A has method msg1 Class B has method msg2 Implicit rule: A message is sent before it is received. / SET / W&I 26/02/15 9
Sequence diagrams» The most common kind of Interaction Diagrams shows how actors and objects interact to realise a use case scenario focuses on the Message interchange between a number of Lifelines msg1 :A :B msg2 Semantics: defined by a pair of two sets of traces: A set of accepted/valid traces A set of rejected/invalid traces Trace = sequence (possibly infinite) of events. / SET / W&I response 26/02/15 Each message has for semantics the trace <sendevent,receiveevent> 10
Sequence diagrams :A :B msg1 activation msg2 When a message is received by an object, a new activation is started. Activation is also known as execution specification. / SET / W&I 26/02/15 11
Sequence diagrams» We can have multiple activations of the same object: Activation resulting from message to self is slightly offset from older activation :A :B msg1 msg msg2 / SET / W&I 26/02/15 12
Sequence diagrams If there is no concurrency, exactly one object is computing at any given instant (Imaginary) horizontal line can cross only one dark area :A :B msg1 msg msg2 13
Sequence diagrams If there is no concurrency, exactly one object is computing at any given instant (Imaginary) horizontal line can cross only one dark area Activations behave as a stack msg1 :A :B msg msg2 A1 Actor A2 A1 Actor B A1 Actor 14
Sequence diagrams If there is no concurrency, exactly one object is computing at any given instant (Imaginary) horizontal line can cross only one dark area Activations behave as a stack Only actors can send messages out of the blue Objects send messages only when they have been made active by receiving a message from another participant msg1 :A :B msg msg2 A1 Actor A2 A1 Actor B A1 Actor 15
What if we want concurrency? Some communication might be asynchronous :A :B msg1 msg msg2 Note the different arrow type than for synchronous communications 16
Question for you» Messages are partially ordered (before/after) Response to msg should be received before msg2 is sent. :A :B» Why isn t the order total? msg1 msg msg2 / SET / W&I 26/02/15 17
Question for you» Messages are partially ordered (before/after) Response to msg should be received before msg2 is sent. :A :B» Why isn t the order total? independent events on different lifelines are not comparable e.g., concurrent execution resulting from asynchronous messages msg1 msg msg2 / SET / W&I 26/02/15 18
Creating and destroying objects Prince Charming getname() :Girl n = getname(): Cinderella Label: assignment of return value to a variable; value returned shown after colon destroy() Object destruction: synchronous message to the object with an X-terminating lifeline new Princess(n) Creating a new object: asynchronous message directly to the head of the new participant :Princess / SET / W&I 26/02/15 19
Summary(1) amessage(aparameter) Synchronous message. Caller waits for callee to complete before resuming execution. Message return. Receiver resumes execution. asyncmsg(aparameter) Asynchronous message. Caller does not wait for callee to complete before resuming execution. 20
Summary(2) <<create>> msg(aparam) :::A Creation of an instance of class :A. <<destroy>> msg(aparam) Destruction of an instance of the lifeline object. 21
Combined fragments and operators» Combined fragments divide a sequence diagram into different areas with different behaviour» Each combined fragment has an operator, one or more operands, and zero or more guard condition» The operator determines how operands execute» Guard conditions determine whether their operands execute 22
Not treated in this course Not treated in this course
Alternatives This is an example of a combined fragment introduced by the operator alt (alternatives). Semantics: union of the traces of the operands added to the set of valid executions of the diagram. 24
Optional behaviour Prince Charming getname() :Girl This is an example of a combined fragment introduced by the operator Opt (option). Opt n = getname() [n = Cinderella] guard Semantics: equivalent to alt with one non-empty operand and one empty operand. destroy() new Princess(n) :Princess Continue with organizing the wedding 25
Alternative and optional behaviours opt: creates a single branch if (condition) then op1() alt: creates multiple branches if (condition1) then op2() else if (condition2) then op3() else op4() 26
Loop and break loop allows you to model iteration. When the break guard condition evaluates to true, the break operand executes, and the loop terminates, 27
Loop types 28
For each element/object example 29
Combined fragments: par, seq and strict Search Google, Bing and Ask in any order, possibly parallel. Search Google, Bing and Yahoo in the strict sequential order. Search Google possibly parallel with Bing and Yahoo, but search Bing before Yahoo.» seq: Different lifelines: parallel The same lifeline: sequential / SET / W&I 26/02/15 30
Parallel (par) semantics» Traces of all possible interleaving.» Ordering within an operand is preserved. 31
Par: shorthand» Co-region: par-combined fragment with one lifeline The two fragments are equivalent / SET / W&I 26/02/15 32
Weak sequencing (seq) semantics» Set of traces with the following properties: 1. Ordering within each operand is maintained 2. Interleaving between different lifelines of different operands 3. Ordering on the same lifeline is preserved. 33
Strict sequencing (strict) semantics» Strict ordering between operands at the first level.» At lower levels: no direct comparison. 34
Combining different combined fragments» Co-region» Option» Loop» Ref (interaction use) allows to use (or call) another interaction. / SET / W&I 26/02/15 35 http://www.uml-diagrams.org/examples/sequence-examples-online-bookshop.png
Interaction use In general, referencing should be done as DiagType DiagName [(ParType:ParName)] : [: RetValType]» In practice, types are frequently omitted Checkout no parameters, no return value Debit Account (accountnumber, amount) parameters, no return value Balance Lookup (accountnumber): Real parameters, return value / SET / W&I 26/02/15 36
Interaction use / SET / W&I 26/02/15 37
Alternative way to represent a return value Gate: a message with one end connected to the sequence diagram's frame and the other end to a lifeline. / SET / W&I 26/02/15 38
Question for you» How to model exception handling in sequence diagram?» NB: UML provides neither notation to model exception handling in sequence diagrams nor any reasoning why it is absent.» Still, what would be your solution? / SET / W&I 26/02/15 39
Question for you» How to model exception handling in sequence diagram?» NB: UML provides neither notation to model exception handling in sequence diagrams nor any reasoning why it is absent.» Still, what would be your solution? Combined fragments alt/break / SET / W&I 26/02/15 40
Negative / invalid behaviours The interaction operator neg describes combined fragment of traces that are defined to be negative (invalid). Negative traces are the traces which occur when the system has failed. All interaction fragments that are different from the negative are considered positive, meaning that they describe traces that are valid and should be possible. Should we receive back timeout message, it means the system has failed. 41
Neg: another example One cannot open the microwave door when it is cooking. Also notice the from 1 to time loop. / SET / W&I 26/02/15 http://www.zicomi.com/combinedfragmentnegative.jsp 42
Not treated in this course Not treated in this course
State invariant Attribute t of Task should have value complete A transaction on SavingsAccount is permitted only if the balance remains positive. 44
Duration constraint / SET / W&I 26/02/15 45
Summary of the syntax http://www.uml-diagrams.org/sequence-diagrams.html 46
Building sequence diagrams» Set the context» Identify participants that should interact to realize the use-case scenario / to respond to messages arriving at the interface. Place the initiator of the interaction on the left of the diagram Multiple objects of the same class: name each» Set the lifeline for each participant» Add messages by drawing arrows Order messages from left to right, top to bottom Show how they are passed from one object to another Include any parameters in parentheses Exclude obvious return values» Add activations to each participant s lifeline» Validate the sequence diagram / SET / W&I 26/02/15 47
Validation» Recall: Interaction diagrams should be consistent with the corresponding class diagrams and use case diagrams» Rule: Objects in [sd] should be instances of classes in [cd]» Rule: Name of the message [sd] should match an operation in the receiver s class [cd] ATM +withdrawcash(accountnumber, amount) http://www.ibm.com/developerworks/rational/library/3101.html 48
Validation» Recall: Interaction diagrams should be consistent with the corresponding class diagrams and use case diagrams» Rule: Objects in [sd] should be instances of classes in [cd]» Rule: Name of the message [sd] should match an operation in the receiver s class [cd] The message is a getter! +name Girl 49
Validation» Recall: Interaction diagrams should be consistent with the corresponding class diagrams and use case diagrams» Rule: Objects in [sd] should be instances of classes in [cd]» Rule: Name of the message [sd] should match an operation in the receiver s class [cd]» Rule: If a message is sent from A to B [sd] then there should be an association from the class of A to the class of B [cd]» Rule: If use-case A generalizes use-case B [ucd] then the sequence diagram of A should be a subgraph of the sequence diagram of B [sd]. 50
Validation» Recall: Interaction diagrams should be consistent with the corresponding class diagrams and use case diagrams» Briand et al. mention 120 consistency rules http://squall.sce.carleton.ca/pubs/tech_report/ TR_SCE-03-01.pdf» Active research domain around 2005 Prototype implementations Inconsistency detection vs. inconsistency resolution 51
UML diagram types: so far / SET / W&I 26/02/15 52