Use filenames associated to objects in Rational Rhapsody

In this step, we will change the Java template to generate Java files according to folders and filenames chosen by Rational Rhapsody.

For each object, Rational Rhapsody proposes two files: one for the specifications and a second for the body. Typically files with extension ADS and ADB in Ada, H and CPP in C++ and only JAVA in Java language.

To add a script:

  1. Click File > New > Script.
  2. Type JavaGeneration/src in the Source folder field.
  3. Type tutorial.java in the Package field.
  4. Type rhapsody.Class in the Type field.
  5. Type qualifiedPath in the Name field.
  6. Click MQL in the Language group.
  7. Click Finish.

A file rhapsody_Class.mqs is created: it allows us to define MQL scripts on the metatype Class. Change its contents to:

package tutorial.java;
metatype rhapsody.Class;

public script qualifiedPath() : String {
return self.getProperty("RHP.SpecificationFilename");
}

Method qualifiedPath is able to retrieve specification name provided by Rational Rhapsody.

The self variable is available in the script contents. This variable is the instance the script is evaluated on (instance of the script's metatype).

The getProperty method is available for all connectors and for all metatype inherited from ModelElement. This may return null if property is undefined or if property name is not added into file "Properties.ini" while using connector "Rational Rhapsody Application".

Use property RHP.ImplementationFilename to get body name (for C++ or Ada).

Note:      Property RHP.SpecificationFilename is set only when Class object should be re-generated under RulesPlayer, but always under RulesComposer.
               Properties RHP.SpecificationFilename and
RHP.ImplementationFilename are available only with connector "Rational Rhapsody On-demand".

 

Now we can update template JavaSource.tgt:

[#package tutorial.java]
[#template public JavaSource(class : rhapsody.Class)]

[#file]
${class.qualifiedPath}[/#file]
[#include JavaDoc()]
public class ${class.name} {
...

Relaunch now the generation and check that Java files are generated in current configuration folder:

               <ProjectDir>/<ComponentDir>/<ConfigurationDir>

Note: please find a full example implementing this solution in RulesComposer samples:

 
              
To add it to your workspace, please:                  
                   Click File > New > Example > RulesComposer Sample > Next
                   Click Rational Rhapsody Code Generation > C++ > Next > Finish

 

 
Prev Section: Deploy the Launch Configuration
Next Section: Either use "On-Demand" or "Application" model reader