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:
- Click File > New > Rule Set.
- Type JavaGeneration/src in the Source folder field.
- Type tutorial.java in the Package field.
- Type JavaGeneration in the Name field.
- Select Call a text template in the Available patterns section.

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

- Click Finish.
A file JavaGeneration.mqr is created in the folder JavaGeneration/src/tutorial/java and is opened in
an editor.
package tutorial.java;
public ruleset JavaGeneration(in model : rhapsody) {
public rule main() {
foreach (class : rhapsody.Class in model.getInstances("Class")) {
$JavaSource(class);
}
}
}
Here is the behavior of this ruleset:
- Expects an input Rational Rhapsody model (the direction in tells RulesComposer the model parameter is
expected to be read from Rational Rhapsody.
- Retrieves the list of instances of the type Class in the input Rational Rhapsody project, using
model.getInstances("Class").
- Iterates on this instance list using foreach and the loop variable
class of type Rational Rhapsody Class.
- 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.
Next Section: Run the generation
Prev Section: Create a Java generation template

Ruleset

MQL (Model Query Language)
Model APIs
Text template call