Modeling and Code Generation


Getting Started: Modeling and Code Generation

Modeling is a good way to design your software. And when it comes to implementing your design, UML Lab's integrated code generator will save you a lot of time - while keeping your coding experience completely flexible. This short tutorial will guide you in generating code easily.

Quickstart: Create a new UML Lab Java Project by selecting File > New > Other > UML Lab Java Project from the menu, choose a project name and confirm the wizard with Finish. Now just start modeling with UML Lab: Create classes for instance just by dragging open a rectangle with your mouse. As soon as you save your diagram, UML Lab will automatically generate source code for you - if you want. That's all there is to it!

Start by creating a new UML Lab Java Project. Select File > New > Other from the menu and choose UML Lab Java Project in the upcoming dialog. You can find it easily by typing "UML" into the filter text area at the top.

In the next dialog enter a project name and confirm the project creation with Finish. Now you have a new Java project with an empty model and class diagram waiting for you to start modeling.

Note: If you want to work with an existing project or a different project type (e.g. PHP), just use the context menu of your project in the Package Explorer and select New > UML Model and Class Diagram. The following dialog will let you choose a name for your new model and diagram. Confirm with Finish to create the new model.

Creating new model elements

Create new classes easily with drag & drop: Click into the diagram and drag down diagonally with the left mouse button to pull up a rectangle. As soon as you release the mouse button, a new class is created. By dragging with the right mouse button instead, you can choose between classes, interfaces, packages, data types or enumerations from a context menu.

Relationships between classes are created much the same: Using the right mouse button, drag from one class to another. This will again trigger a context menu allowing you to choose between associations, generalizations and more.

And renaming the new elements is just as easy: Just start typing to enter the new name and finish with ENTER. The selected element will be renamed instantly.

Properties and operations too are created simply by typing. Just select the target class and start entering a property name or method signature. Of course, full code completion is available for return types, parameter types and more. Properties are created with a signature of name:type, operations with name(parameterName:ParameterType):ReturnType. Parameters and return types are optional. You can also preface the signature with one of +,-,#,~ to choose a visibility of public, private, protected or package respectively.

Alternatively, you can use the Palette at the right side of the diagram, which is collapsed by default. It contains tools for creating all available elements. Extended settings for the current selection (multiplicities, default values, packages, stereotype applications, etc.) are available from the Properties View below the diagram editor.

Generating Code

UML Lab conveniently generates source code on every save. You will be asked about this default setting when saving a diagram for the first time (you can also change it later from the preferences dialog). In addition, you can generate code at any time by using the Generate Code entry from the context menu or the UML Lab menu.

Congratulations, you have just generated your first source code! Let UML Lab help you save time and improve code quality by generating code for repeating patterns instead of implementing boilerplate code again and again.

Closing remarks

Of course this is just the beginning; UML Lab has a lot more to offer and will support you with your own individual development style. A few examples:

  • To review the impact of your model changes to the source code at any time, simply use the Compare Generated Code action from the diagram's context menu.
  • Navigate easily between model and source code: Just select Navigate to > Source from the selected element's context menu to open the source in a separate editor.
  • Change the code style for individual elements or the whole model's target language from the Properties view's Code Style section. Try it yourself: Change the code style of a class or package to JPA for example and have a look at the newly generated code.
  • Stay agile with UML Lab's Round-Trip-EngineeringNG: No matter if you are working on the model or in the code, you can keep working just as you are used to. UML Lab will take care of synchronizing all changes between model and code for you. Work the way it's most convenient - in the model or directly in the source code - and let UML Lab do the rest.

Further information on using UML Lab and working with custom templates for code generation, reverse engineering and Round-Trip-EngineeringNG is available in the advanced tutorials ("Creating Code Styles for existing code" and "Modeling of software and systems by using UML Profiles").