Chapter 4. Code Import and Code Generation

Umbrello UML Modeller is a UML modelling tool, and as such its main purpose is to help you in the analysis and design of your systems. However, to make the transition between your design and your implementation, Umbrello UML Modeller allows you to generate source code in different programming languages to get you started. Also, if you want to start using UML in an already started C++ project, Umbrello UML Modeller can help you create a model of your system from the source code by analysing your source code and importing the classes found in it.

Code Generation

Umbrello UML Modeller can generate source code for various programming languages based on your UML Model to help you get started with the implementation of your project. The code generated consists of the class declarations, with their methods and attributes so you can fill in the blanks by providing the functionality of your classes' operations.

Umbrello UML Modeller 2 comes with code generation support for ActionScript, Ada, C++, C#, D, IDL, Java™, JavaScript, MySQL, Pascal, Perl, PHP, PHP5, PostgreSQL, Python, Ruby, Tcl, Vala, and XMLSchema.

Generating Code

In order to generate code with Umbrello UML Modeller, you first need to create or load a Model containing at least one class. When you are ready to start writing some code, select the Code Generation Wizard entry from the Code menu to start a wizard which will guide you trough the code generation process.

The first step is to select the classes for which you want to generate source code. By default all the classes of your model are selected, and you can remove the ones for which you do not want to generate code by moving them to the left-hand side list.

The next step of the wizard allows you to modify the parameters the Code Generator uses while writing your code. The following options are available:

Options for the Code Generation in Umbrello UML Modeller

Options for the Code Generation in Umbrello UML Modeller

Generation Options

Comment Verbosity

The option Write documentation comments even if empty instructs the Code Generator to write comments of the /** blah */ style even if the comment blocks are empty. If you added documentation to your classes, methods or attributes in your Model, the Code Generator will write these comments as Doxygen documentation regardless of what you set here, but if you select this option Umbrello UML Modeller will write comment blocks for all classes, methods and attributes even if there is no documentation in the Model, in which case you should document your classes later directly in the source code.

Write comments for sections even if section is empty causes Umbrello UML Modeller to write comments in the source code to delimit the different sections of a class. For example public methods or Attributes before the corresponding sections. If you select this option Umbrello UML Modeller will write comments for all sections of the class even if the section is empty. For example, it would write a comment saying protected methods even if there are no protected methods in your class.

Folders

Write all generated files to folder. Here you should select the folder where you want Umbrello UML Modeller to put the generated sources.

The Include heading files from folder option allows you to insert a heading at the beginning of each generated file. Heading files can contain copyright or licensing information and contain variables that are evaluated at generation time. You can take a look at the template heading files shipped with Umbrello UML Modeller to see how to use these variables for replacing your name or the current date at generation time.

Overwrite Policy

This option tells Umbrello UML Modeller what to do if the file it wants to create already exists in the destination folder. Umbrello UML Modeller cannot modify existing source files, so you have to choose between overwriting the existing file, skipping the generation of that particular file or letting Umbrello UML Modeller choose a different file name. If you choose the option to use a different name, Umbrello UML Modeller will add a suffix to the file name.

Language

Umbrello UML Modeller will by default generate code in the language you have selected as Active Language, but with the Code Generation Wizard you have the option to change this to another language.

Generation Wizard Generation

The third and last step of the wizard shows the status of the Code Generation process. You need only to click on the Generate button to get your classes written for you.

Note that the Options you select during the Code Generation Wizard are only valid for the current generation. The next time you run the wizard you will need to re-select all the options (your headings folder, overwrite policy, and so on). You can set the defaults used by Umbrello UML Modeller in the Code Generation section of the Umbrello UML Modeller settings, available at SettingsConfigure Umbrello UML Modeller...

If you have set your Code Generation options to the right settings and want to generate some code right away without going through the wizard, you can select the entire Generate All Code from the Code menu. This will generate code for all the classes in your Model using the current settings (including Output Folder and Overwrite Policy, so use with care).