Sequence diagram

Sequence diagram models the interaction of objects in time. 

This is a diagram that shows a sequence of messaging for a set of participants. Sequence diagrams are used to refine precedent diagrams, a more detailed description of the logic of use cases. 

1. Basic elements 

Participants

Participants are the objects which are directly involved in the interaction. It is depicted as a rectangle, inside of which its name is written. Located at the top of its lifeline. 

Types of objects (participants) that can be displayed on the diagram: 

  1. Object – an instance of the process participant (role in the use case diagram)
  2. Boundary – the class-delimiter – is used for classes that separate the internal structure of the system from the external environment (screen form, user interface, input-output device). The boundary object shows exactly the screen form, which receives and passes data to the handler.
  3. Control – controller class – the active element that is used to perform some operations on objects (software component, module, handler)
  4. Entity – entity-class – is usually used to indicate classes that store some information about business objects (corresponds to a table or database element)
  5. Database – database
  6. Collections – a variety of objects, united by meaning and/or purpose.

Messages 

Messages – a complete piece of information that is sent from one object to another. At the same time, receiving a message initiates the execution of certain actions aimed at solving a particular problem by the object to which this message is sent. It is graphically displayed by arrows of different types. 

There are 5 types of interactions: 

  1. Synchronous message (syncCall) – corresponds to synchronous operation call and implies waiting for a response from the recipient object. Until an answer is received, no action is taken in the System.
  2. Asynchronous message (asyncCall) – which corresponds to an asynchronous operation call and implies that the object can continue to work without waiting for a response.
  3. Reply message — reply message from the called method. This type of message is shown on the diagram as necessary or when the data returned by it carries a semantic load.
  4. Lost message – a message that does not have a message destination, i.e. there is a transmission event for it and no reception event
  5. Found message – a message that does not have a message initiator, i.e. there is a reception event for it and no transmission event

The lifeline of an object is the vertical dashed line associated with a single object in the diagram. 

The focus of control, or the activity of an object, shows the active state of the object in which it can be located, directly performing certain actions. The focus of control is depicted in the form of an elongated narrow rectangle, the upper side of which indicates the beginning of obtaining the focus of control of the object (the beginning of activity), and its lower side – the end of the control focus (end of the activity). 

Recursion – If some object initiates a recursive interaction with itself. In the sequence diagram, recursion is indicated by a small rectangle attached to the right side of the control focus of the object for which this recursive interaction is displayed. 

Syntax

Participants declaration 

You can declare participants using the keyword participant

Instead of the participant, you can use the following keywords: 

  • Actor
  • Boundary
  • Control
  • Entity
  • Database
  • Collections
Код скопирован
  1. @startuml
  2. participant Actor
  3. Actor 0
  4. boundary 1
  5. control 2
  6. entity 3
  7. database 4
  8. collections 5
  9. @enduml

If you immediately announce messages, keywords for participants can be omitted:

Код скопирован
  1. @startuml 
  2. Actor -> Viewer 
  3. @enduml

If you use the long name of the object, or if you need to include non-letter characters in the name, you can use the quotation marks “”

Код скопирован
  1. @startuml  
  2. Anny -> “some strange person”: Who are you? 
  3. @enduml 

You can use alias using the keyword as:

Код скопирован
  1. @startuml
  2. Actor Sam as S
  3. Actor Ban as 2
  4. Actor Ann as 1
  5. S -> B
  6. B -> A
  7. @enduml

Using the order keyword, you can configure the order of the participants in the diagram:

Код скопирован
  1. @startuml  
  2. participant “Superman” order 2
  3. participant “Captain Jack Sparrow” order 4
  4. participant “Frodo Baggins” order 1
  5. participant “Harry Potter” order 3
  6. @enduml 

It is also possible to change the background color of the participant using the color name or its html code:

Код скопирован
  1. @startuml
  2. actor Bob #forestgreen
  3. participant Alice #HotPink
  4. participant “Mythological creature Moidodyr\n(living washbasin)” as L #aqua
  5. @enduml

If the participant is created by the message, you can use the create keyword before the message declaration to emphasize this fact. 

Код скопирован
  1. @startuml 
  2. Bob -> Alice : hello 
  3. create Other 
  4. Alice -> Other : new 
  5. create control String 
  6. Alice -> String 
  7. Alice –> Bob : ok 
  8. @enduml 

Messages

Messages are arrows that are directed from one object (sender) to another (addressee). 

