Tutorial – First TRIMM Java project (standalone)

This is the simplest TRIMM Java project possible. It doesn’t require any project dependencies.

Prerequisites:

Step 1 – Create an Enterprise Architect Model

Please follow the steps in this video

TRIMM Simplest Java Model – Enterprise Architect tutorial from Jeppe Cramon on Vimeo.

Step 2 – Configure the TRIMM Java code generator

TRIMM Java’s default configuration format is YAML, which is a very simple file format (like XML but without all the noisy brackets < >)
We create a configuration file e.g. called JavaModel.yml (the name and extension can be anything you like) and place it in C:\TRIMM-examples\SimplestJavaModel\
You can use NotePad, Edit, NotePad++ or what ever you like to create the file.

The first thing we need to tell TRIMM is where our XMI file is placed:

xmiModelPath: mysimplemodel.xml

Next we need to tell TRIMM which UML tool we used. Since we used Enterprise Architect we will use the value EA

umlTool: EA

Note: Other possible values are MagicDraw16, MagicDraw17 or the FQCN (Fully Qualified Class Name) for a Java class on the classpath that implements the XmiReader interface. Alternative you can specify a .groovy script (that also implements the XmiReader interface)

Finally we need to specify where TRIMM should output its files (relative to the directory from which it is run)
We do this using the generateBaseClassesToPath setting:

generateBaseClassesToPath: output

Note: The TRIMM code generator automatically cleans up files in its output folder that wasn’t part of the code generation phase. It’s therefore necessary that you specify generateBaseClassesToPath to be something different than an empty value, otherwise you will loose all the contents in your project folder!

The complete configuration file looks like this:

xmiModelPath: mysimplemodel.xml
umlTool: EA
generateBaseClassesToPath: output

That’s it for this simple model.

Step 3 – Generate code using TRIMM Java standalone

Open a Command Prompt (or Terminal on Unix/OSX)
For this example I’m assuming that you have extracted TRIMM Java standalone to C:\TRIMM-examples\SimplestJavaModel\ so that this folder now contains a bin and repo folder:

Simplest Java Model project folder content before code generation

Simplest Java Model project folder content before code generation

We want the generated code to be placed in a subfolder called generated.
Run the following command from the command prompt (on Windows):
bin\PojoYamlGenerator -yaml JavaModel.yml

This will yield a lot of console output which ends with the following:

Simple Java model TRIMM Java Console Output

Simple Java model TRIMM Java Console Output

If we do a listing of the files in the generated then we will see that it contains subfolders matching the dk.tigerteam.examples.simplestjavamodel we added to the UML model.
It also contains two files ContactInfo.java and Customer.java which are generated based on the two UML classes in the model.

Simple Java Model - TRIMM Java Generated folder content

Simple Java Model – TRIMM Java Generated folder content

The contents of the two files is shown below.
You might have noticed that the code references java.util.Date automatically eventhough we in our Enterprise Model only wrote Date as type. This is because TRIMM when configured using YAML automatically adds the BuiltInTypesListener extension to the configuration, which resolves typically used Java classes which are not defined in java.lang and therefore needs to be explicit (we use FQCN and imports since your model might contains a class by similar name).

Customer.java

package dk.tigerteam.examples.simplestjavamodel;

import dk.tigerteam.examples.simplestjavamodel.ContactInfo;

public class Customer {
    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private int age;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private java.util.Date created;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private String firstName;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private String lastName;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     * Property type hierarchy:
     * <ul>
     *   <li><b>ContactInfo</b></li>
     * </ul>
     */
    private ContactInfo contactInfo;

    public int getAge() {
        return age;
    }

    public void setAge(int parameter) {
        this.age = parameter;
    }

    public java.util.Date getCreated() {
        return created;
    }

    public void setCreated(java.util.Date parameter) {
        this.created = parameter;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String parameter) {
        this.firstName = parameter;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String parameter) {
        this.lastName = parameter;
    }

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo parameter) {
        this.contactInfo = parameter;
    }
}

ContactInfo.java

package dk.tigerteam.examples.simplestjavamodel;

public class ContactInfo {
    /**
     * No documentation<p/>
     * Defined in ContactInfo<p/>
     */
    private String email;

    /**
     * No documentation<p/>
     * Defined in ContactInfo<p/>
     */
    private String mobilePhone;

    public String getEmail() {
        return email;
    }

    public void setEmail(String parameter) {
        this.email = parameter;
    }

    public String getMobilePhone() {
        return mobilePhone;
    }

    public void setMobilePhone(String parameter) {
        this.mobilePhone = parameter;
    }
}

Please see Tutorial – Simplest TRIMM Java Maven example for how to use TRIMM Java from Maven and for more advanced configuration examples.

TODO: Create a tutorial that shows how to use TRIMM from Ant