23
Visualize with Rational Software Architect Create and transform a UML 2.0 model into code Skill Level: Introductory Eric Long ([email protected]) Software Engineer IBM 21 Mar 2006 Rational Software Architect virtually teems with visual tools to handle a number of development tasks. Of most interest to developers are UML modeling, design patterns, and UML-to-code transformations. After completing this tutorial, you will know how to create a UML model, apply a design pattern to that model, and, finally, transform the abstract model into actual code. Section 1. Before you start Rational® Software Architect provides developers, architects, and analysts visual tools for a wide variety of software development tasks. This tutorial introduces a few of these powerful visual tools: UML modeling, design patterns, and UML transformations. About this tutorial Learn how to create, edit, and analyze UML models using Rational Software Architect. This tutorial demonstrates the following Rational Software Architect capabilities: UML modeling Design patterns Visualize with Rational Software Architect © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 23

Visualize with Rational Software Architect - IBM · PDF fileResources Learn • Visit the developerWorks Rational zone to expand your Rational skills. • "Exposing Design flaws in

  • Upload
    hathuy

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

Visualize with Rational Software ArchitectCreate and transform a UML 2.0 model into code

Skill Level: Introductory

Eric Long ([email protected])Software EngineerIBM

21 Mar 2006

Rational Software Architect virtually teems with visual tools to handle a number ofdevelopment tasks. Of most interest to developers are UML modeling, design patterns,and UML-to-code transformations. After completing this tutorial, you will know how tocreate a UML model, apply a design pattern to that model, and, finally, transform theabstract model into actual code.

Section 1. Before you start

Rational® Software Architect provides developers, architects, and analysts visualtools for a wide variety of software development tasks. This tutorial introduces a fewof these powerful visual tools: UML modeling, design patterns, and UMLtransformations.

About this tutorial

Learn how to create, edit, and analyze UML models using Rational SoftwareArchitect. This tutorial demonstrates the following Rational Software Architectcapabilities:

• UML modeling

• Design patterns

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 23

• UML transformations

This tutorial is intended for software developers, architects, and analysts interestedin learning about Rational Software Architect's visual modeling capabilities. If youwant to learn how to transform high-level UML models into actual code without evenknowing a particular programming language, this tutorial is for you.

Objectives

After completing this tutorial, you will know how to use Rational Software Architect tocreate UML projects and models, apply design patterns to those models (new orexisting), and transform UML models into source code or into a different type ofmodel.

Prerequisites

This tutorial assumes that you have some knowledge of Java™ programming, anddesign patterns. Knowledge of UML models is helpful, but not required.

System requirements

To run the examples in this tutorial, install Rational Software Architect. If you don'thave a copy of Rational Software Architect, download a free trial version. All of theprerequisites for Rational Software Architect are located in the Resources section ofthis tutorial.

Section 2. Create a UML modeling project

To get started, you need to do some initial set up. First, create a UML project andmodel.

Steps

Creating a UML modeling project is very easy:

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 2 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

1. Open Rational Software Architect.

2. Open the Modeling perspective by selecting Open Perspective >Modeling from the Window menu.

3. Make sure all of your open projects are closed.

4. Select New > Project... from the File menu.

5. Ensure the Show All Wizards box is checked.

6. Expand the Modeling folder.

7. Select UML Project.Figure 1. The New Project window

8. Click Next.

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 23

9. Name your project DWorksPatternProject.Figure 2. UML Modeling Project window

10. Click Next.

11. Fill in the Create a UML project wizard as follows:

• Select UML Modeling for the File types.

• Select Blank Model for the Templates.

• Enter PatternModel for the File name.

• Leave the remaining defaults.The wizard should look like this:

Figure 3. Create UML Model window

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 4 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

12. Click Finish.

13. The Model Explorer view contains your UML Project,DWorksDesignPattern and your blank UML model, PatternModel.emx.Figure 4. Current Model Explorer view

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 5 of 23

See, wasn't that easy? Now, on to the more exciting part of the tutorial. Let's buildthe model using RSA's user-friendly visual tools.

Section 3. Model a design pattern

Now it's time to select a design pattern, apply that design pattern to a model,observe all relationships of the pattern, and add attributes and operations to themodel.

Design patterns in RSARational Software Architect comes packaged with support for all ofthe Gang of Four design patterns. You also have the ability toimport and create your own design patterns.

Select a design pattern

1. Select Show View > Other... from the Window menu.

2. Expand the Modeling folder and select Pattern Explorer.Figure 5. Select Pattern Explorer

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 6 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

3. Click OK.

4. In the Pattern Explorer view, expand Design Patterns > Behavioral.

5. Select the Observer pattern.Figure 6. The Observer Design Pattern

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 7 of 23

6. To learn about the pattern, toggle between the Overview and ShortDescription tabs at the bottom of the Pattern Explorer view.

7. In the Pattern Explorer view, drag-and-drop Observer ontoDesignPattern's blank diagram.

8. The editor should contain an instance of the Observer Design Pattern thatlooks like this:Figure 7. Observer Pattern Instance

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 8 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Apply a design pattern

1. In DesignPattern's editor, hover over ConcreteObserver and select Enterargument name/value....Figure 8. Enter argument

