IBM Support

How to create an Activity diagram using the Rhapsody API

Question & Answer


Question

How do you create an Activity diagram using the IBM Rational Rhapsody API

Cause

You wish to automate the creation of Activity diagrams

Answer

The code below will create a TokenOrientated Activity diagram for your Rhapsody project consisting of:

  • Diagram Frames
  • Default Transitions
  • Actions
  • Pins
  • Object Flows
  • Control Flows
  • Object Nodes (both custom and existing types)
  • Decision Connector
  • Flow Final
  • Activity Parameters

An Eclipse project is also attached at the bottom for convenience.




import java.util.List;

import com.telelogic.rhapsody.core.IRPApplication;
import com.telelogic.rhapsody.core.IRPClass;
import com.telelogic.rhapsody.core.IRPConnector;
import com.telelogic.rhapsody.core.IRPFlowchart;
import com.telelogic.rhapsody.core.IRPGraphEdge;
import com.telelogic.rhapsody.core.IRPGraphElement;
import com.telelogic.rhapsody.core.IRPGraphNode;
import com.telelogic.rhapsody.core.IRPGraphicalProperty;
import com.telelogic.rhapsody.core.IRPInstance;
import com.telelogic.rhapsody.core.IRPModelElement;
import com.telelogic.rhapsody.core.IRPObjectNode;
import com.telelogic.rhapsody.core.IRPPackage;
import com.telelogic.rhapsody.core.IRPPin;
import com.telelogic.rhapsody.core.IRPState;
import com.telelogic.rhapsody.core.IRPStereotype;
import com.telelogic.rhapsody.core.IRPTransition;
import com.telelogic.rhapsody.core.RhapsodyAppServer;

