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 Rhapsody several files that Rhapsody could be able to bind and edit on user demand.
Typically in
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 Rhapsody interface object to send the list of files to Rhapsody.
This interface provides several methods to send filenames to 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 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:
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 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 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 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 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
Rhapsody Code Generation > Java > Next > Finish
Prev Section: Either use “On-Demand” or “Application” model reader