Code Generation

Home
Code Generation
Microwave Model
SpaceRacer AI Model
TankWars AI Model

Code Generation

This page describes the code generation strategy used by the exporter for Class Diagrams with State charts. The process is separated into two steps, the analysis and the code generation.

Analysis

The first step of the code generation is the analysis of the Class Diagrams and the State Charts and their transformation into an intermediate representation. This intermediate representation avoids code duplication in code generators and decouples the generation of code from the representation of our model.

Processing the Class Diagram is a fairly straight forward process. The analysis collects and stores the class's attributes, methods, constructors and destructors. As for the state chart, the model is flatten (as done by David Meunier's code from the original exporter) and reduced to a much simpler format.

This part of the export process is done by the exporter_ClassDiagram.py and exporter_StateChartDiagram.py files.

Code Generation

Once we have the intermediate representation, we can easily generate the code. Although C++, Java and Python are different programming languages, the code generation process is fairly similar and straight foward. One expection to this would be C++, where all allocated memory must eventually be manually reclaimed.

This part of the export process is done by the individual exporters:

  • Python : exporter_PythonExporter.py
  • Java : exporter_JavaExporter.py
  • C++ : exporter_CPPExporter.py
  • C# : exporter_CSharpExporter.py

Libraries

The Class Diagram with State Chart formalism includes a construct, GEN(target, message), that allows a class to queue a message (an event) on a target class. In a type-strict language (such as Java or C++), this is tricky to implement because all the Class Diagrams with State Charts must share a common type.In Java, all CDST are the implementation of the Atom3CDST interface. In C++, all CDST are the subtype of the Atom3CDST. The source code for these constructs can be found in the lib directory of the CD_classDiagramsV3 formalism. These files must be copied into the project source directory for the generated CDST code to be used.

Comparing the Code Generators

FeaturePythonJavaC++C#
Generate CodesYesYesYesYes
Allows bool, int or float attributesYesYesYesYes
Allows string attributesYesYesNoNo
Allows for SubclassesNoNoNoYes
Allows for DestructorsNoNoYesNo
Allows for Pre/Post class codePrePrePreBoth
Supports GEN construct
(inter-class communication)
YesYesYesNo
Allows HistoryNoNoNoNo