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:
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.
Singleton com.sodius.mdw.rhapsody.api.utils.NullRhapsodyFileMapper.INSTANCE should be used when the interface is not defined to avoid NullPointerException during evaluation of your ruleset.
This interface provides several methods to send filenames to Rational Rhapsody. The filename argument should contain only one file with its absolute path:
String addMainFileName(rhapsody.ModelElement element, String filename);
This can be called several times with the same element, but a different filename.
To add a script:
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} {
...
Now, in Rational Rhapsody, click in menu on File > Project properties and select tabProperties.
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.
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 with
the RulesPlayer Helper