Use filenames associated to objects in Rhapsody

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

For each object, 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.ModelElement in the Type field.
  5. Type getPropertyValueByKey in the Name field.
  6. Click Java in the Language group.
  7. Click Finish.

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

package tutorial.java;
 
import java.util.Collection;
import java.util.Iterator;
 
import com.sodius.mdw.metamodel.rhapsody.Property;
import com.sodius.mdw.metamodel.rhapsody.scripts.ClassScriptContainer;
 
public class rhapsody_Class public class ClassScriptContainer
 
     public String getPropertyValueByKey(String key) {
             Collection properties = self.getProperties();
             Iterator it = properties.iterator();
             Property currentProperty = null;
             while ( it.hasNext() ) {
                    currentProperty = (Property) it.next();
                    if ( currentProperty != null && 
                        currentProperty.getKey().equals(key) ) {
                            return currentProperty.getValue();
                    }
             }
             return null;
     }
}

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 method getPropertyValueByKey is able to retrieve properties associated to each object by RulesPLayer/RulesComposer.

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
             .getPropertyValueByKey("RHP.SpecificationFilename");
}

Method qualifiedPath is able to retrieve specification name provided by Rhapsody.
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.

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 Rhapsody Code Generation > C++ > Next > Finish

 

 

Prev Section: Deploy the Launch Configuration