You can change the style of the arrows in the following ways: 

  • end arrow with x to indicate a lost message
  • using \ or / instead of creating only the top or bottom part of the arrow.
  • Repeat the end of the arrow (for example, >> or //) for fine rendering.
  • use- instead - to create a dotted arrow
  • end with an o in the spearhead
  • use bidirectional arrows <->
Код скопирован
  1. @startuml 
  2. Bob ->x Alice 
  3. Bob -> Alice 
  4. Bob ->> Alice 
  5. Bob -\ Alice 
  6. Bob \\- Alice 
  7. Bob //– Alice 
  8. Bob ->o Alice 
  9. Bob o\\– Alice 
  10. Bob <-> Alice 
  11. Bob <->o Alice 
  12. @enduml 

The content of the message that appears above the arrow is recorded following: keyword after the announcement of the sender and addressee. For multi-line messages, use The content of the message that appears above the arrow is recorded: after the announcement of the sender and addressee. For multi-line messages, use \n.

Код скопирован
  1. @startuml 
  2. Bob ->x Alice : How are you? 
  3. Bob -> Alice: Why do not\nyou answer? 
  4. Bob <– Alice: What was \nthe question?  
  5. Bob -\ Alice: All clear with you 
  6. Bob o<->o Alice: I am tired of this 
  7. @enduml 

A participant can send the messages to himself

Код скопирован
  1. @startuml
  2. Bob ->x Alice : How are you?
  3. Bob -> Alice: Why do not\nyou answer?
  4. Bob <– Alice: What was \nthe question?
  5. Bob -\ Alice: All clear with you
  6. Alice->Alice: This is a signal\n to self
  7. Bob o<->o Alice: I am tired of this
  8. @enduml

You can change the color of specific arrows using the following rules:

Код скопирован
  1. @startuml
  2. Bob -[#red]> Alice : hello
  3. Alice -[#0000FF]->Bob : ok
  4. @enduml

You can use inbound or outbound arrows if you need to focus on part of the diagram . To do this, use square brackets to indicate the left [ or right ] side of the diagram. 

Код скопирован
  1. @startuml
  2. [-> A: DoWork
  3. activate A
  4. A -> A: Internal call
  5. activate A
  6. A ->] : << createRequest >>
  7. A<–] : RequestCreated
  8. deactivate A
  9. [<- A: Done
  10. deactivate A
  11. @enduml

The autonumber keyword is used to automatically number messages in a sequence. 

You can designate the number at which the count starts (autonumber start) and the number that will be used as the increment (autonumber start increment). 

Код скопирован
  1. @startuml
  2. autonumber
  3. Bob -> Alice : Authentication Request
  4. Bob <- Alice : Authentication Response
  5. autonumber 42
  6. Bob -> Alice : Another authentication Request
  7. Bob <- Alice : Another authentication Response
  8. autonumber 50 10
  9. Bob -> Alice : Yet another authentication Request
  10. Bob <- Alice : Yet another authentication Response
  11. @enduml

You can specify the number format by specifying it in double quotes: 

Код скопирован
  1. @startuml
  2. autonumber “<b>[000]”
  3. Bob -> Alice : Authentication Request
  4. Bob <- Alice : Authentication Response
  5. autonumber 150 “<b>(<u>##<u>)”
  6. Bob -> Alice : Another authentication Request
  7. Bob <- Alice : Another authentication Response
  8. autonumber 40 10 “<font color=red><b>Message 0”
  9. Bob -> Alice : Yet another authentication Request
  10. Bob <- Alice : Yet another authentication Response
  11. @enduml

To stop or continue numbering, you can use the keywords autonumber stop and autonumber resume, respectively.

Код скопирован
  1. @startuml
  2. autonumber “<b>[000]”
  3. Bob -> Alice : Authentication Request
  4. Bob <- Alice : Authentication Response
  5. autonumber stop
  6. Bob <- Alice : Authentication Response
  7. autonumber resume “<b>[000]”
  8. Bob <- Alice : Authentication Response
  9. @enduml

Message groups 

Grouping messages is possible using the following keywords: 

  • Alt / else – Several alternative fragments (alternative); only the fragment whose condition is true is satisfied
  • Opt – An optional fragment; is satisfied only if the condition is true. Equivalent to one-branch alt
  • Par – parallel; all fragments are executed in parallel
  • Loop – a fragment can be executed several times, and protection denotes the body of the iteration
  • Region – critical region; a fragment can have only one thread running at a time
  • Neg – negative fragment; means wrong interaction
  • Ref – reference; refers to the interaction defined in another diagram. A frame is drawn to encompass the lines of life involved in the interaction. You can define parameters and return a value
  • Sd – sequence diagram; used to outline the entire sequence diagram, if necessary

It is possible to add text that should be displayed in the title. The end keyword is used to end a group. Keep in mind that nested groups are allowed.

Код скопирован
  1. @startuml
  2. Alice -> Bob: Authentication Request
  3. alt successful case
  4. Bob -> Alice: Authentication Accepted
  5. else some kind of failure
  6. Bob -> Alice: Authentication Failure
  7. group My own label
  8. Alice -> Log : Log attack start
  9. loop 1000 times
  10. Alice -> Bob: DNS Attack
  11. end
  12. Alice -> Log : Log attack end
  13. end
  14. else Another type of failure
  15. Bob -> Alice: Please repeat
  16. end
  17. @enduml

Delimiter

You can use the delimiter == to break the diagram into several steps. 

Код скопирован
  1. @startuml 
  2. == Initialization == 
  3. Alice -> Bob: Authentication Request 
  4. Bob –> Alice: Authentication Response 
  5. == Repetition == 
  6. Alice -> Bob: Another authentication Request 
  7. Alice <– Bob: another authentication Response 
  8. @enduml

Notes

Notes can be added to the diagram using the keyword note left or note right immediately after the message. You can write note text between the note left and end note without a colon. This is useful for creating long notes. 

You can post comments on participants using keywords note left of, note right of or note over.   

Код скопирован
  1. @startuml
  2. Alice->Bob : hello
  3. note left: this is a first note
  4. Bob->Alice : ok
  5. note right: this is another note
  6. Bob->Bob : I am thinking
  7. note left
  8. a note
  9. can also be defined
  10. on several lines
  11. end note
  12. note left of Alice #aqua
  13. This is displayed
  14. left of Alice.
  15. end note
  16. note right of Alice: This is displayed right of Alice.
  17. note over Alice: This is displayed over Alice.
  18. note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice.
  19. note over Bob, Alice
  20. This is yet another
  21. example of
  22. a long note.
  23. end note
  24. @enduml

In addition to comments, links can be inserted into diagrams using the keyword ref over 

Код скопирован
  1. @startuml
  2. participant Alice
  3. actor Bob
  4. ref over Alice, Bob : init
  5. Alice -> Bob : hello
  6. ref over Bob
  7. This can be on
  8. several lines
  9. end ref
  10. @enduml

Lifeline

On the lifeline, you can specify delays, gaps and focuses of control or activation or deactivation of the object lifeline 

Using the punctuator “” it is possible to designate a delay, providing it with a comment if necessary. Delay is a segment of points on the lifeline of an object. 

You can display the gaps in the diagram using the “|||” punctuator; this construction simply lengthens the distance between the events. 

The keywords activate and deactivate are used to indicate the activation of an object. 

Activate and deactivate apply to the previous message. That is, the activation of the participant begins after the message that was before the announcement. 

Код скопирован
  1. @startuml
  2. Alice -> Bob: Hi, are you sleeping?
  3. …5 minutes later…
  4. Bob <- Alice: OK, bye then
  5. |||
  6. Alice -> Bob: Hi, are you sleeping?
  7. Bob –> Alice: No
  8. ||50||
  9. Alice -> Bob: make me coffee please
  10. activate Bob
  11. Bob –> Alice: add milk?
  12. Alice -> Bob: Yes
  13. deactivate Bob
  14. @enduml

To show the end of the participant’s line of existence, after its activation, you can use the keyword destroy

Код скопирован
  1. @startuml 
  2. Alice -> Bob: 1       
  3. activate Bob 
  4. Bob –> Alice: 2 
  5. Alice -> Bob: 3 
  6. destroy Bob 
  7. … 
  8. @enduml

Recursion or nested activity lines can be indicated on the diagram as follows: simply activate the same participant after different messages. You can add color: 

Код скопирован
  1. @startuml
  2. participant User
  3. User -> A: DoWork
  4. activate A
  5. A -> A: Internal call
  6. activate A #DarkSalmon
  7. A -> B: << createRequest >>
  8. activate B
  9. B –> A: RequestCreated
  10. deactivate B
  11. deactivate A
  12. A -> User: Done
  13. deactivate A
  14. @enduml