Sequence diagram of UML modeling

Source: Internet
Author: User

It is now February, And now you may have read or heard people talk about the changes made in UML 2.0, including the new specifications of several progressive UML. Considering the importance of the new specification, we are also modifying the basis of this article series to focus our attention on the omg uml 1.4 specification, the transfer to OMG has adopted the draft UML 2.0 specification (also known as UML 2 ). I do not like to change the focus from 1.4 to 2.0 in the middle of a series of articles, but the standardization of the UML 2.0 draft is an important step forward, and I feel that I need to expand the text.

For some reason, OMG improved UML. The main reason is that they want UML models to express the model-driven architecture (MDA), which means that UML must support more model-driven symbols. At the same time, the UML 1.x symbol set is sometimes difficult to apply to large applications. In addition, symbol elements need to be improved to make the image easier to read. (For example, the logical process of the model in UML 1.x is too complex and sometimes impossible to complete. Significant progress has been made in serialization logic modeling due to changes in the symbol set of the sequence diagram in UML 2 ).

Note the text I mentioned above: "The uml2.0 draft Specification has been adopted ." Indeed, the specifications are still in the draft State, but the key is that the draft specifications have been adopted by OMG, which is an organization that uses them until the new standards are quite reliable. Before UML 2 is fully adopted, there will be some modifications to the specification, but these changes should be minimal. The major change will be within UML-including the features used by software companies that typically implement UML tools.

The main purpose of this article is to continue to focus on the basic UML diagram. This month, we will learn more about the sequence diagram. Note that the following example is based on the new UML 2 Specification.

Purpose
A Sequence Chart is used to display the interaction between objects in a series of order of interaction. Like a class chart, developers generally think that a sequence chart is only meaningful to them. However, the business personnel of an organization will find that the sequence diagram shows how different Business Objects interact, which is useful for communicating with the current business. In addition to recording the current events of an organization, a business-level sequence chart can be used as a requirement file to meet future system transfer requirements. In the project demand phase, analysts can bring the use cases to the next level by providing a more formal expression. In that case, use cases are often subdivided into one or more sequence diagrams.

The technical staff of the Organization can find that sequence diagrams are very useful in recording how a future system behave. In the design phase, architects and developers can use graphs to explore the interaction between system objects, so as to enrich the entire system design.

One of the main purposes of a sequence chart is to transform the Needs of the use case expression into further and more formal levels of fine expression. Use Cases are often divided into one or more sequence diagrams. In addition to the purpose of designing a new system, sequence diagrams can also be used to record how objects in an existing system (called a legacy) interact with each other. This document is useful when you hand over the system to another person or organization.

Symbol
Since this is the first article in my UML diagram series based on UML 2, we need to first discuss a supplement to the UML 2 diagram symbol, that is, a symbolic component called a framework. In UML 2, frame elements are used as a basis for many other graph elements, but most people first come into contact with frame elements as graphical boundaries of graphs. When a graphic border is provided for a graph, a frame element provides a consistent position for the labels of the graph. In a UML diagram, the framework components are selectable. As you can see in figures 1 and 2, the labels of the diagrams are placed in the upper left corner, in the "namebox" of the framework that I will call, it is a kind of angle rectangle, and the actual UML diagram is defined inside a large closed rectangle.

Figure 1: Empty UML 2 framework component

In addition to providing a graphical border, framework components used in graphs also have important functions to describe interactions, such as sequence diagrams. In a sequence diagram, a sequence receives and sends messages (also known as interactions). A model can be established by connecting the message and the Framework Element Boundary (see figure 2 ). This will be described in more detail in the later section "Beyond basics.

Figure 2: A Sequence Diagram for receiving and sending messages

Note that in Figure 2, for a sequence chart, the label of the chart starts with the text "SD. When you use a frame element to close a graph, the labels of the graph must follow the following format:

Graph type graph name

UML specifications provide specific text values for graph types. (For example, SD represents a sequence diagram, activity represents an activity diagram, and use case represents a use case diagram ).

Basic
The main purpose of a sequence chart is to define the Event Sequence and generate expected output. The focus is not on the message itself, but on the order in which messages are generated. However, most sequence diagrams indicate what messages are transmitted between System Objects and the order in which they occur. The image transmits information in the horizontal and vertical dimensions: the vertical dimension indicates the time sequence of the message/call occurrence from top to bottom, and the horizontal dimension indicates the object instance to which the message is sent from left to right.

