Altova UModel 2024 Enterprise Edition

This example shows you how to generate C++ code with UModel. You will first create a simple UModel project, configure it for code generation, and then generate the actual code.


Create a new UModel project and its structure

On the File menu, click New. This creates an empty project with two default packages ("Root" and "Component View"). Next, right-click the "Root" package, and create a few more packages, as illustrated below. (If you are completely new to the UModel graphical user interface, see the UModel Tutorial and How to Model... chapters to get started.)


In this example, the "Design View" package acts as a container for whatever is going to be the design part of your model (classes and class diagrams, for example), while the "MyNamespace" package will act as a namespace for all classes that are to be created. In general, however, the package structure is not prescriptive in any way; you may organize your packages in a different way if so required.


Right-click the "Design View" package and select Code Engineering | Set as C++ Namespace Root from the context menu. When prompted by UModel that the C++ profile will be applied to the package, click OK to confirm. The C++ profile built into UModel is now included to the project.


Next, click the "MyNamespace" package and select the <<namespace>> check box in the Properties window. This applies the "namespace" stereotype to the package and its icon changes to ic-code-pack. You can now create classes under this namespace.


Create C++ classes

You can either create classes directly from the Model Tree window, or from a class diagram. For the scope of this example, create a class diagram from the Diagram Tree window as shown below:


This example assumes that all your classes must be generated under the "MyNamespace" namespace. Therefore, when prompted to select an owner for the diagram, select the "MyNamespace" package (as illustrated below). If you choose a different package, any elements that you add to the diagram will belong to the same package as the diagram (which may or may not be the intended goal).



Next, create the classes, types, and other elements required in your model, for example, a simple diagram that illustrates a Car class:


Of particular interest in the diagram above is the enumeration ColorType and the data type string. These types are not C++ fundamental types, so they are not included in the C++ profile built into UModel. For this reason, they must be created in the model explicitly, using the Enumeration um_ic_enumeration and DataType um_ic_datatype toolbar buttons, respectively. By contrast, fundamental types (such as int or bool) are automatically available for selection as you type, see also Type Autocompletion in Classes. For step-by-step instructions about designing classes and their members, see Class Diagrams, as well as the How to Model... chapter.


Configure the project for code engineering

Right-click the "Component View" package and add a new Component um_ic_component (that is, a software component) to it. Click the new software component and, in the Properties window, set the following properties:


Code language of the component ("C++ 14", in this example)

Code generation directory ("C:\codegen", in this example).


Also, ensure that the "use for code engineering" property is set to True.


Next, create a ComponentRealization um_ic_realization relationship between the classes from which C++ code must be generated (Car and ColorType, in this example) and the code engineering component. This can be done either from a Component diagram, or, more simply, as follows:


In the Model Tree window, click the class to be realized by the component (Car and ColorType, in this example) and drag and drop onto the code engineering component (Component1).


Perform the same step for the ColorType class.


Note: In case you forget to create a ComponentRealization um_ic_realization relationship for a class, UModel still generates the corresponding code file, even though warnings will be issued in the Messages window. This setting is configurable from Tools | Options | Code Engineering tab (the check box name is Generate missing ComponentRealizations).


Generate C++ code

You can now generate the actual C++ code, as follows:


1.On the Project menu, click Merge Program Code from UModel Project. (Alternatively, press F12). A dialog box appears where you can adjust whether changes in code should be merged with those in the code, or overwrite them (if applicable). For the scope of this example, the default settings are OK, since code is generated for the first time. For more information, see Code Synchronization Settings.

2.Click OK. The Messages window displays the code engineering result.


© 2018-2024 Altova GmbH