Sunday, 1 September 2019

Rational Rhapsody Tip #64 - Customizing Rhapsody's C/C++ code generator using a simplifier plugin

I've been doing more training lately with the IBM Rational Rhapsody Developer Edition. This has promoted me to show a few videos on the fundamentals of C/C++ code generation. This video gives an "under the hood" look at the two stage process that underlies the built-in C/C++ code generation technology. Based on a modified version of the UsersSimplifiers.rpy project in the Rhapsody samples folder, it shows how we can use plugin customisation to make a single change to the generated code for many files.
















Not many people realize that Rhapsody's own C and C++ code generator is based on a two stage process starting with a model-to-model transform. We can view this model-to-model transform by setting the CPP_CG::Configuration::ShowCgSimplifiedModelPackage property and regenerating the code.


The results of the "simplifier" step will then be shown in the Rhapsody browser. This "simplification" takes into account diagrams, properties and other settings in the model. The model is called 'simplified' because it is flattened out into just types, attributes, operations and relations, e.g., statecharts are converted into C/C++ operations that will implement them.


The user model has the diagrams and properties and other settings that determine what to generate. Whereas the simplified model has a simplified metamodel that has a direct correspondence with the code that will be written into the .h/.cpp/.c files

So, why make the simplified model available in the browser? Well, it's because it's possible to customize this simplification step.

This project is a modified version of the UserSimplifiers sample in the Rhapsody installation.


Let's look at an example of a call-back hook available during C/C++ code generation. This plugin and these hooks will be pulled into the Rhapsody process when the project is loaded. In this example, this post simplication call-back checks for a «SingletonPattern» stereotype on classes.


If present, the Java plugin adds additional elements to the simplified model prior to Rhapsody writing the .h/.cpp files.


Let's re-generate again to a different configuration's folder to view its effect. First we need to set the stereotype that the post simplification hook is looking for.


The simplified model now has additional elements added by the simplification plugin.


The active code view shows us the .h/.cpp files that were generated. It includes user-defined methods as well as operations/attributes automatically generated. We can view the differences using a textual diff tool


Let's look at another example, an onPostSimplication call-back for operations. This adds a call the Reporter singleton class to the body of operations.


To get the simplifier plugin change to be reloaded, we need to completely exit and re-launch Rhapsody. When we open the sample model the plugin will be loaded into the Rhapsody process (due to a profile that loads a .hep profile).

Like pretty much everything in Rhapsody, the simplifier is controlled through Rhapsody properties. Here we can see that the stereotype is setting CPP_CG::Operation::Simplify to ByUserPostDefault.


With the simplifier plugin now active for operations, let's re-generate the code. We can then view the results in the textual differencing tool. This code was added by the simplifier plugin.


Like adding a new gear to your process, using customisations like this we can make a single change to the generated code for many files.

No comments:

Post a Comment