Generator User Guide Demo 2
In this demo we again will generate Java Swing application, but unlike in previous Demo 1, this time it is going to be one Java application per input model (in Demo 1 we generated Java application for each input XML Document).
- create new language: 'generator_demo.L2'
- in language properties dialog add extended language : 'jetbrains.mps.sampleXML'
- create new generator for this language (see Demo 1 for details).
As the code we are going to generate is almost identical to that in Demo 1, we will copy the application class template from L1 generator:
- go to model 'main@generator' in L1
- select the 'Document' template in project tree and copy it to clipboard
- return to model 'main@generator' in L2 (select this model in tree) and paste from clipboard
- in the same manner copy template switch 'switch_JComponentByElementName' from L1 to L2 generator model
Conditional Root Rule
Conditional Root Rule generates root node in output model using template attached to this rule.
- go to L2 generator and open mapping configuration 'main' in editor
- add new Conditional Root Rule (press Insert while cursor is in conditional root rules section)
- make reference on the 'Document' template
- open the 'Document' template (ctrl-<left-click> on reference in editor)
- in the template's header remove 'Document' - press Ctrl+Del while cursor is on the word 'Document' (we are using this template in Conditional Root Rule which doesn't pass an input node to its template)
- remove property-macro from the class name - select '$' and press Ctrl+Del (the name of class is going to be static)
- rename class to 'DemoApp'
The SWITCH-macro in 'DemoApp' template is expecting an input XML Document in order to obtain the document's root element and apply template switch to it.
To provide the SWITCH-macro with correct input node we will wrap statement 'container.add(..);' into a LOOP-macro:
- in the template code select the statement 'container.add($SWITCH$[null]);' - put cursor inside statement, use Ctrl+W to inflate selection
- press Ctrl-Shift+M to add node-macro
- choose $LOOP$ from auto-completion menu (Ctrl+Space)
- enter code in the LOOP's mapped nodes function as below
The mapped nodes function will find all Documents in input model and return sequence of these documents (the return statement can be omitted) to generator.
Then generator will iterate this sequence, create 'container.add(..);' statement for each Document and pass Document as an input node to the SWITCH-macro.
That's it. The last step, as usual, is hitting Shift-F9 to re-generate generator.
Running First Test
To test L2 generator we need the exactly same input as in Demo 1 except that we are going to 'engage' different language on its generation.
- go to 'test1' model in 'test_models' solution (select it in project tree)
- choose Clone Model in popup menu
- enter the new model name: 'generator_demo.test2'
- in properties dialog of the 'test2' model, in the Languages Engaged on Generation section:
- remove 'generator_demo.L1' language
- add 'generator_demo.L2' language
Generate model 'test2' and switch to IntelliJ IDEA to review the generated files. You will find new package 'test2' containing the generated class 'DemoApp'.
Abandon Root Rule
Note that directory 'test2' 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 the Demo 1 we have defined explicit mapping of input Documents to output Classes using Root Mapping Rule.
In Demo 2 we do not map input Documents to any output roots.
To prevent copying of Documents to output model we will use Abandon Root Rule .
- return to L2 generator and open mapping configuration 'main' in editor
- add new Abandon Root Rule - press Insert while cursor is in the abandon roots section
- choose 'Document' as its applicable concept
Running Second Test
- switch to IntelliJ IDEA and delete all files from the 'source_gen/generator_demo/test2' directory
- return to MPS and regenerate the model 'test2'
- switch to IntelliJ IDEA again: the DemoApp is generated and no more XML files
End of Demo 2