Altova UModel 2024 Basic Edition

Reverse Engineering (from Code to Model)

Accueil Préc Haut Suivant

This tutorial section illustrates how to import existing program code from a directory into a new UModel project (reverse engineering). You will also add a new class into the model, prepare it for code generation, and then merge changes back into the Java code (forward engineering). Although this tutorial illustrates importing Java code, the process is similar if you would like to import existing C# or VB.NET code.

 

Note: The sample Java code used in this tutorial is available as a ZIP archive at the following path: C:\Users\<username>\Documents\Altova\UModel2024\UModelExamples\OrgChart.zip. Please unzip the archive to the same directory before starting the tutorial.

 

Importing existing code from a directory

1.On the File menu, click New.

2.On the Project menu, click Import Source Directory.

3.Select the language of the source code (in this example, Java).

4.Click the Browse button btn_browse , select the OrgChart directory unzipped previously, and click Next. Notice the Enable diagram generation check box is selected, which instructs UModel to generate Class Diagrams and Package Diagrams from the source code.

um_reverse_01

5.Select the Generate diagram per package option. This instructs UModel to create a new diagram for each package. The diagram styling options can be changed later if necessary.

um_reverse_02

6.Click Next to continue. This dialog box allows you to define the package dependency generation settings.

um_reverse_03

7.Click Finish. When prompted, save the new model to a directory on your system. The data is parsed, and a new package called "OrgChart" is created.

tut-01-04

8.Expand the new package and keep expanding the sub packages until you get to the OrgChart package (com | OrgChart). Double-click the "Content of OrgChart" diagram icon:

um_reverse_05

The "Content of OrgChart" diagram is now displayed in the main pane.

 

um_reverse_06

 

Adding a new class to the OrgChart diagram

At this stage, you have fully reverse engineered some existing Java code and created a model out of it, which also includes several automatically generated diagrams. We will now go one step further, and extend the model to include a new class.

 

1.Right-click inside the "Content of OrgChart" diagram, and then select New | Class from the context menu.

2.Click the header of the new class, and enter CompanyType as the name of the new class.

um_reverse_07

3.Add new operations to the class using the F8 shortcut key. For the purpose of this example, add the following operations: CompanyType(), getCompanyType():String, setCompanyType():String.

um_reverse_08
Note:Since the class name is CompanyType, the operation CompanyType() is automatically assigned the <<constructor>> stereotype.

 

Making the new class available for code generation

Now that the model has been extended with a new class, you will most likely want to update the underlying code accordingly, in order to keep both in sync. However, if you press F11 to check the project syntax at this stage, a warning is displayed in the Messages window: 'CompanyType' has no Component Realization to a Component - ComponentRealization to Component 'OrgChart' will be generated. The reason is that the new class requires realization to a component before code can be generated from it, as explained in Round-Trip Engineering (Model-Code-Model). In some cases (including this example), UModel can generate the required realization automatically; however, you can also define the realization dependency manually, as follows:

 

1.While the CompanyType class is selected in the diagram, locate the property "code file name" in the Properties window and enter "CompanyType.java" as file name.

um_reverse_09

2.Click the new CompanyType class in the Model Tree, drag upwards and drop onto the OrgChart component below the Component View package. A notification appears when the mouse pointer is over a component.

um_reverse_10

This method creates a relation of type "ComponentRealization" between a class and a component. An alternative way to do this is to draw the relation in a component diagram, see Component Diagrams. Expand the Relations item below OrgChart to see the newly created relation.

um_reverse_11

 

Merging program code from a package

In UModel, you can generate code at package level, component level, or for the entire project, see also Synchronizing the Model and Source Code. In this example, we will generate code at component level, as follows:

 

1.In the Model Tree window, locate the OrgChart component in the "Component View".

2.Right-click the OrgChart component, and select Code Engineering | Merge Program code from UModel Component from the context menu.

um_reverse_12

The messages window displays the syntax checks being performed and status of the synchronization process.

um_reverse_13

When the process completes, the new CompanyType.java class has been added to the folder ...\OrgChart\com\OrgChart\.

 

All method bodies and changes to the code will either be commented out or deleted depending on the setting in the "When deleting code" group, in the Synchronization settings dialog box.

 

You have now completed a full round-trip code engineering cycle with UModel.

© 2017-2023 Altova GmbH