Web Sequence Diagrams - Part II

In the previous article I introduced Web Sequence Diagrams and showed how to define interactions between participants and how to execute conditional logic. In this article I want to show you how Web Sequence Diagrams supports the remainder of the functionality commonly found in sequence diagrams and then explore some of the features that Web Sequence Diagrams charges for so that you can make an educated decision as to whether or not you want to pay for its premium features.

Optional Segments Using the OPT Construct

Last time, we reviewed the ALT construct for defining conditional logic to be executed. While the ALT construct can be used for a simple if() statement, its power is in its ability to define other alternate conditions. If you want to represent a conditional block of logic without alternate paths, then an OPT construct may suit you better.

Web Sequence Diagrams supports the OPT construct using the OPT keyword:

Listing 1. Opt Example

title OPT Construct
Client -> Server: Do Something
opt Operation Failed
  Client -> "Fault Queue": Log failure
end

This yields the following sequence diagram:

Figure 1. Using the OPT Construct in a Sequence Diagram

OPT segments look very similar to ALT segments and, although you can add an "else" clause to an OPT in Web Sequence Diagrams, OPT segments are really just meant to show an optional block of logic to run, not a series of alternate blocks of logic.

Loops

Whether they are while, do while, or for loops, all programming languanges have some sort of iteration functionality. Sequence Diagrams represent iterative processing using loops. Web Sequence Diagrams supports loops using the LOOP construct:

  1. Define a loop using the "loop" construct
  2. Define a condition that constrains the loop
  3. Define the logic to be performed in the loop
  4. Define an end statement to mark the end of the loop

As an example, consider a client retrieving a list of elements from a server and iterating over the results:

Listing 2. Loop Example

title Loop Example

Client -> Server: Get List
activate Client
activate Server

Server --> Client: List
deactivate Server

loop All Elements in the List
  Client -> Client: Handle Element
end

This yields the following sequence diagram:

Figure 2. Using the LOOP Construct in a Sequence Diagram

When reading this sequence diagram, all of the logic contained inside the LOOP segment will be performed for each iteration of the loop. In addition to looping over a collection, as shown in this example, I have also seem people define the condition as the number of time to execute, such as loop 1000 times ... do stuff ... end.

Notes

If you would like to annotate your sequence diagram with additional information you can do so using notes. Notes are constructed using the "note" keyword, followed by instructions on where to place the note, the note content, and then terminated with an "end note" keyword. Valid positions are:

  • over participant
  • over participant1, participant2, ..., participant n
  • left of participant
  • right of participant

Listing 3 shows an example of defining notes and positioning them in various places in the sequence diagram.

Listing 3. Note Example

title Note Example

participant Client as client
participant Server as server

note left of client
   Note left of Client
end note

client -> server: Get List
activate client
activate server

note right of server
  Note right of server
end note

note over client, server
  Note over both Client and Server
end note

server --> client: List
deactivate server


loop All Elements in the List

  note over client
    Note over client
  end note

  client -> client: Handle Element

  note over client
    This is a note \n with a manual line break
    And a natural line break
  end note

end

This yields the sequence diagram shown in figure 3.

Figure 3. Adding Sequence Diagram Notes

One additional note about notes, just like life line signals, you can break up a note into multiple lines using "\n". But unlike lifeline signals, you can also break a note into multiple lines with a normal carriage return.

Commercial Features

We've reviewed the free features found in Web Sequence Diagrams but, in addition to hosting an instance in your own environment, there are a few features reserved for commercial use. These include:

  • Parallel Signals: denotes things that happen at the same time
  • States: allows you to display states in rounded rectangles
  • Autonumbering: automatically prefixes all signals with a number in case you want to display order of operations

You can learn more about these, as well as the free features, on their Examples page. But for your reference, figure 4 shows a screen shot of the 3 commercial features.

Figure 4. Commercial Features

Summary

This first article in this series introduced you to Web Sequence Diagrams and demonstrated how to create a diagram with participants, how to send signals to and from participants, how to allow a participant to send signals to itself, how to use activation bars, and how to use the alt keyword to define conditional segments in your sequence diagram. In this article we built on this foundation and saw how to use optional segments, loops, and notes and then we briefly reviewed the features Web Sequence Diagrams provides commercially for paying customers.