Chain the transformation and the generation

At this point we have:

We now want to combine transparently these evaluations to generate in one step SQL code from a UML model:

  1. Click File > New > Rule Set.
  2. Type com.sodius.mdw.samples.tutorial/src in the Source folder field.
  3. Type tutorial.uml2relational in the Package field.
  4. Type TransformAndGenerate in the Name field.
  5. Click Finish.

Change the TransformAndGenerate ruleset contents to:

package tutorial.uml2relational;

public ruleset TransformAndGenerate(in source : uml21) {

    public rule main() {
        // create an empty Relational model
        var target = context.createModel("relational");
		
        // Transform input UML model to Relational
        @UML2Relational(source, target).main();
		
        // Generate SQL code
        $GenerateSQL(target);
    }

}

This ruleset:

  1. creates an empty Relational model (using the special variable context),
  2. transforms the input UML model into this Relational model (invoking the rule main of the ruleset UML2Relational),
  3. generates SQL code using the Relational model (the character '$' is the notation used to call a text template).

The ruleset TransformAndGenerate takes an input UML model. Here the Relational model is not visible from the end-user. It's just a pivot model used to reduce the complexity of the evaluation: it enables to break into two distinct steps the transformation of a UML model into SQL code. Note that it is just a design choice, we may have chosen to declare the Relational model as an output model as well.

You can launch the ruleset TransformAndGenerate, using the product21.xmi file as UML input.

The evaluation report now contains a file generated/generatedTables.sql. Double-clicking on this file opens a text editor:

-- 
-- TABLE IdentifiedElement
-- 
DROP TABLE IF EXISTS `IdentifiedElement`;
CREATE TABLE `IdentifiedElement` (
    `ID` INT 
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
-- 
-- TABLE NamedElement
-- 
DROP TABLE IF EXISTS `NamedElement`;
CREATE TABLE `NamedElement` (
    `NAME` VARCHAR(255) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

...

Related reference
Context variable
Text template call