ruleset

Syntax

visibility ruleset ruleSetName(parameters) {
    rules declarations
}

where:


Each parameter is of the form:

direction name : type

where:

Description

The ruleset statement defines the signature of a ruleset, as well as the contained rules.

The name of a ruleset must be unique within its package, and should not collide with a text template or a Java type. It must match the name of the file in which it is defined: a ruleset ModelTransformation must be defined in a ModelTransformation.mqr file.

The ruleset may define expected parameters, that will be accessible to each rule of the ruleset. Any caller of the ruleset must provide matching arguments.

Examples

This will generate a Java source file for each Class contained in a UML 2.1 model:

package com.mycompany.example;

// expects a loaded UML 2.1 model as input
public ruleset GenerateAllJavaClasses(in model : uml21) {

    public rule generate() {
    
        // loop on each Class of the UML 2.1 model
        foreach (class in model.getInstances("Class")) {
        
            // calls the text template GenerateJavaClass
            $GenerateJavaClass(class);
        }
    }
}

This will add a super class to each Class of a UML 2.1 model:

package com.mycompany.example;

// Expects a loaded UML 2.1 model as input
// The model is modified and may be saved once the evaluation is completed
public ruleset CreateUMLModel(inout model : uml21) {

    public rule main() {
    
        // creates a new Class
        superClass = model.create("Class");
        superClass.name = "SampleSuperClass";
        
        // loop on each Class of the UML 2.1 model
        foreach (class in model.getInstances("Class")) {
        
            // makes the class to inherit from superClass
            if (class != superClass)
                @addGeneralization(class, superClass) {
        }
    }
    
    private rule addGeneralization(class : uml21.Class, superClass : uml21.Class) {
        generalization = model.create("Generalization");
        generation.child = class;
        generalization.parent = superClass;
        generalization.namespace = class.namespace;
    }
}

Related concepts
Ruleset