Search code examples
mps

How to add a specific generation phase that would do Model-To-MS Word?


Assume that I have developed a set of behaviours in MPS that allow me to convert an instance of a WordDocumentconcept (and children), which describes a Word processor document, into a MS Word document using POI and that I have been able to implement an action in an MPS plugin that allows me to generate my desired MS Word document by a right click on my root node.

I would like to add this as a phase in the generation process, so that after the Model-To-Model phases, the generation process of MPS does Model-to-MS Word generation instead of Model-to-Text.

Is MPS customizable that way, and what would be the set of concepts to use?


Solution

  • When you run make or rebuild on models in MPS, MPS will start a so called MakeSession in this session MPS executes multiple steps. One step in the make session is for instance "generate", which runs the model to model transformation, and a second one is "textgen", which then writes the resulting model of the generate step to disk by executing the textgen definition of the languages.

    These individual steps are called a "facet". You can contribute your own facets into the over all make process. To do so you need to create a plugin aspect in your language and then create a facet in there. In the facet your can declare it's dependencies and priorities. In your case you want to run before textgen but after generation, so that you can access the result of the generation.

    The facets can stat their input data in a declarative way. In your case you need the GResource which represents the output of the generator facet. Then you can access the model(s) on it and run your POI code on it.

    A minimal example would look like this:

    facet RunPoi extends <none> {                                                                                                                                                                     
        Required: Generate, TextGen                                                                                                                                                                     
    
        <no optional facets>                                                                                                                                                                            
        Targets:                                                                                                                                                                                        
            target genWord overrides <none> weight default { 
                resources policy: transform GResource -> <no output>                                                                                    
    
            Dependencies:                                                                                                                             
                after generate                                                                                                                          
                before textGen                                                                                                                          
                before textGenToMemory                                                                                                                  
    
            <no properties>                                                                                                                           
            <no queries>                                                                                                                              
            <no config>                                                                                                                               
            (progressMonitor, input)->void { 
                foreach resource in input { 
                    SModel mdl = resource.model; 
                    // run poi code with mdl 
                } 
            }   
        }
    }