Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

Figure 12.2: Code−generation menu items

To show or hide these menu options, select the Add−Ins → Add−In Manager menu option. In the Add−In Manager dialog box, as shown in Figure 12.3, use the check boxes to show or hide the options for various languages.

Figure 12.3: Add−In Manager

Once you have a class or component selected on a diagram, select the appropriate code−generation option from the menu. If any errors are encountered as the code generation progresses, these errors will be noted in the log window.

What Gets Generated?

When you generate code, Rose collects information from the Logical view and the Component view of your model. Although no modeling tool can create a completed application for you, Rose will generate a great deal

410

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

of skeletal code. In the following chapters, we'll discuss specifically what is generated for a given language.

Elements generated by the code include the following:

Classes All classes in the model will be generated in the code.

Attributes The code will include the attributes of each class, including the visibility, data type, and default value. In some languages, Get and Set operations can also be automatically generated for the attributes.

Operation signatures The operations will be declared in the code, along with any parameters, the data types of the parameters, and the return type of the operation.

Relationships Some of the relationships in your model will cause attributes to be created when you generate code.

Components Each component will be implemented by the appropriate source code file.

Documentation When documentation has been added, the code generator will insert the documentation at the appropriate place.

Once the files have been generated, there are two steps remaining. First, the developers take the files, and they code each of the operations of the classes. Then, the graphical user interface is designed.

Rose is not intended to be a GUI design tool—you can use your programming language's environment to design the screens and forms. Instead, this approach helps you ensure that the system you're building has a solid design. The team can review the Rose model to reach an agreement on the best architecture and the best design, and then generate code from the model. Rather than having 20 different programmers designing in 20 different directions, everyone is working off of the same blueprint.

When generating code, Rose will use the package structure you've set up in the Component view to create the appropriate directories. By default, the root directory it uses for code generation is the directory with the Rose application file in it. You can change the directory through the code−generation properties for your language.

If you don't have components set up, Rose will use the package structure in the Logical view to set up the directory structure. Again, the default directory is the Rose directory, but you can change it through the code−generation properties.

Introduction to Reverse Engineering Using Rational Rose

Reverse engineering is the ability to take information from source code and create or update a Rose model. Through its integration with C++, Java, Visual Basic, and many other languages, Rose supports the reverse engineering of code into a UML model. One of the challenges with information technology projects is keeping the object model consistent with the code. As requirements change, it can be tempting to change the code directly, rather than changing the model and then generating the changed code from the model. If this happens in Rose, the team can reverse engineer the changes from the code back into the model. Reverse engineering helps us keep the model synchronized with the code.

In the reverse−engineering process, Rose will read components, packages, classes, relationships, attributes,

411

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

and operations from the code. Once this information is in a Rose model, you can make any needed changes to the model and then regenerate the code through the forward−engineering features of Rose.

The options you will have available will depend on the version of Rose you are using:

Rose Modeler will not include any reverse−engineering functionality.

Rose Professional includes reverse−engineering capabilities for one language.

Rose Enterprise includes ANSI C++, Visual C++, C++, CORBA, XML, Visual Basic, Ada, and Java reverse engineering, as well as database generation and reverse engineering.

A new feature of Rose is the ability to reverse engineer a web application.

Rose add−ins will give you reverse−engineering capabilities in other languages, such as PowerBuilder or Forte.

Model Elements Created During Reverse Engineering

During the reverse−engineering process, Rose will collect information about:

Classes

Attributes

Operations

Relationships

Packages

Components

Using this information, Rose will create or update an object model. Depending upon the language you are reverse engineering, you can create a new Rose model or update the current Rose model.

Let's begin by examining classes, attributes, and operations. If you have a source code file that contains a

412

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

class, the reverse−engineering process will create a corresponding class in your Rose model. Each of the attributes and operations of the class will appear as attributes and operations of the new class in the Rose model. Along with the attribute and operation names, Rose pulls in information about their visibility, data types, and default values.

For example, when reverse engineering the following Java class, Rose will produce the model shown in Figure 12.4.

Figure 12.4: Reverse engineering a Java class

//Source file: C:\\Flight.java

public class Flight

{

private int FlightNumber; private long DepartureDate; private int GateNumber;

public Flight()

{

}

/**

@roseuid 3942C4F50253 */

public boolean AddPassenger(int PassengerID)

{

}

/**

@roseuid 3942C4FE02B0 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 3942C5060211

413

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

*/

public boolean SwitchGate(int NewGate)

{

}

}

If you originally created the classes using Rose, and then made some changes to the classes in the code, these changes will be reflected in the model during the reverse−engineering process. For example, if you deleted an operation in the code, the operation will be deleted from the model during reverse engineering. If you added an attribute or operation directly into the code, this new attribute or operation will be added to the model during reverse engineering.

In addition to classes, Rose will collect information about the relationships in the code. If one class contains an attribute whose data type is another class, Rose will create a relationship between the two classes. For example, given the following two Java classes, Rose will create an association relationship between them, as shown in Figure 12.5.

Figure 12.5: Reverse engineering an association relationship

//Source file: C:\\Class_A.java

public class ClassA_

{

public Class_B theClass_B;

public Class_A()

{

}

}

//Source file: C:\\Class_B.java

public class Class_B

{

public Class_B()

{

}

}

Inheritance relationships are also generated in the Rose model. Rose will create generalization relationships to support any inheritance in the code. If you have packages of foundation classes in your model, such as the JDK or PowerBuilder system types, Rose will add generalization relationships between the reverse−engineered classes and the base classes.

The components in the code will also be represented in Rose after the reverse−engineering process. Each language deals with components differently. We'll discuss the reverse engineering of components in the following chapters.

414