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:
- Object — an instance of the process participant (role in the use case diagram)
- 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.
- Control — controller class — the active element that is used to perform some operations on objects (software component, module, handler)
- Entity — entity-class — is usually used to indicate classes that store some information about business objects (corresponds to a table or database element)
- Database — database
- 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:
- 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.
- Asynchronous message (asyncCall) — which corresponds to an asynchronous operation call and implies that the object can continue to work without waiting for a response.
- 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.
- Lost message — a message that does not have a message destination, i.e. there is a transmission event for it and no reception event
- 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
- @startuml
- participant Actor
- Actor 0
- boundary 1
- control 2
- entity 3
- database 4
- collections 5
- @enduml

If you immediately announce messages, keywords for participants can be omitted:
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 “”
:
You can use alias using the keyword as
:
Using the order
keyword, you can configure the order of the participants in the diagram:
- @startuml
- participant “Superman” order 2
- participant “Captain Jack Sparrow” order 4
- participant “Frodo Baggins” order 1
- participant “Harry Potter” order 3
- @enduml

It is also possible to change the background color of the participant using the color name or its html code:
- @startuml
- actor Bob #forestgreen
- participant Alice #HotPink
- participant “Mythological creature Moidodyr\n(living washbasin)” as L #aqua
- @enduml

If the participant is created by the message, you can use the create
keyword before the message declaration to emphasize this fact.
- @startuml
- Bob -> Alice : hello
- create Other
- Alice -> Other : new
- create control String
- Alice -> String
- Alice –> Bob : ok
- @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
<->
- @startuml
- Bob ->x Alice
- Bob -> Alice
- Bob ->> Alice
- Bob -\ Alice
- Bob \\- Alice
- Bob //– Alice
- Bob ->o Alice
- Bob o\\– Alice
- Bob <-> Alice
- Bob <->o Alice
- @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
.
- @startuml
- Bob ->x Alice : How are you?
- Bob -> Alice: Why do not\nyou answer?
- Bob <– Alice: What was \nthe question?
- Bob -\ Alice: All clear with you
- Bob o<->o Alice: I am tired of this
- @enduml

A participant can send the messages to himself
- @startuml
- Bob ->x Alice : How are you?
- Bob -> Alice: Why do not\nyou answer?
- Bob <– Alice: What was \nthe question?
- Bob -\ Alice: All clear with you
- Alice->Alice: This is a signal\n to self
- Bob o<->o Alice: I am tired of this
- @enduml

You can change the color of specific arrows using the following rules:
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.
- @startuml
- [-> A: DoWork
- activate A
- A -> A: Internal call
- activate A
- A ->] : << createRequest >>
- A<–] : RequestCreated
- deactivate A
- [<- A: Done
- deactivate A
- @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).
- @startuml
- autonumber
- Bob -> Alice : Authentication Request
- Bob <- Alice : Authentication Response
- autonumber 42
- Bob -> Alice : Another authentication Request
- Bob <- Alice : Another authentication Response
- autonumber 50 10
- Bob -> Alice : Yet another authentication Request
- Bob <- Alice : Yet another authentication Response
- @enduml

You can specify the number format by specifying it in double quotes:
- @startuml
- autonumber “<b>[000]”
- Bob -> Alice : Authentication Request
- Bob <- Alice : Authentication Response
- autonumber 150 “<b>(<u>##<u>)”
- Bob -> Alice : Another authentication Request
- Bob <- Alice : Another authentication Response
- autonumber 40 10 “<font color=red><b>Message 0”
- Bob -> Alice : Yet another authentication Request
- Bob <- Alice : Yet another authentication Response
- @enduml

To stop or continue numbering, you can use the keywords autonumber stop
and autonumber resume
, respectively.
- @startuml
- autonumber “<b>[000]”
- Bob -> Alice : Authentication Request
- Bob <- Alice : Authentication Response
- autonumber stop
- Bob <- Alice : Authentication Response
- autonumber resume “<b>[000]”
- Bob <- Alice : Authentication Response
- @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.
- @startuml
- Alice -> Bob: Authentication Request
- alt successful case
- Bob -> Alice: Authentication Accepted
- else some kind of failure
- Bob -> Alice: Authentication Failure
- group My own label
- Alice -> Log : Log attack start
- loop 1000 times
- Alice -> Bob: DNS Attack
- end
- Alice -> Log : Log attack end
- end
- else Another type of failure
- Bob -> Alice: Please repeat
- end
- @enduml

Delimiter
You can use the delimiter ==
to break the diagram into several steps.
- @startuml
- == Initialization ==
- Alice -> Bob: Authentication Request
- Bob –> Alice: Authentication Response
- == Repetition ==
- Alice -> Bob: Another authentication Request
- Alice <– Bob: another authentication Response
- @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
ornote over.
- @startuml
- Alice->Bob : hello
- note left: this is a first note
- Bob->Alice : ok
- note right: this is another note
- Bob->Bob : I am thinking
- note left
- a note
- can also be defined
- on several lines
- end note
- note left of Alice #aqua
- This is displayed
- left of Alice.
- end note
- note right of Alice: This is displayed right of Alice.
- note over Alice: This is displayed over Alice.
- note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice.
- note over Bob, Alice
- This is yet another
- example of
- a long note.
- end note
- @enduml

In addition to comments, links can be inserted into diagrams using the keyword ref over
- @startuml
- participant Alice
- actor Bob
- ref over Alice, Bob : init
- Alice -> Bob : hello
- ref over Bob
- This can be on
- several lines
- end ref
- @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.
- @startuml
- Alice -> Bob: Hi, are you sleeping?
- …
- …5 minutes later…
- Bob <- Alice: OK, bye then
- |||
- Alice -> Bob: Hi, are you sleeping?
- Bob –> Alice: No
- ||50||
- Alice -> Bob: make me coffee please
- activate Bob
- Bob –> Alice: add milk?
- Alice -> Bob: Yes
- deactivate Bob
- …
- @enduml

To show the end of the participant’s line of existence, after its activation, you can use the keyword destroy
- @startuml
- Alice -> Bob: 1
- activate Bob
- Bob –> Alice: 2
- Alice -> Bob: 3
- destroy Bob
- …
- @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: