Visualize with Rational Application Developer

A tour of Rational Application Developer's visual diagramming tools

Rational Application Developer provides visual diagramming tools to help you gain insight and knowledge of existing applications or to more easily create and understand new applications. After completing this tutorial, you will know how to create class, sequence, browse, and topic diagrams.

Eric Long (elong@us.ibm.com), Software Engineer, IBM

Author photoEric Long is a Software Engineer in the IBM Developer Skills Program. Eric graduated from The University of Texas with a degree in Computer Science. He joined IBM in July of 2004 and currently works in Austin, Texas. As a Software Engineer, he provides technical information to developers on open source and industry trends and technologies through speaking engagements, Web content, and faculty consultations at IBM Academic Initiative member universities. His work also includes technical demos and content available at www.ibm.com/university.



07 February 2006

Also available in Russian

Before you start

Rational Application Developer provides diagrams which enable you to view code from different perspectives using UML 2.0 notation. These diagrams are referred to as UML visualization, not to be confused with UML Modeling. All UML visualizations in Rational Application Developer are synchronized with a source file. Modifications to the source file will dynamically change the diagram and vice versa. To create UML 2.0 models, you need to use Rational Software Modeler or Rational Software Architect. Stay tuned to developerworks for future tutorials covering the visual capabilities of both of those products.

About this tutorial

This tutorial introduces you to the process of creating UML and non-UML diagrams using Rational Application Developer. This tutorial demonstrates the following Rational Application Developer diagrams:

  • Class diagrams
  • Sequence diagrams
  • Browse diagrams
  • Topic diagrams

Note:Rational Application Developer can be used to diagram more than just Java classes in class diagrams. Database components can also be diagramed. However, this tutorial does not cover that. For more information on diagramming database components, see the Resources section.

This tutorial is written for developers who are interested in learning about Rational Application Developer's visualization capabilities.

Objectives

After completing this tutorial, you will know how to use Rational Application Developer to create class, sequence, browse, and topic diagrams.

Prerequisites

This tutorials assumes that you have a solid understanding of Java programming. Knowledge of UML 2.0 diagrams is helpful, but not required.

System requirements

To run the examples as demonstrated in this tutorial, you need to have Rational Application Developer installed on your machine.

If you don't already have a copy of Rational Application Developer, you can download a free trial version. All of the prerequisites for Rational Application Developer are located in the Resources section of this tutorial.


Create a Java project and package

To get started with this tutorial, you need to do some initial set up. First, create a Java project to hold all of the different elements you will be creating (i.e. packages, Java classes, diagrams, etc.).

Create a Java project

Creating a Java project is very easy:

  1. Open Rational Application Developer.
  2. To open the Java perspective, from the Window menu, select Open Perspective > Java.
  3. Make sure all of your open projects are closed.
  4. From the File menu, select New > Project.
  5. Make sure the Show All Wizards box is checked.
  6. Expand the Java folder.
  7. Select Java Project.
    Figure 1. The New Project window
    New Project window
  8. Fill in the Create a Java project wizard as follows:
    • Project Name: IBMPayroll
    • Location: Create project in workspace.
    • Project layout: Use project folder as root for sources and class files.
    The wizard should look like this:
    Figure 2. The New Java Project window
    New Java Project window
  9. Click Finish.

Now, create a package to house your Java classes.

Create a package

  1. In the Package Explorer view, right-click the newly created IBMPayroll project.
  2. Select New > Package.
    Figure 3. New Package
    New Package
  3. In the New Java Package window, name the package com.ibm.common.
  4. Click Finish.
  5. The Package Explorer should now look like this:
    Figure 4. Package Explorer view
    com.ibm.common

Now on to the more exciting part of the tutorial. Let's move on to UML class diagrams.


Create and edit a UML class diagram

This section of the tutorial covers the creation and population of a UML class diagram.

Create a Java class diagram

  1. In the Package Explorer view, right-click the IBMPayroll project.
  2. Select New > Class Diagram.
  3. Fill in the Create Class Diagram window as follows:
    • Enter or select IBMPayroll/com/ibm/common as the parent folder.
    • Name the file IBMPayrollDiagram.
    The window should look like this:
    Figure 5. New Class Diagram window
    New Class Diagram dialog
  4. Click Finish.
  5. From the Palette palette (right side of editor), click the Class button, then click anywhere in the class diagram editor.
  6. Fill in the New Java Class window as follows:
    • Source Folder: IBMPayroll.
    • Package: com.ibm.common.
    • Name: Zipcode.
    • Make sure the bottom three check boxes are checked.
    The New Java Class window should look like this:
    Figure 6. New Java Class window
    New Java Class dialog
  7. Click Finish. The diagram editor should now show a UML visualization of a Java class. Note that this is NOT a UML 2.0 model. It is a representation of the Java code using UML 2.0 notation.
    Figure 7. Zipcode UML representation
    Zipcode
  8. Create two additional classes in the com.ibm.common package: PhoneNumber and Employee.
  9. From the Palette view, click the Association button association.
  10. Drag-and-drop from the Employee class to the Zipcode class.
  11. Fill in the Create Association window as follows:
    • Name the association zipcode.
    • Leave the remaining options as default.
    • Click Finish.
      Figure 8. Create Association window
      Association window
  12. Repeat the same steps for an association between Employee and PhoneNumber.
  13. Name the association phoneNumber.
  14. Arrange the classes in the diagram editor to look like this:
    Figure 9. Class diagram after adding three classes
    Bare diagram
  15. Double-click any of the newly created Java classes in the diagram editor to look at its source file.
  16. Examine the automatically generated source files.

Edit a Java class diagram

In this section, the Zipcode and PhoneNumber classes are edited using Rational Application Developer visual tools. Add a field to the PhoneNumber class and a field and method to the Zipcode class.

  1. In the Package Explorer view, double-click IBMPayrollDiagram.dnx in the IBMPayroll project.
  2. In the editor, right-click the PhoneNumber class.
  3. Select Add Java > Field....
    Figure 10. Add Java Field
    Add Java Field
  4. Fill in the Create Java Field window as follows:
    • Name: theNumber
    • Type: java.lang.String
    • Visibility: private
    The window should look like this:
    Figure 11. Create Java Field window
    Java Field window
  5. Click Finish.
  6. Repeat the same steps to add a field to the Zipcode class.
  7. Fill in the Create Java Field window as follows:
    • Name: theCode
    • Type: java.lang.String
    • Visibility: private
    • Click Finish.
  8. In the editor, right-click the Zipcode class.
  9. Select Add Java > Method....
    Figure 12. Add Java method
    Add Java method
  10. Fill in the Create a Java Method window as follows:
    • Name: isZipcode
    • Type: boolean
    • Visibility: public
    • In the Parameters section, click Add.
    • Name the Parameter code and the type java.lang.String.
      Figure 13. Create parameter
      Create parameter
  11. Click OK.
  12. The Create Java Method window should look like this:
    Figure 14. Create Java Method window
    Java Method dialog
  13. Click Finish. In the Package Explorer view you should see an error in the Zipcode class now, that's OK.
  14. Double-click the Zipcode class in the diagram editor to open the source file.
  15. Replace the isZipcode() method with the following code:
    Listing 1. isZipcode()
    public boolean isZipcode(String code) {
    	boolean isValid = true;
    	try {
    	  int length = code.length();
    	  if ((length != 5) && (length != 10)) {
    		 isValid = false;
    		}
    	for (int i = 0; i < length; i++) {
    		 char c = code.charAt(i);
    		 if ((length == 10) && (i == 5) &&) (c != '-')) {
    			isValid = false;
    		 } else if ((c < '0') || (c > '9')) {
    			isValid = false;
    		 }
    	  }
       } catch (Exception e) {
    	  System.out.println("Error in isZipcode()" + e);
       } finally {
    	  if (isValid == false)
    		 return false;
       }
       return isValid;
    }
  16. Be sure to save all of your projects now (CTRL + SHIFT + S) or File > Save All. Notice the error is now gone.
  17. In the Package Explorer view, right-click the Employee class.
  18. Select Source > Generate Getters and Setters...
    Figure 15. Generate Getters and Setters
    Generate Getters and Setters
  19. In the Generate Getters and Setters window, check phoneNumber and zipcode to generate both getters and setters.
    Figure 16. Generate Getters and Setters window
    Generate Getters and Setters window
  20. Repeat the same steps for the Zipcode class and the PhoneNumber class.
  21. Again, save all the new changes (CTRL + SHIFT + S) or File > Save All.
  22. The diagram editor should now look like this:
    Figure 17. Final class diagram
    Final class diagram

Visualize a method using a sequence diagram

Now that a UML class diagram has been created, you are going to utilize another visualization capability that Rational Application Developer offers: sequence diagrams. A sequence diagram shows the sequence (in time) of messages, which are exchanged among roles that implement the behavior of the system. This section of the tutorial guides you through the process of creating a sequence diagram using Rational Application Developer.

Create a sequence diagram

  1. From the Package Explorer view, expand the Zipcode class.
    Figure 18. Expanded Zipcode class
    Zipcode
  2. Right-click the isZipcode(String) method and select Visualize > Add to New Diagram File > Static Method Sequence Diagram.
    Figure 19. Select sequence diagram
    Select sequence diagram
  3. The diagram is automatically created and opened (default name is sequencediagram.tpx).
  4. Explore the sequence diagram.
    Figure 20. Sequence diagram
    Sequence diagram
  5. For quick navigation around large diagrams, the Outline view allows you to quickly move the viewable area of the editor (this applies to all diagrams).
    Figure 21. Diagram outline view
    Outline view

Create a topic diagram from existing code

A topic diagram is a non-editable diagram that provides a quick way to show relationships between elements. They are useful to define a query and to display and persist the results from that query. An example would be creating a hierarchy topic diagram that you can update to always show the hierarchy for a specific class. This section of the tutorial shows you how to use Rational Application Developer to create a topic diagram from existing code.

Create a topic diagram

  1. From the Package Explorer view, right-click the Employee class.
    Figure 22. Employee class
    Employee class
  2. Select Visualize > Add New Diagram File > Topic Diagram...
    Figure 23. Select new topic diagram
    Topic diagram
  3. Fill in the Topic Wizard window as follows:
    • Parent Folder: IBMPayroll/com/ibm/common
    • File name: EmployeeTopicDiagram
  4. The wizard should now look like this:
    Figure 24. Topic Wizard
    Topic Wizard
  5. Click Next.
  6. Select Java Field Types as the topic.
    Figure 25. Topic Wizard
    Topic Wizard
  7. Use the defaults for the remaining pages, however feel free to explore the options.
  8. Click Finish.
  9. The finished browse diagram looks like this:
    Figure 26. Browse diagram
    Browse diagram

Create a browse diagram

A browse diagram is a temporary, non-editable diagram that shows the results of a query on a context diagram element in a model. Use browse diagrams to navigate through a model to view the details of its elements and relationships. For example, create a browse diagram to show a dynamic view of a class and its related elements to understand how it fits into the model.

Create a browse diagram

  1. From the Package Explorer view, right-click the Employee class.
    Figure 27. Employee class
    Employee class
  2. Select Visualize > Explore in Browse Diagram...
    Figure 28. Select Explore in Browse Diagram
    Browse diagram
  3. The diagram is automatically created.
  4. Click all of the buttons in the editor to explore all relationships to the Employee class.
    Figure 29. Select all Relationships
    Browse buttons
  5. Click Apply.
  6. The final diagram looks like this:
    Figure 30. Final browse diagram
    Final browse diagram

Conclusion

Congratulations! In this tutorial, you covered the creation of a class, sequence, browse, and topic diagram to create and visualize a single Java class. Visual diagramming is far more useful when trying to understand and edit larger applications. With the knowledge you've gained from this tutorial, you can easily move on and apply the same skills to complex applications using Rational Application Developer.

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Rational software on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational, DevOps
ArticleID=102687
ArticleTitle=Visualize with Rational Application Developer
publish-date=02072006