Quick Tour - Rules Development

This document contains descriptions of some of the more noteworthy features of rules and services development with MDWorkbench.


Generation

Text template

A text template specifies the information, source code or other text, to generate in a given file as well as the name of the file.

Using TGL WYSIWYG layout capabilities, any text document is easily generated.

[#package com.mycompany.example]

[#-- we define a template GenerateJavaFile which expects a UML class argument --]
[#template GenerateJavaFile(class : uml21.Class)]
[#file]${class.name}.java[/#file] [#-- the file where to write generated contents --]
public class ${class.name} {

    public String toString() {
        return "${class.name} instance";
    }
}
[/#template]

Java integration

Java Bean capabilites are transparently integrated in text templates, allowing developers to access Java classes in MDWorkbench.

Java Integration


Protect hand-written user code

Protected tags can be used to define user code areas preserved in the generated files, allowing iterative development lifecycles.

public void ${operation.name}() {
    [#protectedStartTag]// Start of user code of method ${operation.name}()[/#protectedStartTag]
    TODO insert your manual code here
    [#protectedEndTag]// End of user code[/#protectedEndTag]
}

Whitespace handling

Whitespaces are automatically ignored (not printed to output) on lines that contain only processing directives (e.g. 'if' and 'foreach' statements).

This lets you write well formated templates without impacting the generated output.

[#template WhitespaceExample(model : uml21.Model)]
[#file]test.txt[/#file]·[#-- the file where to write generated contents --]
An example of whitespace stripping:
[#-- check if the class is abstract --]
[#foreach class in model.getInstances("Class")]
    class name: ${class.name}
····[#if class.isAbstract()]
        this class is abstract [#-- there is static text on this line, no whitespace stripping --]
····[/#if]
[/#foreach]
[/#template]

Transformation

Transformation rulesets

Model transformations are specified using rulesets.

A ruleset is a collection of rules that define the various steps that are needed in order to accomplish a transformation.

Rules are implemented using a dedicated imperative language, the Model Query Language (MQL).

Transformation rulesets


MQL (Model Query Language)

The Model Query Language (MQL) is an intuitive language for accessing and creating information in models.

Constructs exist to easily iterate over collections of elements, and to simply retrieve values in the model without complex expressions or difficult syntax.

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);
        }
    }
}

Scripts

Support of scripts

Common functionality may be discovered that should be encapsulated on the various metatypes.

MDWorkbench allows the definition of scripts on metatypes to dynamically enhance a metamodel.


Support of profiles

A profile in MDWorkbench enables you to enhance a metamodel with dynamic metatypes.

Scripts can then be attached on these new metatypes, allowing you to use polymorphism and inheritance rather than 'if' statements in a script's implementation.


Debugger

Breakpoints and step by step

A debugger is available in MDWorkbench. You can:

  • Toggle line breakpoints
  • Suspend/resume executions
  • Evaluate step by step

Variables

Variables can be inspected when the execution is suspended. Using this view, you can inspect:

  • local variables and parameters
  • transient links
  • the currently generated contents, in case of a text template