Lifeline
When drawing a sequence diagram, place the lifeline symbol component across the top of the diagram. The lifeline indicates the role or object instance that is modeled in the sequence. 1 The lifeline painting is a square, with a dotted line from top to bottom passing through the center of the bottom boundary (Figure 3 ). The lifeline name is placed in the square.

Figure 3: An Example of the student class used for the lifeline of an object named freshman

The lifeline Naming Standard of UML follows the following format:

Entity name: Class Name

In the example 3, the lifeline represents the entity of the class student, and its object name is freshman. Note that the lifeline name is underlined. When an underline is used, it means that the lifeline in a sequence diagram represents a specific entity of a class, not a specific entity of a specific type (for example, a role ). In a future article, we will learn about structured modeling. Now, only comment on the sequence diagram, which may contain roles (for exampleBuyerAndSellerInstead of describing who plays those roles (for exampleBillAndFred). This allows repeated use of graphs in different contexts. Simple drag-and-drop: the Instance name of the sequence diagram is underlined, but the role name is not.

In Figure 3, the lifeline example is a named object, but not all lifeline objects represent named objects. On the contrary, a lifeline can be used to represent an anonymous or unnamed entity. When a sequence chart is modeled for an unnamed instance, the name of the lifeline follows the same pattern as that of a named instance. However, the location of the lifeline name remains blank, rather than providing an example chart name. Refer to Figure 3 again. If the lifeline is representing an anonymous example of the student class, the lifeline will be "student ". At the same time, because sequence diagrams are used in the project design phase, an unspecified object is completely legal: for example, "Freshman ".

Message
For readability, the first message in a sequence diagram always starts from the top and is usually located on the left of the diagram. Then the secondary message is added to the graph, which is slightly lower than the previous message.

To display an object (for example, a lifeline) and send a message to another object, you draw a line pointing to the receiving object, including a solid arrow (for a synchronous call operation) or a stick arrow (if it is an asynchronous signal ). The message/method name is placed on the line with an arrow. The message being passed to the receiving object, indicating an operation/method implemented by the class of the receiving object. In the example in figure 4, the analyst object calls the system object of an instance of the reportingsystem class. The analyst object calls the getavailablereports method of the system object. The system object then calls the getsecurityclearance method on the secsystem object, including the userid parameter. The secsystem class type is securitysystem. 2

Figure 4: an instance that transmits messages between objects

In addition to displaying only the message calls in the sequence diagram, the diagram in Figure 4 also contains the returned message. These return messages are optional. A return message is drawn with a dotted line with an open arrow pointing back to the lifeline of the source. On this dotted line, you place the return value of the operation. In Figure 4, when the getsecurityclearance method is called, The secsystem object returns userclearance to the system object. When the getavailablereports method is called, The system object returns availablereports.

In addition, the returned message is an optional part of the sequence diagram. Returns the specific/abstract degree of dependency modeling for messages. It is useful to return a message if it needs to be more specific; otherwise, the active message is enough. I personally like to include a returned message whenever a value is returned, because I found that additional details make it easier to read a sequence diagram.

When modeling a sequence chart, sometimes an object needs to pass a message to itself. When is an object called itself? A purist will argue that an object should never pass a message to itself. However, it may be useful in some situations to model an object that transmits a message to itself. For example, figure 5 is an improved version of Figure 4. Figure 5 shows the system object that calls its determineavailablereports method. The system transmits the message "determineavailablereports" to itself. The model focuses on the fact of the process rather than the system object.

To draw a call object, such as what you do at ordinary times, draw a message, but not connect it to another object, but connect the message to the object itself.

Figure 5: The system object calls its determineavailablereports Method

The message instance in Figure 5 shows synchronous messages. However, you can also model asynchronous messages in the sequence diagram. An asynchronous message is similar to a synchronous method, but the line drawn by the message carries a single stick, as shown in figure 6.

Figure 6: sequence diagram fragment of asynchronous messages passed to entity 2

Constraints
When modeling the interaction of an object, messages are transmitted to the object only when a condition is met. Constraints are used in various UML diagrams for control flow. Here, I will discuss the constraints of UML 1.x and UML 2.0. In UML 1.x, a constraint can only be allocated to a single message. In UML 1. X, to draw a constraint on a sequence diagram, you place the constraint element on the constrained message line before the message name. Figure 7 shows a part of the sequence diagram, which has a constraint on the message addstudent method.

Figure 7: A segment in the UML 1.x sequence diagram, where the addstudent message has a constraint

In Figure 7, the constraint is the text "[pastduebalance = 0]". With the constraints on this message, if the accounting system returns an overdue balance of zero, the addstudent message will be passed. The constraint symbol is simple. The format is:

[Boolean Test]

For example,

[pastDueBalance = 0]

Combined fragments (variant scheme, selection items, and loops)
However, in most sequence diagrams, the UML 1.x "in-line" constraint is insufficient to process the required logic of a modeling sequence. This missing feature is a problem with UML 1.x. UML 2 has solved this problem by removing the "in-line" constraint and adding a symbolic component called a combination fragment. A combined Shard is used to combine a set of messages and display the condition branches in a sequence diagram. The UML 2 Specification specifies 11 types of interaction for combined fragments. Three of the eleven types will be introduced in the "Basic" section, and the other two types will be introduced in the "beyond basic" section, the remaining six types will be introduced in another article. (Hi, this is an article, not a book. I hope you can read this part in one day !)

Variant
A variant is used to specify the mutex selection between two or more message sequences. 3 variants support the classical "if then else" logic modeling (for example,IfI bought three,ThenI got a 20% discount on my purchase;OtherwiseI get a 10% discount for my purchase ).

As you will notice in figure 8, the combined fragment component of a variant is drawn using a frame. The word "Alt" is placed in the namebox of the framework. Then the larger rectangle is divided into the operation elements referred to as UML 2. 4. The operating elements are separated by dotted lines. Each operator element has a constraint for testing, which is placed on the left upper part of the operator element at the top of the lifeline. 5 if the constraint of the Operation element is "true", then the operation element is the operation element to be executed.

Figure 8: A Sequence diagram fragment that contains a variant combination Fragment

Figure 8 shows how the sequence starts from the top, that is, the bank object gets the Check amount and account balance. In this case, the variant combinations in the sequence diagram take over. Because of the constraint "[balance> = amount]", if the balance exceeds or is equal to the amount, the Bank object will be delivered with the adddebittransaction and storephotoofcheck messages to the account object in sequence. However, if the balance does not exceed or equal to the amount, the sequential process is that the bank sends the addinsuffientfundfee and notereturnedcheck messages to the account object, and the returncheck message to itself. Because of the "else" constraint, when the balance is not greater than or equal to the amount, the second sequence is called. The "else" constraint is not required in the combination fragment of variants. If an operator element does not have a definite constraint on it, the "else" constraint is assumed.

The combination fragments of variants are not limited to simple "if then else" verification. A large number of variant paths may be required. If you need more variants, all you have to do is add an operator element to a rectangle with sequence constraints and messages.

Select item
Select item Combination fragments for sequence modeling. These sequences give a specific condition that will happen; or, the sequence does not happen. A selection item is used to model a simple "If then" expression. (For example, if there are fewer than five circles on the shelf, make two more circles ).

The selected item Combination fragment symbol is similar to the variant combination fragment, except that it has only one operating element and never has an "else" constraint (it does so and has no reason ). You need to draw a selection item Combination and draw a frame. The text "Opt" is the text that is placed in the namebox of the framework. In the content area of the framework, the constraints of the selection items are placed in the upper left corner of the top corner of the lifeline. Then, the message sequence of the selected item is placed in the rest of the content area of the framework. These elements 9 are shown.

Figure 9: A Sequence diagram segment that includes the selected item Combination Fragment

It is easy to read the selected item Combination fragments. Figure 9 is the re-processing of the sequence diagram fragment in Figure 7. However, this time it uses a combination fragment of a selection item, because if student's overdue balance is equal to 0, more messages need to be transmitted. According to the sequence diagram in Figure 9, if the overdue balance of student is equal to zero, and the overdue balance of addstudent, getcostofclass, and chargeforclass messages h between them is not equal to zero, in the selected item Combination fragment, the sequence does not transmit any message.

In Example 9, a sequence diagram segment contains a selection constraint. However, a constraint is not a required component. In a high-level and abstract sequence chart, you may not want to describe the conditions for selecting items. You may just want to point out that the clip is selectable.

Loop
Sometimes you will need to model a duplicate sequence. In UML 2, modeling for a recurring sequence has been improved, with a circular combination fragment attached.

The circular composite fragment surface is very similar to the selected item composite fragment. You can draw a framework and place the text "loop" in the namebox of the framework ". In the content area of the framework, at the top of a lifeline, circular constraint 6 is placed in the upper left corner. Then, the cyclic message sequence is placed in the rest of the framework content area. In a loop, apart from standard Boolean tests, one constraint can test two specific conditional statements. The specific constraint condition is the minimum number of cycles for writing "MININT = [The number]" (for example, "MININT = 1, and the maximum number of cycles for writing "maxint = [The number]" (for example, "maxint = 5. Through the minimum cycle test, the cycle must run at least a specified number of times, and the number of cycles cannot reach the maximum number of cycles specified by the constraint.

Figure 10: A Sequence Chart example of cyclic composite fragments (click to enlarge)

The cyclic Operation shown in Figure 10 is performed until the hasanotherreport message of the reportsenu object returns false. If the loop sequence should run, the loop of this sequence diagram uses a Boolean test to confirm. To read this picture, you start from the top, just as you normally do. When you reach the combined parts of the loop, perform a test to check whether the value of hasanotherreport is true. If the hasanotherreport value is equal to true, the sequence enters the loop segment. Then, you can track cyclic messages in a sequence diagram, just like normal.

Beyond basic

I have already introduced the basics of sequence diagrams, which should allow you to model most of the interactions that will normally occur in the system. The following section describes the higher-order symbolic elements used for sequence diagrams.

Reference another sequence diagram
When using sequence diagrams, developers love to reuse existing sequence diagrams in their sequence diagrams. 7. Introduce "interactive" components in UML 2. Append interaction is the most important innovation in UML 2 interaction modeling. The original sequence diagram is organized into a complex sequence diagram. Because of these, you can combine (reuse) Simpler sequences to generate more complex sequences. This means that you can abstract a complete, possibly complex sequence into a single conceptual unit.

An interactive component is drawn using a framework. The text "Ref" is placed in the namebox of the framework, and the referenced sequence diagram name is placed in the content area of the framework, together with any parameters of the sequence diagram. The following schema references the name symbols of a sequence chart:

Sequence diagram name [(parameter)] [: return value]

Two examples:

1. Retrieve Borrower Credit Report(ssn) : borrowerCreditReport

Or

2. Process Credit Card(name, number, expirationDate, amount : 100)

In example 1, the syntax calls a sequence diagram called retrieve borrower credit report and passes it to the SSN parameter. The returned variable borrowercreditreport is returned for the Series retreive borrower credit report.

In instance 2, the syntax calls the sequence diagram called process credit card and passes it to the parameter name, number, expiration date, and amount. However, in example 2, the amount parameter will be 100. Because Example 2 does not have a return value label, and the sequence does not return values (assuming that the modeling sequence does not need to return values ).

Figure 11: A Sequence diagram referencing two different sequence diagrams

Figure 11 shows a sequence diagram that references the sequence diagrams "balance lookup" and "debit account ". The sequence starts from the upper left corner and the customer passes a message to the teller object. The teller object transmits a message to the theirbank object. At that time, the balance lookup sequence diagram was called, and the accountnumber was passed as a parameter. The balance lookup Sequence Chart returns the balance variable. Then, check the constraints of the selected item Combination fragment and confirm that the balance is greater than the amount variable. When the balance is greater than the amount, call the debit account sequence diagram and pass it the accountnumber and amount parameters. After that sequence is complete, the withdrawcash message returns cash to the customer.

It is important to note that in Figure 11, theirbank's lifeline is hidden by interactive balance lookup. Because the lifeline of interaction is hidden, it means theirbank lifeline is referenced in the "balance lookup" sequence diagram. Apart from hiding the lifeline of interaction, UML 2 also specifies that the lifeline must have the same theirbank in its own "balance lookup" sequence.

Sometimes, you model a sequence chart, where interactions overlap.NoThe lifeline referenced in the interaction. In that case, the lifeline is displayed like a normal lifeline and will not be hidden by overlapping interactions.

In Figure 11, the sequence references the "balance lookup" sequence diagram. The "balance lookup" sequence diagram is shown in Figure 12. Because the sample sequence has a parameter and a return value, its label -- located in the namebox of the graph -- follows a specific pattern:

Graph type graph name [parameter type: parameter name]
[: Return Value Type]

Two examples:

1. SD Balance Lookup(Integer : accountNumber) : Real

Or

2. SD Available Reports(Financial Analyst : analyst) : Reports

Figure 12 illustrates Example 1. In this example, the balance lookup sequence uses the accountnumber parameter as the variable in the sequence, and the sequence diagram shows the returned real object. In this case, the returned object uses the sequence diagram entity name.

Figure 12: A sequence chart that uses the accountnumber parameter and returns a real object

Figure 13 illustrates Example 2. In a sequence diagram, a parameter is obtained and an object is returned. However, parameters in Figure 13 are used in sequence interaction.

Figure 13: a sequence chart that uses parameters in its interaction and returns a Reports object

Portal
The previous section shows how to pass information through parameters and return values and reference another sequence diagram. However, there is another way to transmit messages between sequence diagrams. The portal may be an easy way to model a message passing between a sequence diagram and its context. A portal is just a message, and the image is represented as the frame edge connecting one end to the sequence diagram, and the other end to the lifeline. You can see the refactoring in figures 14 and 15 using the steps 11 and 12 of the door. In the example in Figure 15, a portal called getbalance obtains the accountnumber parameter. Because the arrow line is connected to the graph frame, and the arrow is connected to the lifeline, The getbalance message is an entry door. A sequence chart also has an exit gate and returns the balance variable. The same can be seen at the exit because it is a returned message that connects the frame from a lifeline to the graph and connects the arrow to the frame.

Figure 14: rebuild in Figure 11.

Figure 15: rebuild in Figure 12.

Combined fragments (jump and parallel)
As described in the previous section "Basic", I introduced the combined fragments of "variants", "selection items", and "loops. Most people will use the most of these three combinations. However, there are two other combinations of fragments, a large number of people will find useful-jump and parallel.

Jump
The hop combination fragment is consistent with the selected item Combination fragment in almost every aspect, except for two exceptions. First, the text "break" in the namebox of the jump framework replaces "option ". Second, when a message that jumps to the combined Shard is running, other messages with closed interaction will not be executed because the sequence breaks the closed interaction. In this way, the jump composite fragment is very similar to the break keyword in the C ++ or Java programming language.

Figure 16: reconstruction of the sequence diagram segment from figure 8, where the segment uses jump instead of Variant

Redirection is most commonly used for model exception handling. Figure 16 is a reconstruction of figure 8, but this time figure 16 uses a jump combination fragment because it treats the balance <amount situation as an exception rather than a variant stream. To read Figure 16, read down from the upper left corner of the sequence. When the sequence reaches the return value "balance", it checks to see if there is less balance than the amount. If the balance is not less than the amount, the next message to be passed is the adddebittransaction message, and the sequence continues normally. However, when the balance is less than the amount, and then the sequence goes into the jump combination fragment, its message is transmitted. Once the messages in the jump combination have been transmitted, the sequence will exit without sending any other messages (for example, adddebittransaction ).

Note that one important thing about redirection is that they only cause a closed interaction sequence to exit and do not need to complete the sequence described in the figure. In this case, the jump combination is a variant or part of a loop, and then only the variant or loop is exited.

Parallel
Today's modern computer systems are constantly evolving in complexity and sometimes concurrent tasks. When it takes longer to complete a complex task than expected, some systems adopt parallel processing of various parts of the process. When you create a sequence chart to display parallel processing activities, you need to use parallel combination of fragment components.

Parallel combination fragments are drawn using a framework. You put the text "par" in the namebox of the framework. Then you divide the content section of the Framework into horizontal operation elements by dotted lines. Each operating element of the framework represents a thread running in parallel.

Figure 17: oven is an object instance that performs two tasks in parallel.

Figure 17 may not provide an example of the best computer system instance for objects doing parallel activities, but provides an easy-to-understand example of a series of parallel activities. The sequence is as follows: hungryperson transmits the cookfood message to the oven object. When the oven object receives the message, it sends two messages (nukefood and rotatefood) to it at the same time. After these messages are processed, the hungryperson object returns yummyfood from the oven object.

Summary
A sequence diagram is a good diagram used to record system requirements and organize system design. The reason why a sequence chart is so useful is that it displays the interaction logic between objects in the system according to the time sequence of interaction.

Reference

  • UML 2.0 superstructure final adopted specification (Chapter 1) http://www.omg.org/cgi-bin/doc? PTC/2003-08-02
  • UML 2 sequence dimo-overview http://www.agilemodeling.com/artifacts/sequenceDiagram.htm
  • UML 2 tutorial http://www.omg.org/news/meetings/workshops/UML%202003%20Manual/Tutorial7-Hogg.pdf

Footer
1. In a fully modeled system, the object (class instance) will also be modeled in the class diagram of the system.

2. When reading this sequence diagram, assume that the analyst logs on to the system.

3. Note that two or more constraints and conditions attached to different variant operators may be true at the same time, however, only one operating element will actually occur at most during running (in that case, the variant "wins" is not defined according to the UML standard ).

4 although the operator yuan looks like a path on a road, I don't call them a path. A swimming track is a UML symbol used in an activity diagram. SeeThe rational edgeEarly articles on activity diagrams.

5. Generally, the lifeline of the constraint attached is the lifeline that owns the variables contained in the constraint expression.

6. For the selected item Combination fragment, you do not need to place a constraint on the cyclic combination fragment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.