code-generationdslmps

How to use multiple concepts per text gen component in Jetbrains MPS 2017.1?


I created a simple DSL in JetBrains MPS for an automation task at hand. The DSL allows defining information objects which in turn shall be used for code generation. I am able to generate my models - but I am struggling to find a way to assign an additional text gen component to the same language concept, i.e. what I am trying to achieve is:

I feel that I got a wrong understanding on how to use this. Any ideas on how to achieve different outputs for the same language concept?


Solution

  • MPS will only allow you to have single textgen definition per concept.

    The MPS way to do what you want to do is to do this before textgen with a model to model transformation.

    Define additional concept for your three objects you want to create. Their content is basically the same as in the root concept that you already have. Theses concept will only be used to define your textgen. So next define the textgen for theses concepts.

    Now you still need to create instances of these because of course you don't want the user to specify theses information multiple times. To do this you need to define a generator. Generators are used for model to model transformations in MPS.

    By default your language should already have a generator. In the generator is a mapping configuration main. In there you create a root mapping for each of the of the three new concepts you created. Each of this rules has as the input concept the one that the user specifies and as a template one of the new concepts. You can create a new template by pressing alt + enter in the editor and choose the concept you want to create. Make sure you set keep input root to true instead of default.

    Inside the template you specify a COPY_SRC to copy all of the contents from the input to the output.

    What then happens during generation is that MPS will create three new concepts out of the one that the user created in the original model and after the executes the textgen for all of them. This also allows you to create different files for each root.

    I think the DEMO 1 project shows some of the things you will need for this.