8. Sequence Diagrams

A sequence diagram is an easily comprehensible visualization of single scenarios or examples of business processes with regard to their behavior in time. It focuses on when the individual objects interact with each other during execution. It is particularly useful for modeling usage scenarios such as the logic of methods and the logic of services. Poseidon now makes it even easier to model with the addition of inspections. Whenever Poseidon has a suggestion for your diagram, a red exclamation point will appear near the questionable item. Mouse over the exclamation point to reveal the critique.

The diagram essentially includes a timeline that flows from the top to the bottom of the diagram and is displayed as a dotted line. The interaction between objects is described by specifying the different kinds of messages sent between them. Messages are called stimuli. They are displayed as arrows; the diverse arrowheads stand for different kinds of messages (see below).

New in UML 2 is the frame surrounding the the diagram with a label in the upper left corner. These frames are also used for interaction occurrences and combined fragments within the diagram itself, such as with a loop or switch statement (called 'alt' in UML).

The following diagram shows a typical example:

Figure 10.5. A Sequence diagram.

A Sequence diagram.

Objects and Lifelines

After creating or changing objects, they are automatically arranged in the Diagram pane. Focus of control is also automatically rendered by Poseidon. Each object can have a lifeline, which is represented by a dashed line that extends downward from the object. The lifeline is a time axis for the diagram, with time passing as one moves down the lifeline. Focus of control is displayed along this lifeline as a thin rectangle.

An object receives a message and then begins an activity resulting in an execution occurrence. The object now has the focus of control. An activated object is either executing its own code or is waiting for the return of another object to which it has sent a message.

Self messages

In Poseidon, an object can send a message to itself, called a self-message. In the case of a message to itself, the arrow starts and finishes on the object's lifeline. A self stimulus is created by dropping the stimulus target point on the source object.

Selecting an operation

A message can be regarded as a procedure call and can be connected with any operation provided by the receiving object, depending on the type of the receiving object. This is achieved by connecting the message with an action that will cause the class operation to be called.

Try it Yourself - Assign an operation to a message
  1. Open a class diagram.

  2. Create a new class by double-clicking the 'New Class' button on the toolbar, then click anywhere in the class diagram.

  3. With the new class selected, add an attribute by clicking the 'Add Operation' button in the Properties tab toolbar (below the diagram).

  4. Open a sequence diagram.

  5. Add an object to the diagram by clicking the 'New Lifeline' button, then click anywhere in the diagram.

  6. Add a message and new object by clicking the 'New Synchronous Message' rapid button.

  7. Select the new lifeline, then set the type of the lifeline from the Type dropdown by selecting the class created in step 2.

  8. Select the message in the diagram.

  9. Finally, select 'operation_1' (created in step 3) from the Operation dropdown menu of the message properties tab.

Similarly, a message can access an attribute in the originating object.

Try it Yourself - Add an Accessed Attribute to a Message
  1. Open a class diagram.

  2. Create two new classes by double-clicking the 'New Class' button on the toolbar, then click twice anywhere in the class diagram. By default, these will be called 'Class_1' and 'Class_2'.

  3. With 'Class_2' selected, add an attribute by clicking the 'Add Attribute' button in the Properties tab toolbar (below the diagram).

  4. From the resulting attribute properties tab, select 'Class_1' from the Type dropdown.

  5. Open a sequence diagram.

  6. Add an object to the diagram by clicking the 'New Lifeline' button, then click anywhere in the diagram. Set the type of this new lifeline by selecting 'Class_2' from the Type dropdown of the lifeline properties tab.

  7. Add a message and new object by clicking the 'New Synchronous Message' rapid button.

  8. Select the message in the diagram.

  9. Finally, select 'attribute_1' from the Accessed Attribute dropdown menu of the message properties tab.

Combined fragments

A fragment is simply a piece of a sequence diagram. These fragments can be combined to form logical units surrounded by a frame, each with one or more interaction operands and an interaction operator. The operators are indicated by the label given in the frame surrouding the fragment. Operands are contained within this frame, and can be separated into different regions with a dashed horizontal line. Frames are created by first selecting the desired operator in the toolbar, then dragging a box around the lifelines and messages to be included within the frame.

Take, for instance, the loop operator. The loop will execute one fragment a specified number of times, and therefore does not have separate regions. The conditional for the loop can either be a number or a boolean expression.

For this combined fragment, the minimum and maximum iterations can be added directly in the properties panel. These are reflected in the fragment label.

Constraints can also be added by navigating to the operand (click on the operand name) and then adding a guard condition.

The 'alt' operator functions like a switch statement, where the conditional (guard) for the first region is evaluated, then the second, and so on through the regions so that at most one of the operands will execute. The final region should contain the 'else' region. In Poseidon, additional regions can be created (and deleted) directly in the diagram with rapid buttons.

Activations

An activation shows the period of time during which an object will perform an action, either directly or through a subordinate procedure. It is represented as a tall thin rectangle with the top aligned with its point of initiation and the bottom aligned with its point of completion.

Now, let's consider how Poseidon deals with starting and terminating activations. When an object receives a stimulus, an activation is created that starts at the tip of the incoming arrow. When an object sends a stimulus, an existing activation is terminated at the tail of the outgoing arrow. There are two exceptions: First, an outgoing send stimulus does not terminate an existing activation, because it represents an asynchronous message. Second, if an object has explicitly set the focus of control, its activation will continue during the whole lifetime.

8.1. Diagram Elements

  • Objects - Elements responsible for sending and receiving messages.

  • Synchronous Message - The message sender waits for a response before continuing on.

  • Asynchronous Message - Illustrates an asynchronous message, which means that it is regarded as a signal. As such, the sender doesn't wait for an answer from the receiver.

8.2. Toolbar

Select
Add or remove space between elements
Add new lifeline
Create synchronous message
Create asynchronous message
Create destroy message
Create lost message
Create found message
Create asynchronous message from lifeline to gate
Create asynchronous message from gate to lifeline
Create an interaction occurrence
Create a combined fragment
Comment
Connect Comment to Element
Add Shape
Repaint