After you design the model of your application in UModel (for example, one or more class diagrams), you might want to quickly generate a prototype project which includes all defined interfaces, classes, operations, and so on, in your language of choice. UModel enables you to generate C#, VB.NET, or Java program code from a model, based on UML elements found in your UModel project (such as interfaces, classes, operations, and so on). This process is also known as "forward engineering". The generated code will create all objects exactly as they were defined in the model, so that you can proceed to their actual implementation.
Code generation is also applicable to XML schemas and databases*. For example, you could design an XML schema or a database with UModel and then generate the corresponding file (or SQL script, in case of databases) from the model. To achieve this, consult the mapping tables to find out which schema or database elements map to UModel elements, see UModel Element Mappings.
* Engineering databases requires UModel Enterprise of Professional editions.
In order for code generation to be possible, the UModel project must meet the following minimum requirements:
|•||One of the packages in your project must be designated as namespace root (see instructions below). The namespace root can be a C#, Java, VB.NET, XSD, or Database namespace. This package must contain all classes and interfaces from which code is to be generated.|
|•||A component must exist which is realized by all classes or interfaces for which code must be generated.|
|•||The component must have a physical location (directory) assigned to it. Code will be generated in this directory.|
|•||The component must have the property use for code engineering enabled.|
|•||In case of databases, a connection to the target database must be created first, using the menu option Project | Import SQL database. Once the connection is established, you can design or modify the database structure in the model and commit the changes to the database through a SQL script .|
In addition to this, it is recommended that you include one of the built-in UModel subprojects corresponding to the language (or the language version) you want to use, see Including Subprojects. For example, if your application must target a specific version of C#, Java, or VB.NET, this would enable you to use the corresponding data types while designing your UML classes, interfaces, and so on.
For a worked example of how to create a project from scratch and generate code from it, see Example: Generate Java Code.
To set a package as namespace root:
|•||Right-click a package in the Model Tree and select Code Engineering | Set as <...> Namespace Root from the context menu, where <...> is one of the following: C#, Java, VB.NET, XSD.|
When you set a package as namespace root, UModel informs you that the UML profile of the corresponding language will also be added to the project and applied to the selected package. Click OK to confirm when prompted by a dialog box such as the one below.
To add a component which realizes the desired classes or interfaces:
|1.||Right-click a package in the Model Tree and select New Element | Component from the context menu. This adds a new Component to the model.|
|2.||In the model tree, click the class or interface that must be realized by the component, and then drag and drop the cursor onto the component (in this example, Class1 from Package1 was dragged onto Component1). This automatically creates a ComponentRealization relation in the Model Tree.|
There is also an alternative approach to do this, by creating a Component diagram and then drawing a ComponentRealization relation between the component and the classes or interfaces. For more information, see Component Diagrams.
To prepare a component for code engineering:
|1.||Select the component in the Model Tree (it is assumed that this component is already realized by at least one class or interface, as explained above).|
|2.||In the Properties window, locate the directory property and set it to the path where you want to generate code.|
|3.||In the Properties window, select the check box use for code engineering.|
For example, in the image below, the component Component1 from package Component View is configured to generate Java 8.0 code into the directory C:\codegen:
© 2019 Altova GmbH