public class createActivityDiagram {

static IRPFlowchart fc = null;

public static void main(String[] args) {
IRPApplication app = RhapsodyAppServer.getActiveRhapsodyApplication();
IRPPackage pkg = app.activeProject().addPackage("aPackage");
IRPClass cls = pkg.addClass("aClass");
fc = cls.addActivityDiagram();
fc.setPropertyValue("Activity_diagram.Transition.line_style", "rectilinear_arrows");
fc.setPropertyValue("Activity_diagram.DefaultTransition.line_style", "rectilinear_arrows");
IRPStereotype stObjFlow = (IRPStereotype)app.activeProject().findNestedElementRecursive("ObjectFlow", "Stereotype");
////////////////////////////////////
///// ADD DIAGRAM GRAPHICS ////
////////////////////////////////////

// basic action
IRPState action1 = fc.getRootState().addState("Action1");

// object nodes

// custom type
IRPInstance obj1 = (IRPInstance) pkg.addImplicitObject("myObject1");
IRPObjectNode objNode1 = (IRPObjectNode)fc.addNewAggr("ObjectNode", "objNode1");
objNode1.setRepresents(obj1);

// existing type
IRPObjectNode objNode2 = (IRPObjectNode)fc.addNewAggr("ObjectNode", "objNode1");
objNode2.setRepresents(app.activeProject().findNestedElementRecursive("double", "Type"));

// add pins
IRPPin pin1In = (IRPPin)action1.addConnector("InPin");
IRPPin pin1Out = (IRPPin)action1.addConnector("OutPin");

IRPPin pin2In = (IRPPin)objNode1.addConnector("InPin");
IRPPin pin2Out = (IRPPin)objNode1.addConnector("OutPin");

IRPPin pin3In = (IRPPin)objNode2.addConnector("InPin");
IRPPin pin3Out = (IRPPin)objNode2.addConnector("OutPin");

// condition connector; see also Fork, History, Join, and Termination
IRPConnector cond1 = fc.getRootState().addConnector("Condition");

//Action Block - Not compatible with TokenOrientated Activity
//IRPState aBlock1 = fc.getRootState().addState("ActionBlock1");
//aBlock1.setStateType("Block");

// Flow Final
IRPState final1 = fc.getRootState().addState("Final1");
final1.setStateType("FlowFinal");

////////////////////////////////////////
///// CONNECT WITH TRANSITIONS ////
////////////////////////////////////////
//IRPTransition defaultTran = action1.createDefaultTransition(fc.getRootState());


IRPTransition objTran1 = pin1Out.addTransition(pin2In);
IRPTransition objTran2 = pin2Out.addTransition(pin3In);

objTran1.setStereotype(stObjFlow);
objTran2.setStereotype(stObjFlow);

pin3Out.addTransition(cond1);

IRPTransition condTran2 = cond1.addTransition(final1);
IRPTransition condTran1 = cond1.addTransition(pin1In);

// add guards to condition transitions
condTran1.setItsGuard("if x < 10");
condTran2.setItsGuard("else");


////////////////////////////
//// CREATE DIAGRAM ////
////////////////////////////

// automatically create the diagram graphics for the elements added above
fc.createGraphics();

////////////////////////////////////
//// ADJUST GRAPHICS POSITIONS ////
////////////////////////////////////

setPosition(action1, 300, 100);
setPosition(pin1In, 345,0);

setPosition(objNode1, 300, 250);
setPosition(pin2In, 345, 195);

setPosition(objNode2, 300, 400);
setPosition(pin3In, 345, 195);

setPosition(cond1, 300, 550);

setPosition(final1, 300, 700);

////////////////////////////////////
//// POST CREATION GRAPHICS ////
////////////////////////////////////

//enable diagram frame so we can draw to it. This can only be enabled after diagram graphics are created
fc.setShowDiagramFrame(1);

// add activity parameter to frame. This can only be done after the diagram frame is enabled.
IRPPin pinExtIn1 = fc.addActivityParameter("ExternalIn1");

// connect up the frame's activity parameter pin to the diagram's action pin
IRPTransition ObjTran1 = pinExtIn1.addTransition(pin1In);
ObjTran1.setStereotype(stObjFlow);
// because we have already run "crateGraphics" we must draw this transition manually.
fc.addNewEdgeForElement(ObjTran1,(IRPGraphNode)getGraphElement(pinExtIn1), 0, 0, (IRPGraphNode)getGraphElement(pin1In), 0, 0);

setPosition(pinExtIn1, 300, 0);


//listGraphicalProperties(getGraphElement(action1));



}

public static void setPosition(IRPModelElement element, Integer x, Integer y) {

IRPGraphElement g = null;
if ((g = getGraphElement(element)) != null) {
g.setGraphicalProperty("Position", x + ", " + y);
System.out.println (element.getName() + " position at "+ g.getGraphicalProperty("Position").getValue());
}
}


public static IRPGraphElement getGraphElement(IRPModelElement element) {
IRPGraphElement ret = null;

List<IRPGraphElement> gList = fc.getGraphicalElements().toList();
if (!gList.isEmpty()) {
for (IRPGraphElement g : gList) {
if (g.getModelObject() != null) {
if(g.getModelObject().getGUID().equals(element.getGUID())) {

return g;
}
}
}
}


return ret;

}



public static void listGraphicalProperties (IRPGraphElement g) {
List<IRPGraphicalProperty> prpList = g.getAllGraphicalProperties().toList();
for (IRPGraphicalProperty prp : prpList) {
System.out.println(prp.getKey() + " - " + prp.getValue());
}

}

}

Disclaimer

All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

[{"Product":{"code":"SSB2MU","label":"IBM Engineering Systems Design Rhapsody"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"General Information","Platform":[{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"8.0;8.0.1;8.0.2;8.0.3;8.0.4;8.0.5;8.0.6;8.1;8.1.1;8.1.2;8.1.2.1;8.1.3;8.1.4;8.1.5","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

Product Synonym

Rational Rhapsody

Document Information

Modified date:
27 May 2022

UID

swg21988596