2. Name the parameter Shopper, then press Enter. The Observer instancenow has a concrete observer named Shopper. Let's add a parameterusing a different approach.

3. From the Palette (right of diagram), click to expand the Class Diagramsection.Figure 9. Palette

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 9 of 23

4. Select Class and click anywhere in the diagram (do not click on theObserver instance).

5. Name the newly created class Order and press Enter.

6. Drag-and-drop Order onto ConcreteSubject in the Observer instance.Figure 10. Drag-and-drop ConcreteSubject

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 10 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

7. Your diagram should look similar to this (you might not see all of therelationships...you'll fix that in the next section).Figure 11. Observer instance

Observe the design pattern relationships

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 11 of 23

1. Right-click on any element in the DesignPattern diagram.

2. Select Filters > Show Related Elements... from the context menu.Figure 12. Show relationships

3. Select Show All Relationships from the Custom Query list.Figure 13. Show/Hide Relationships

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 12 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 13 of 23

4. Click OK

5. You can see all of the elements and relationships represented in thismodel:Figure 14. Observer pattern

6. Now you are ready to add some elements to your blank Observer designpattern model.

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 14 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Add methods and attributes to a model

1. In the PatternModel editor, right-click the Order concrete subject.

2. Select Add UML > attribute.Figure 15. Add attribute

3. Type -orderNum : String (the "-" makes the attribute private).

4. Press Enter.

5. Repeat the same steps on Shopper, but name the attribute -custID :String.

6. In the PatternModel editor, right-click the Shopper concrete subject.

7. Select Add UML > operation.

8. Type +makeOrder() (the "+" makes the operation public).

9. PatternModel's editor should now look like this:Figure 16. Observer pattern

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 15 of 23

Good work! You have completed your model (a very simple model) and can useRational Software Architect's tools to transform this model into actual code.

Section 4. Transform your model into Java code

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 16 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Now that you have a completed model within your UML project, you are going totransform that model to actual code. Rational Software Architect supports thefollowing model-to-code transformations out of the box:

• UML -> Java

• UML -> EJB

• UML -> XSD

• UML -> CORBA

• UML -> C++

However, if you would like more options, there are built-in tools and wizards that letyou create custom transformations in Rational Software Architect. You are going totransform your existing model into Java.

Transform a UML model to Java code

1. In the Model Explorer view, expand DWorksPatternProject.Figure 17. Expanded project

2. In the Model Explorer view, select both Order and Shopper.

3. Right-click on either of the selected objects and select Transform > RunTransformation > UML - Java.Figure 18. Run transformation

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 17 of 23

4. In the Run this Transformation window, click Create new TargetContainer....Figure 19. Transformation window

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 18 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

5. In the New Java Project window, name the projectDWorksTransformedJava.Figure 20. New Java project

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 19 of 23

6. Click Finish.

7. Now that you are back in the Run this Transformation window, click Run.RSA transforms the model into Java source code. The Model Explorerview should contain this Java project:Figure 21. Transformed Java project

8. Double-click Order.java to see the transformed code. The code shouldlook like this:

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 20 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Figure 22. Transformed Order class

Try running a different transformation from UML-to-C++ and explore the results.

Section 5. Conclusion

Congratulations! In this tutorial, you covered the creation of a UML project andmodel, selecting and applying a design pattern to a model, adding and editingattributes and operations to a model, and transforming that model into actual code.You did all of this without even writing a line of code! It is easy to see (even with thissimple tutorial) how visual UML modeling tools simplify code development. At this

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 21 of 23

point, you might want to explore some of the links in the Resources section of thistutorial.

developerWorks® ibm.com/developerWorks

Visualize with Rational Software ArchitectPage 22 of 23 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Resources

Learn

• Visit the developerWorks Rational zone to expand your Rational skills.

• "Exposing Design flaws in your code: Part 1" (developerWorks, August 2005)details the visual tools available to run internal code reviews in Rational SoftwareArchitect.

• " Visualize with Rational Application Developer" (developerWorks, February2006) details the visualization capabilities of Rational Application Developer.

• "Discover IBM Rational visual tools for application development"(developerWorks, February 2006) covers all of the different visual tools availablein the Rational Software Development Platform.

• Stay current with developerWorks technical events and webcasts.

Get products and technologies

• Download a free trial version of Rational Software Architect.

• Build your next development project with IBM trial software, available fordownload directly from developerWorks.

• Order the SEK for Rational, a two-DVD set containing the latest IBM trial softwarefor Linux from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.

Discuss

• Participate in developerWorks blogs and get involved in the developerWorkscommunity.

About the author

Eric LongEric Long is a Software Engineer in the IBM Developer Skills Program. Eric graduatedfrom The University of Texas with a degree in Computer Science. He joined IBM inJuly of 2004 and currently works in Austin, Texas. As a Software Engineer, heprovides technical information to developers on open source and industry trends andtechnologies through speaking engagements, web content, and faculty consultations atIBM Academic Initiative member universities. His work also includes technical courses,demos, articles, and tutorials available at http://www.ibm.com/university andhttp://www.ibm.com/developerworks.

ibm.com/developerWorks developerWorks®

Visualize with Rational Software Architect© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 23 of 23