Generator User Guide Demo 5

In this demo we will learn how to use generation scripts and utility classes.

There are two kinds of generation scripts: pre-processing and post-processing scripts.
Pre-processing scripts are invoked before applying of generator rules and usually alter input model in a way that makes it easier for further processing by rules.
Post-processing scripts are invoked after all generator rules are done and are applied to output model.

New Language

See Demo 2 for details.

Doing so you won't be even offered to import generator model from L4.

New Test Model

Pre-processing Script

Our L5 generator doesn't support text nodes, but text node in this context has the same meaning as 'label' element with 'text' attribute.
Therefore, instead of adding more rules to our generator, we will add pre-processing script which will convert text nodes to 'label' elements and let existing rules do rest of the job.

First Test

Re-generate generator, generate files from model 'test5', run the generated 'generator_demo.test5.DemoApp' class.

Utility Classes in Generator

Now suppose we got an idea to fix possible syntax and stylistic errors in generated texts. We can create a post-processing script to do that but those NLP algorithms tend to be quite complex, so we decide to develop them in a separate utility class.

The problem is that an utility class can not be created in generator model 'main@generator' because any root node in any model with stereotype 'generator' is treated as a root template.
The solution is to create another model in generator module:

We are going to call the 'fixText()' method in post-processing script and pass output model as a parameter. The output model contains classes, methods, expressions and so on. Thus we are manipulating with a java-like syntax tree here.
The 'fixText()' method will replace all strings "MPS" with strings "JetBrains MPS".

Post-processing Script

Now we can use this utility method 'fixText()' in post-processing script:

Re-generate generator.
You can use Ctrl-F9 (generate all changed models in module). (warning) Note that Shift-F9 (generate current model) is not enough any more because now we have two models requiring generation.

Second Test

Generate model 'test5', run 'DemoApp'.

End of Demo 5