Customize filenames associated to Rational Rhapsody objects

In this step, we will change the Java template to generate Java files in your own project workspace folder.

For each object, we can propose to Rational Rhapsody several files that Rational Rhapsody could be able to bind and edit on user demand.

Typically in Ada, we can propose four files for a Class object: two for specification and body, and two others to regroup nested Port objects.

In our tutorial, we present a case for Java language with a single filename per object.

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.ModelElement in the Type field.
  5. Type rulesFileMapper in the Name field.
  6. Click MQL in the Language group.
  7. Click Finish.

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

package tutorial.java;
metatype rhapsody.ModelElement;
public script
rulesFileMapper() : com.sodius.mdw.rhapsody.api.utils
                                          
.RhapsodyFileMapper {
     var mapper : com.sodius.mdw.rhapsody.api.utils
                           
.RhapsodyFileMapper =
null;
     mapper = self.eMetamodel()
                   
.getModelReaderDescriptor("Rhapsody On-Demand")
                   
.getProperty("interfaceFileMapper");
     // Under RulesComposer, interface is inactive
    
if (mapper == null) {
            
return com.sodius.mdw.rhapsody.api.utils
                    .NullRhapsodyFileMapper.INSTANCE;
     }
     return mapper;
}

Method rulesFileMapper retrieves Rational Rhapsody interface object to send the list of files to Rational Rhapsody.

This interface provides several methods to send filenames to Rational Rhapsody. The filename argument should contain only one file with its absolute path:

 
 String addFileName(rhapsody.ModelElement element, String filename);

 This can be called several times with the same element, but a different filename.

 
 String addMainFileName(rhapsody.ModelElement element, String filename);

             This can be called several times with the same element, but a different filename.

 String addMakeFileName(rhapsody.Configuration element, String filename);

 This should be called only one time per generation for a Rational Rhapsody Configuration object only.

 String addTargetFileName(rhapsody.Configuration element, String filename);

 This should be called only one time per generation for a Rhapsody Configuration object only.

 

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.ModelElement in the Type field.
  5. Type qualifiedPath in the Name field.
  6. Click MQL in the Language group.
  7. Click Finish.

This method is added to file rhapsody_ModelElement.mqs, change its contents to:

public script qualifiedPath() : String {
     return self.rulesFileMapper.addFileName(self,
                            "C:/workspace/Tutorial_Java/src/"+
                           
self.name+".java");
}

Method qualifiedPath computes filename and sends it to the Rational Rhapsody interface.

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} {
...


The relaunch the generation and check that Java files are generated in folder:

             C:/workspace/Tutorial_Java/src

Now, in Rational Rhapsody, click in menu on File > Project properties and select tab Properties.

 
As interface is inactive under RulesComposer, we should deploy your project as a launch configuration under Rational Rhapsody to use it with RulesPlayer.

For details, see chapter Deploy the Launch Configuration.

Change the following properties for your new launch configuration:

lang_CG:Configuration:CodeGeneratorTool = External
lang_CG:Configuration:ExternalGeneratorFileMappingRules = DefinedByGenerator
lang_CG:Configuration:GeneratorRulesSet = JavaGeneration.classpath
lang_CG:Configuration:GeneratorScenarioName = tutorial.java.JavaGeneration.main

Where lang is here JAVA.

Check that Rational Rhapsody displays the message “Loading external generator...”.

Now, you can execute command Code > Re Generate > Entire Project.

When operation is completed, select a Class object and right-click Edit Code. And check that source file is edited in folder   
    C:/workspace/Tutorial_Java/src
.

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 > Java > Next > Finish


Prev Section: Either use “On-Demand” or “Application” model reader
Next Section: Launch evaluation of an external ruleset