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\UModel2019\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 , 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.|
|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.|
|6.||Click Next to continue. This dialog box allows you to define the package dependency generation settings.|
|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.|
|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:|
The "Content of OrgChart" diagram is now displayed in the main pane.
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.|
|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.|
|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.|
|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.|
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.
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.|
The messages window displays the syntax checks being performed and status of the synchronization process.
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.
© 2019 Altova GmbH