Create a ruleset to launch the Java template

In previous step, we created a text template expecting a UML Class parameter to generate a Java source file. Now we want to iterate on all classes of a UML model and generate the Java source file for each of these classes. This is done using a ruleset.

A ruleset is a group of logically interdependent rules, where a rule defines a set of procedural expressions that query or update model elements.

To create a ruleset:

  1. Click File > New > Rule Set.
  2. Type com.sodius.mdw.samples.tutorial/src in the Source folder field.
  3. Type tutorial.java in the Package field.
  4. Type JavaGeneration in the Name field.
  5. Select Call a text template in the Available patterns section.

  6. Click Next.
  7. Type tutorial.java.JavaSource in the Text template field, on second page.

  8. Click Finish.

A file JavaGeneration.mqr is created in the folder com.sodius.mdw.samples.tutorial/src/tutorial/java and is opened in an editor.

package tutorial.java;

public ruleset JavaGeneration(in model : uml21) {

    public rule main() {
        foreach (class : uml21.Class in model.getInstances("Class")) {
            $JavaSource(class);
        }
    }

}

Here is the behavior of this ruleset:

  1. Expects an input UML model (the direction in tells MDWorkbench the model parameter is expected to be loaded, from an XMI file for example)
  2. Retrieves the list of instances of the type Class in the input UML model, using model.getInstances("Class").
  3. Iterates on this instance list using foreach and the loop variable class of type UML Class.
  4. Calls the text template JavaSource with the class variable as argument ('$' is the notation used to call a text template). The referenced template is evaluated and the generated contents is written on disk.

Related concepts
Ruleset

Related reference
MQL (Model Query Language)
Model APIs
Text template call