Generator User Guide Demo 2

In this demo we will again generate a Java Swing application, but unlike in previous Demo 1, this time it is going to be a single Java application per input model (in Demo 1 we generated a separate Java application for each input XML Document). The single Java application that Demo 2 creates, will contain all components mentioned in all XML Documents of the input model.

New Language

We need to do some little technical setup first:

As the code that we are going to generate is almost identical to that in Demo 1, we will copy the application class template from the demoLang1 generator:

Conditional Root Rule

The process of code generation in MPS can be viewed as a series of model-to-model transformations. An AST in one language is gradually transformed into an AST expressed in concepts from another language. In each step individual concepts from one language get translated (reduced) into concepts from another language. Conditional Root Rule is somewhat special in that it creates new nodes out of nothing. It generates a brand new root node in the output model using a template referred to in this rule without having any original node in the input model.


The SWITCH-macro in 'DemoApp' template is expecting an XML Document on the input, since this is how we implemented it in Demo 1. It then extracts the Document's root element and applies a template switch to it. However, there may be multiple Documents in the input model (in fact in our demo app will have two - a button and a label), so we need to gradually process them all in sequence and generate an appropriate "container.add(null);" statement for each of these Documents.
To provide the SWITCH-macro with a single Document node, we will wrap the 'container.add(..);' statement inside a LOOP-macro:

The mapped nodes function will find all Documents in the input model and return a sequence of these Documents to the generator (the return statement can be omitted in BaseLanguage).
Then, the generator will iterate through this sequence, create 'container.add(..);' statement for each Document and gradually pass each Document as an input node to the SWITCH-macro.

That's it. The last step, as usual, is hitting Shift-F9 to make the generator.

Running First Test

To test the demoLang2 generator we need the exactly same input as in Demo 1 except that we are going to 'engage' different language on its generation.

Generate the 'test2' model and preview the generated sources.

Abandon Root Rule

Note that the generated output also contains two 'generated' XML files: Button.xml and Label.xml. MPS did not generate those XML files in previous Demo 1. Why does it do so in this demo?

Because in Demo 1 we have defined explicit mapping of input Documents to output Classes using Root Mapping Rule, the input was consumed and the xml files were not generated..
In Demo 2 we do not explicitly map input Documents to any output roots and so they remain in the model until the text-generation phase, which turns them into xml files.

To prevent copying Documents to the output model we will use the Abandon Root Rule.

After rebuilding the project you will no longer get xml files on the output.