Customize filenames associated to IBM Engineering Systems Design Rhapsody - Developer 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 IBM Engineering Systems Design Rhapsody - Developer several files that IBM Engineering Systems Design Rhapsody - Developer 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:
- Click File > New > Script.
- Type JavaGeneration/src in the Source folder field.
- Type tutorial.java in the Package field.
- Type rhapsody.ModelElement in the Type field.
- Type rulesFileMapper in the Name field.
- Click MQL in the Language group.
- 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 IBM Engineering Systems Design Rhapsody - Developer interface object to send the list of files to 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 IBM Engineering Systems Design Rhapsody - Developer. 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 IBM Engineering Systems Design Rhapsody - Developer 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:
- Click File > New > Script.
- Type JavaGeneration/src in the Source folder field.
- Type tutorial.java in the Package field.
- Type rhapsody.ModelElement in the Type field.
- Type qualifiedPath in the Name field.
- Click MQL in the Language group.
- 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 IBM Engineering Systems Design Rhapsody - Developer 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 IBM Engineering Systems Design Rhapsody - Developer, 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 IBM Engineering Systems Design Rhapsody - Developer 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 IBM Engineering Systems Design Rhapsody - Developer 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 IBM Engineering Systems Design Rhapsody - Developer Code Generation > Java > Next > Finish