Main Content

Author Sequence Diagrams Interactively

Sequence diagrams describe operational system behaviors and allow you to validate an expected sequence of events as system design proceeds. Sequence diagrams are integrated with architectures in System Composer™ to represent these expected interactions between architecture components. You can create, edit, and simulate sequence diagrams in System Composer by accessing the Architecture Views Gallery.

This topic shows you how to author a sequence diagram to describe the scenario of a pedestrian crossing the street at an intersection with traffic lights. You will learn how to:

  • Add lifelines and messages with message labels including triggers and constraints to represent interactions.

  • Include fragments and operands with constraints to further specify the behavior of the interaction.

Note

This example uses Stateflow® blocks. If you do not have a Stateflow license, you can open and simulate the model but can only make basic changes, such as modifying block parameters.

For a roadmap of the sequence diagram topics, see Describe System Behavior Using Sequence Diagrams.

Traffic Light Example

This traffic light example contains sequence diagrams to describe pedestrians crossing an intersection. The model describes these steps:

  1. The traffic signal cycles from red to green to yellow.

  2. When the pedestrian crossing button is pressed, if the traffic signal is green, the traffic signal transitions from yellow to red for a limited time.

  3. The pedestrians cross while the walk signal is active.

Open the architecture model that contains the sequence diagrams.

model = systemcomposer.openModel('TLExample');

To view the sequence diagrams, open the Architecture Views Gallery.

openViews(model)

Create New Sequence Diagram

To open or create sequence diagrams:

  1. Open the Architecture Views Gallery by navigating to Modeling > Sequence Diagram.

  2. To create a new sequence diagram, click New > Sequence Diagram.

  3. A new sequence diagram called SequenceDiagram1 is created in the View Browser, and the Sequence Diagram tab becomes active. Under Sequence Diagram Properties, rename the sequence diagram Inhibit.

Add Lifelines and Messages

A lifeline in a sequence diagram represents a component in your architecture. You can represent communication, or an exchange of information, between two lifelines with a message. Information exchange is represented by ports on architecture components. In a sequence diagram, a message must consist of at least two ports and one connector from your architecture. Use message labels on messages to describe the expected conditions for this information exchange.

  1. To add a lifeline, select Component > Add Lifeline. A new lifeline with no name is created and is indicated by a dotted line.

    A new lifeline being created in a sequence diagram.

  2. Click the down arrow and select source. The source lifeline detects when the pedestrian presses the crossing button. Use the down arrow to add four more lifelines named poller, switch, controller, and lampController. The poller lifeline checks if the pedestrian crossing button has been pressed. The switch lifeline processes the signal. The controller lifeline determines which color the pedestrian lamp and traffic light should display. The lampController lifeline changes the traffic light colors.

    A sequence diagram with five lifelines.

  3. Draw a line from the source lifeline to the poller lifeline. Start to type sw in the To box, which automatically fills in as you type. Once the text fills, select sw.

    A sequence diagram with a message between the source and poller lifelines.

    Since the switchout port and sw port are connected in the model, a message is created from the switchout port to the sw port in the sequence diagram.

  4. A message label has a trigger, an optional guard, and an optional constraint in the form of trigger[guard]{constraint}, where trigger represents the identifying event for this message, guard represents an additional condition to determine whether the message occurs, and constraint is an expression that is expected to be true when this message occurs.

    • In signal events, the trigger follows this format: direction(signal [+|-] value) which specifies a triggering edge with a direction and an expression. The direction can be:

      • rising — The edge expression is rising from strictly below zero to a value equal to or greater than zero.

      • falling — The edge expression is falling from strictly above zero.

      • crossing — The edge expression is either rising or falling past zero.

    • In message events, the trigger follows this format: port which specifies the name of the input message port and represents the arrival of a message.

    • A guard in square brackets on the message label is a MATLAB® Boolean expression which is an additional condition to determine whether the message occurs. Evaluation of the guard only occurs once a valid trigger has been detected. During execution, the sequence diagram waits to proceed to the next message until the guard expression evaluates to true.

    • A constraint in curly brackets on the message label is a MATLAB Boolean expression which specifies an expected value of inputs to the destination lifeline. During execution, the evaluation of the constraint determines whether the sequence diagram shows a pass or fail for that message.

    Click the message and double-click the empty message label that appears. Enter this condition and constraint.

    rising(sw-1){sw==1}

    The message is triggered when the sw signal rises from below 1 to a value of 1 or above. The constraint in curly brackets indicates that if sw is not equal to 1, the message is invalid.

    Note

    Only destination elements are supported for message labels. In this example, switchout is a source element and cannot be included.

    A sequence diagram with a message condition between the source and poller lifelines.

    The signal name sw is valid input data on the port for a Stateflow chart behavior. The poller component with state chart behavior has sw in the Symbols pane.

    State chart behavior with the sw value displayed in the Symbols pane.

    Note

    The signal name can also be a data element on a data interface on a port. Enter Tab to autocomplete the port and data element names. For more information, see Represent System Interaction Using Sequence Diagrams.

    In this example, when the sw signal becomes 1, the pedestrian crossing button has been pressed, and a message to the poller lifeline is recognized.

  5. In addition to signal-based communication, sequence diagrams also support message-based communication, or message events. Create a message by drawing a line from the poller lifeline to the switch lifeline. Start typing switchEvent in the To box until switchEvent is available to select.

    Since a connection exists in the architecture model, a message is created from source port switchEvent.

  6. Click the message and double-click the empty message label that appears. Enter this condition representing the port and guard.

    switchEvent[switchEvent==1]

    Sequence diagram with a message guard and a condition for a message event switch event.

    When the message event switchEvent is received and its value is 1, the message has occurred in the corresponding architecture model and validates the architectural design.

