Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 12: Introduction to Code Generation and Reverse Engineering Using Rational Rose

One of the most powerful features of Rational Rose is its ability to generate code that represents a model. In this chapter, we'll take a look at the fundamental steps you must take before you can generate code from your Rose model. Then, we'll examine the reverse−engineering process, and see what is reverse engineered into a Rose model.

The code−generation options you have available will vary by the version of Rose you have installed. There are three different versions of Rose currently available:

Rose Modeler allows you to create a model for your system, but will not support code generation or reverse engineering.

Rose Professional allows you to generate code in one language.

Rose Enterprise allows you to generate code in Ada 83, Ada 95, ANSI C++, CORBA, Java, COM, Visual Basic, Visual C++, C++, and XML. It also supports the generation and reverse engineering of databases.

A number of Rose partner companies have developed add−ins to support code generation and reverse engineering in other languages. Check Rational's website, http://www.rational.com/, for information about add−in products available for Rose.

Checking your Rose model

Setting code−generation properties

Generating code using Rational Rose

Preparing for reverse engineering

Reverse engineering code into a Rose model

Preparing for Code Generation

There are six basic steps to generating code:

1.

Check the model.

2.

401

Chapter 12: Introduction to Code Generation and Reverse Engineering Using Rational Rose

Create the components.

3.

Map the classes to the components.

4.

Set the code−generation properties.

5.

Select a class, component, or package.

6.

Generate the code.

Not all of these steps are necessary in each language. For example, you can generate C++ code without first creating components. You can create code in any language without running the Check Model step, although you may have some errors during the code generation. In the following chapters, we'll discuss the details of generating and reverse engineering code in the various languages.

Although not all of these steps are required, we recommend completing the first five steps before generating code. The model check will help to find inconsistencies and problems in your model that you certainly wouldn't want to affect the code. The component steps serve as a way to map your logical system design to its physical implementation, and they provide you with a great deal of useful information. If you skip these steps, Rose will use the package structure in the Logical view to create components.

Step One: Check the Model

Rose includes a language−independent model check feature that you can run to ensure that your model is consistent before you generate code. It's always a good idea to run this check before you attempt to generate code, because it can find inconsistencies and errors in your model that might prevent code from being generated correctly.

To check your Rose model:

1.

Select Tools → Check Model from the menu.

2.

Any errors that are found will be written to the log window.

Common errors include things like messages on a Sequence or Collaboration diagram that are not mapped to an operation, or objects on a Sequence or Collaboration diagram that are not mapped to a class. The rest of this section explores some of the common errors and their solutions.

The message below indicates that you have an object in a Sequence or a Collaboration diagram that has not been mapped to a class.

Unresolved reference from use case "<Use case name>" to ClassItem with name (Unspecified) by object <Object name>>

402

Chapter 12: Introduction to Code Generation and Reverse Engineering Using Rational Rose

First, look at the objects on your Sequence or Collaboration diagram. Each box should contain the object name, followed by a colon, followed by the class name:

Next, find the object without the class name. Right−click the object and select Open Specification from the shortcut menu. In the object specification window, select the object's class using the Class drop−down list box.

The following message lets you know that you have a message on a Sequence or a Collaboration diagram that has not been mapped to an operation.

Unresolved reference to Operation with name <Message name> in message <Message name> between <Class name> and <Class name> in Sequence diagram <Use case name>/<Sequence diagram name>

Right−click the appropriate message on the diagram (the error in the log window will let you know the name of the offending message and its Sequence or Collaboration diagram), and map the message to an operation. If necessary, create a new operation for the message.

Finding Access Violations

The Check Model menu item will find most of the inconsistencies and problems in a model. The Access Violations menu item will find violations that occur when there is a relationship between two classes in different packages but no relationship between the packages themselves. For example, if we have an Order class in an Entities package that has a relationship to an OrderManager class in a Control package, there must be a relationship between the Entities package and the Control package. If there is not, Rose will find an access violation.

To find access violations:

1.

403

Chapter 12: Introduction to Code Generation and Reverse Engineering Using Rational Rose

Select Report → Show Access Violations from the menu.

2.

Rose will display any access violations in the Access Violations window, as shown in Figure 12.1.

Figure 12.1: Access Violations window

Running a Language−Independent Check

To run a language−independent check, choose Tools → Check Model. If you are using Java, you can run a Java−specific syntax check by selecting Tools → Java → Syntax Check. This check will find errors such as multiple public classes in a single compilation unit. If you are using CORBA, run Tools → CORBA → Syntax Check to perform a similar analysis. Select Tools → XMLDTD → Syntax Check for XML.

If any of these checks find errors, they will appear in the Rose log. Once all errors have been corrected, move on to step two.

Step Two: Create Components

The second step in the code−generation process is to create components to hold the classes. There are many types of components: source code files, executable files, runtime libraries, ActiveX components, and applets, to name a few. Before you generate code, you can map each of your classes to the appropriate source code component.

Once the components are created, you can add dependencies between them on a Component diagram. Dependencies between the components are the compilation dependencies in the system. (See Chapter 10, "Component View," for more information about components and component dependencies.)

If you are generating C++, Java, or Visual Basic, you aren't required to complete this step in order to generate code. In Java or Visual Basic, Rose will automatically create the appropriate component for each of your classes.

To create a component:

1.

Open a Component diagram.

2.

404