This section is a detailed tutorial indicating how to customize the C++ Designer generator. The goal of this example is to handle stereotyped classes to generate specific code.
The customization happens over five stages:
Creating a variant
To create a new variant, you only have to copy the structure of the standard variant directory, found in your project’s resources.
The “standard” variant directory
The “init.py” file is mandatory in the main variant’s directory, as well as in the “acts” directory, in order to make python act available to the engine. If this file is missing, your python ACT won’t be loaded.
The description file must be updated to indicate what your custom variant does.
Name the copied variant “CustomVariant”. For now, remove all content from the “acts” and “types” directories, and open the “products” directory.
Defining a new product
Our new stereotype is available on Classes, that is to say we have to edit the “Class.py” product file.
All we have to do is add a new generation task when an element has the <<CxxCustomClass>> stereotype.
1# Product definition for Class
2# Bound variables:
3# PRODUCT the product being build
4# ELT the element to generate
5
6import act
7from java.util import ArrayList
8from com.modeliosoft.modelio.api.mda.model import ObUtils
9
10if (CXX.isCxxElement(ELT) and not act.isNoCode(ELT)):
11 # headerFile = CXX.makeNamespacePath(ELT) + "/" + CXX.makeCxxName(ELT) + "." + CXX.makeHeaderFileExtension(ELT)
12 headerFile = CXX.makeDefaultHeaderFilePath(ELT)
13
14 # bodyFile = CXX.makeNamespacePath(ELT) + "/" + CXX.makeCxxName(ELT) + "." + CXX.makeBodyFileExtension(ELT)
15 bodyFile = CXX.makeDefaultBodyFilePath(ELT)
16
17 files = ArrayList()
18 if (ELT.isStereotyped("CxxCustomClass")):
19 PRODUCT.addFileGeneration(ELT, files, "CustomClassGen")
20 else:
21 if (act.isExternal(ELT)):
22 if (ObUtils.isTagged (ELT, "Cxx.GenerateHeaderFile")):
23 files.add(headerFile)
24 else:
25 files.add(headerFile)
26 files.add(bodyFile)
27 PRODUCT.addFileGeneration(ELT, files, "standard.ClassGen")
The new product file launches a generation using a new ACT, “CustomClassGen”, when the current class is stereotyped <<CxxCustomClass>>. Otherwise, the standard ACT, “ClassGen” is applied.
Using “standard.ClassGen” means the generator will take the “ClassGen” ACT from the standard variant. It is slightly different from entering only “ClassGen”, which would search an ACT with this name in the current variant before launching the standard one.
We now have to add the new ACT called from this product.
Defining a new python ACT
In the “acts” directory of the custom variant, just add a new file called CustomClassGen.py.
Here is a sample showing the content of this file:
1#
2# Template CustomClassGen
3#
4from com.modeliosoft.modelio.cxxdesigner.engine.act import IAct
5from com.modeliosoft.modelio.api.model import *
6from com.modeliosoft.modelio.api.mda.model import ObUtils
7import act
8
9class CustomClassGen (IAct):
10
11 ################################################################################
12 # Generation code
13 #
14 def run(self, ctx, el):
15 hxx = ctx.getOutputs()[0]
16 cxx = ctx.getOutputs()[1]
17
18 hxx.println("// Write custom code for the header here")
19
20 cxx.println("// Write custom code for the header here")
It is usually a better idea to modify an existing ACT rather than to create a new one from scratch.
The variant will be ready to use as soon as it has been declared in your Modelio project.
Declaring the variant
Open the C++ project’s edition box, and select the generation tab.
The “Generation” tab of the project edition box
We have to select the variant in this tab, but this isn’t available right now.
Open the variant management box, in the upper right corner.
The “Variant management” box
Add a new variant, and select the directory corresponding to “CustomVariant”.
Here it is, the variant is available in your project. Once selected, it will be usable for the C++ generation.