Generating Source Code from Sequence Diagrams

This section demonstrates how to draw and edit a sequence diagram that generates source code. To generate source code from a sequence diagram, you will:

  1. Create a project and class

  2. Generate a sequence diagram from a class

  3. Create source-generating elements on the sequence diagram

  4. Create messages on the sequence diagram

  5. Generate implementation code

Creating a project and class

To create a project and class, follow these steps:

1. Create a Together Java Modeling Project. In the New Project dialog, enter a project name, and click Finish. The Diagram view opens displaying the default package diagram.

2. Create a new class on the diagram, and name it MyApplication.

3. Right click on the MyApplication class, and select New > Operation from the context menu. The in-place editor activates.

4. Create a main class method by entering the following code in the in-place editor:

main(args: String[]):void

5. Press Enter. The new main() method is created.

 

Generating a sequence diagram from a class

To generate a sequence diagram from the main method, perform these steps:

1. Right click on the main method, and select Generate Sequence Diagram from the context menu. The Generate Sequence Diagram wizard displays.

2. Click Next, accepting the default settings for the first page of the wizard.

3. Click Finish, accepting the default settings for the second page of the wizard. The sequence diagram, MyApplication.main[1], opens in a new diagram tab of the Diagram view.

Creating source-generating elements on the sequence diagram

To create source-generating elements on the sequence diagram, follow these steps:

1. Right click on the activation rectangle of message #1, and select New > For Block from the context menu.

2. The in-place editor activates. Using the in-place editor, enter: int i = 0; i < 4; i++

The label displays on the activation bar as: for(int i = 0; i < 4; i++)

3. Add an object to the sequence diagram. Click the Object button on the diagram toolbar, and click on the diagram to create a new object. Name the object, frame.

4. Right click on the frame object, and choose Select Class > More from the context menu. The Select Class dialog displays.

5. In the Model elements list, expand the following nodes: libraries > javax > swing. Select JFrame from the list, and click Add >>.

6. Click OK to close the dialog. The name of the selected class displays on the frame object.

 

Creating messages on the sequence diagram

To create messages on the sequence diagram, complete these steps:

1. Draw a message link from the statement block, for(int i = 0; i < 4; i++), to the lifeline of the frame object. Message #1.1 is created.

NB: All of the messages created in the following steps should also have same source and destination.

2. Right click on message #1.1, and select Type > Creation from the context menu. The message changes visually to the creation type. Note that the message link now points to the frame object, which means that the object is being created.

3. Draw a new message. Its label is message #1.2. Right click on the message link, and choose Select Operation > More from the context menu. The Select Operation dialog displays. Expand the JFrame node, scroll through the list, and select setDefaultCloseOperation(int):void. Click OK.

The message label becomes: 1.2:setDefaultCloseOperation(int operation):void

4. Double click the message 1.2 label to invoke the in-place editor, and enter (JFrame.EXIT_ON_CLOSE) after the closing curly brace as shown here: . Press Enter to close the in-place editor and save your changes.

The message label becomes: 1.2: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE):void

5. Draw message #1.3. Similar to Step 3, use the Select Operation dialog (Select Operation > More), expand the Component node, and select the setSize(int,int):void method. Invoke the in-place editor for the message, and enter (600,400) after the closing curly brace as shown here: .

The message label becomes: 1.3: setSize(600,400):void

6. Draw message #1.4. Use the Select Operation dialog, expand the Component node, and select the setLocation(int,int):void method. Invoke the in-place editor for the message, and enter (50*i,50*i) after the closing curly brace as shown here: .

The message label becomes: 1.4: setLocation(50*i,50*i):void

7. Draw message #1.5, Using the Select Operation dialog, expand the Window node, and select the show() method from the list.

The message becomes: 1.5:show():void

Generating implementation code

To generate implementation code for the sequence diagram, follow these steps:

1. Right click on the background of the sequence diagram, and select Generate Implementation from the context menu. The first page of the Sequence diagram refactoring wizard displays. Warning messages display, if applicable. Click Next to display the second page of the wizard.

2. The second page of the wizard displays changes that are necessary to perform refactoring. Notice the sections on the page highlighting the original source code to be replaced and the refactored source code to replace it. Click Finish.

The Editor displays the implementation code. Message labels, for which implementation code has been generated, display in bold on the diagram. If code generation fails for certain messages, those messages do not display in bold.

3. On the sequence diagram, double click one of the message links displayed in bold, and observe that the Editor scrolls to the point of appropriate method invocation.

4. In the Editor, add an import statement for javax.swing. Double click on the first occurrence of JFrame in the code. Right click on JFrame, and select Source > Add Import from the context menu.