Question & Answer
Question
How do you use New Term stereotypes with the IBM Rational Rhapsody API
Cause
Unable to create or access elements using the new term stereotype name
Answer
Here we will use the example of SysML constraint Blocks. Note you must be using the SysML profile with this example. See help for adding a profile to your project.
Since Constraint Blocks are a new term stereotype applied to Classes, you need to add a class and then apply the "ConstraintBlock" stereotype to that class. This is the same process for creating any non-UML type, including blocks. You can confirm this by checking the specific stereotype in the SysML profile.
The new term value is set to the UserDefinedMetaClass property of each element it is applied to.
Therefore, to retrieve the Constraint Blocks you must first retrieve all the classes, then use a condition on the UserDefinedMetaClass method to filter out the Constraint Blocks in the results.
Example Java code would be:
// create constraint blocks
IRPApplication app = RhapsodyAppServer.getActiveRhapsodyApplication();
IRPStereotype cbSType = (IRPStereotype)app.activeProject().findNestedElementRecursive("ConstraintBlock", "Stereotype");
IRPPackage pkg = app.activeProject().addPackage("TestPkg");
IRPClass cb1 = (IRPClass)pkg.addNewAggr("Class", "Test");
if(cbSType != null) {
cb1.setStereotype(cbSType);
}
// OR
IRPClass cb2 = (IRPClass)pkg.addNewAggr("Class", "Test2");
cb2.addStereotype("ConstraintBlock", "Class");
/// retrieve the constraint blocks
List<IRPModelElement> cbs = app.activeProject().getNestedElementsByMetaClass("Class", 1).toList();
for (IRPModelElement el : cbs) {
if(el.getUserDefinedMetaClass().equals("ConstraintBlock")) {
System.out.println(el.getName() + " - " + el.getUserDefinedMetaClass());
}
}
Product Synonym
Rational Rhapsody
Was this topic helpful?
Document Information
Modified date:
27 May 2022
UID
swg21969498