Add Fragments and Operands

A fragment indicates how a group of messages execute or interact.

A fragment is used to model complex sequences, such as alternatives, in a sequence diagram.

An operand is a region in a fragment. Fragments have one or more operands depending on the kind of fragment. Operands can contain messages and additional fragments.

Each operand can include a constraint to specify whether the messages inside the operand execute. You can express the precondition of an operand as a MATLAB Boolean expression using the input signal of a lifeline.

To access the menu of fragments:

  1. Click and drag to select two messages.

  2. Pause on the ellipsis (...) that appears to access the action bar.

    An ellipsis displays in the lower right part of the sequence diagram.

  3. A list of fragments appears:

    For more information, see Author Sequence Diagram Fragments.

    Select the Alt Fragment fragment.

    The Alt Fragment is selected in a list of composite fragments.

  4. The Alt Fragment fragment is added to the sequence diagram with a single operand that contains the selected messages.

  5. Select the fragment to enter an operand condition. Choose a fully qualified name for input data and use a constraint condition relation.

    switch/inhibit==0

    This constraint is a precondition that determines when the operand is active. This constraint specifies that the inhibit flag is set to 0. Thus, pedestrian crossing is allowed at this intersection using a pedestrian lamp.

    The messages inside an operand can only be executed if the constraint condition is true.

  6. Highlight the first operand under the Alt Fragment fragment. In the toolstrip, select Fragment > Add Operand > Insert After. This action adds a second operand.

    Add a constraint condition relation to the second operand. The second operand in an Alt Fragment fragment represents an elseif condition for which the message is executed.

    switch/inhibit==1

    This constraint represents when the inhibit flag is set to 1. Thus, pedestrian crossing is not controlled by a walk signal on that intersection.

    Create a message with a message label inside the second operand.

    The second Alt Fragment operand displays a constraint condition.

    For the first operand in the Alt Fragment fragment, since the inhibit flag is set to 0, the first message to the controller lifeline is recognized and validates the pedRequest message. Then, when the switchPed message value is 1, the lampController component behavior allows pedestrians to cross.

    For the second operand in the Alt Fragment fragment, since the inhibit flag is set to 1, the switch bypasses the controller. The message switchPed with a value of 2 goes directly to the lampcontroller, which does not affect the traffic signal. Pedestrian crossing is not specifically supported in this traffic intersection.

See Also

Tools

Functions

Objects

Related Topics