Build Your Own Generator¶
At BESSER, you can also build your own code generator. Code generators consist of M2T model-to-text transformations to automatically generate software artifacts from an input model (could be any type of model).
BESSER provides an interface (abstract class) called GeneratorInterface that you can inherit to build your code
generator. This way, we standardize the use of BESSER code generators, improve maintainability, and usability (you can
check the code of
the GeneratorInterface in the repository).
As an example, let’s look at our Python class code generator below. Notice how this generator inherits from the
GeneratorInterface class and defines two methods:
- Constructor method
__init__() this method contains the parameters
model(indicating the B-UML model) andoutput_dir(the directory where the generated code will be stored) which is optional.
- Constructor method
generate()methodto generate the
classes.pyfile. The M2T transformation (lines 30 to 34) is performed using Jinja, a templating engine for generating template-based documents. However, you could use the tool of your choice for these transformations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
Remember that in BESSER, B-UML models have a set of methods to facilitate their traversal. For example, for structural models,
the class.attributes method gets the list of attributes of the class, class.all_attributes gets the list of attributes
including the inherited ones (if the class inherits from another one), model.classes_sorted_by_inheritance() gets the classes
of the model sorted according to the inheritance hierarchy, and so on. You can consult the API documentation for
more information.
Registering Your Generator in the Web Editor¶
To make your generator available in the BESSER Web Modeling Editor, register
it in besser/utilities/web_modeling_editor/backend/config/generators.py. Each entry uses the
GeneratorInfo NamedTuple:
from besser.utilities.web_modeling_editor.backend.config.generators import GeneratorInfo
SUPPORTED_GENERATORS["my_generator"] = GeneratorInfo(
generator_class=MyGenerator,
output_type="zip", # "file" for single-file output, "zip" for archives
file_extension=".zip", # extension of the generated artifact
category="web_framework", # logical grouping (object_oriented, web_framework, database, etc.)
requires_class_diagram=True, # True if the generator needs a ClassDiagram as input
)
Set requires_class_diagram=False for generators that consume other diagram types
(e.g., agent diagrams, quantum circuits, deployment models).
You should also add a filename mapping in get_filename_for_generator() in the same file so
the download response uses a meaningful name.