Create Activity Diagram using Open API
Activity diagram is one of the behavior diagram in Unified Modeling Language (UML) which used to model the flow of control of your system. With the support of model of decision, activity diagram provide graphical representation to present the step-by-step flow of control of your system. Instead of manually create the activity diagram, you can also generate it via Visual Paradigm’s Open API. In this article we will show you how to create activity diagram with Open API.
This is the activity diagram we are going to create. It consist of 1 activity, 7 action node, one initial and one final node, fork & join node, decision and merge node, object node as well as control flows and object flows.
Create Blank Activity Diagram
Let’s start by create a blank diagram. We can use DiagramManager.createDiagram operation to create a blank new diagram.
// create blank activity diagram DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager(); IActivityDiagramUIModel diagram = (IActivityDiagramUIModel) diagramManager.createDiagram(IDiagramTypeConstants.DIAGRAM_TYPE_ACTIVITY_DIAGRAM);
Create the base Activity and Activity Parameter Node
When the diagram is ready we can start to create the elements in the diagram. We first create the Activity.
// create Process Order activity model IActivity activityProcessOrder = IModelElementFactory.instance().createActivity(); // specify the name of the activity model activityProcessOrder.setName("Process Order"); // specify the pre-condition & post-condition activityProcessOrder.setPrecondition("Order complete"); activityProcessOrder.setPostcondition("Order closed"); // set the activity as single execution activityProcessOrder.setSingleExecution(true); // create view for Process Order activity IActivityUIModel activityProcessOrderShape = (IActivityUIModel) diagramManager.createDiagramElement(diagram, activityProcessOrder); // specify its size and position activityProcessOrderShape.setBounds(173, 82, 790, 391); // set the caption placement to top left activityProcessOrderShape.setModelElementNameAlignment(IShapeUIModel.MODEL_ELEMENT_NAME_ALIGNMENT_ALIGN_TOP_LEFT); // set to automatic calculate the initial caption position activityProcessOrderShape.setRequestResetCaption(true);
After we create the activity we then create the parameter node on the activity.
// create Requested Order activity parameter node IActivityParameterNode paramRequestedOrder = IModelElementFactory.instance().createActivityParameterNode(); paramRequestedOrder.setName("Requested Order"); // add the parameter node to Process Order activity activityProcessOrder.addActivityParameterNode(paramRequestedOrder); // create view for Requested Order activity parameter node IActivityParameterNodeUIModel paramRequestedOrderShape = (IActivityParameterNodeUIModel) diagramManager.createDiagramElement(diagram, paramRequestedOrder); paramRequestedOrderShape.setBounds(126, 198, 95, 30); // add the view of Requested Order to Process Order activityProcessOrderShape.addChild(paramRequestedOrderShape); paramRequestedOrderShape.setRequestResetCaption(true);
Create Initial and Final Node
Next we create the initial and final node in the activity.
// create initial node & its view IInitialNode initialNode = IModelElementFactory.instance().createInitialNode(); activityProcessOrder.addChild(initialNode); IInitialNodeUIModel initialNodeShape = (IInitialNodeUIModel) diagramManager.createDiagramElement(diagram, initialNode); initialNodeShape.setBounds(265, 148, 20, 20); activityProcessOrderShape.addChild(initialNodeShape); // create final node & its view IActivityFinalNode finalNode = IModelElementFactory.instance().createActivityFinalNode(); IActivityFinalUIModel finalNodeShape = (IActivityFinalUIModel) diagramManager.createDiagramElement(diagram, finalNode); finalNodeShape.setBounds(885, 267, 20, 20); activityProcessOrderShape.addChild(finalNodeShape);
Create Actions
Now we move on to create the actions.
// create Receive Order action & its view IActivityAction actionReceiveOrder = IModelElementFactory.instance().createActivityAction(); actionReceiveOrder.setName("Receive Order"); activityProcessOrder.addChild(actionReceiveOrder); IActivityActionUIModel actionReceiveOrderShape = (IActivityActionUIModel) diagramManager.createDiagramElement(diagram, actionReceiveOrder); actionReceiveOrderShape.setBounds(255, 195, 60, 40); actionReceiveOrderShape.setRequestResetCaption(true); activityProcessOrderShape.addChild(actionReceiveOrderShape);
Now it’s your turn to create the other actions.
Create Decision and Merge Node
Next we create the decision and merge node.
// create decision node & its view IDecisionNode decision = IModelElementFactory.instance().createDecisionNode(); IDecisionNodeUIModel decisionShape = (IDecisionNodeUIModel) diagramManager.createDiagramElement(diagram, decision); decisionShape.setBounds(373, 195, 20, 40); activityProcessOrderShape.addChild(decisionShape); // create merge node & its view IMergeNode merge = IModelElementFactory.instance().createMergeNode(); IMergeNodeUIModel mergeShape = (IMergeNodeUIModel) diagramManager.createDiagramElement(diagram, merge); mergeShape.setBounds(809, 195, 20, 40); activityProcessOrderShape.addChild(mergeShape);
Create Fork and Join Node
After create the decision and merge node, we then create the fork and join node.
// create fork node & its view IForkNode fork = IModelElementFactory.instance().createForkNode(); IForkNodeUIModel forkShape = (IForkNodeUIModel) diagramManager.createDiagramElement(diagram, fork); forkShape.setBounds(574, 185, 12, 60); activityProcessOrderShape.addChild(forkShape); // create join node & its view IJoinNode join = IModelElementFactory.instance().createJoinNode(); IJoinNodeUIModel joinShape = (IJoinNodeUIModel) diagramManager.createDiagramElement(diagram, join); joinShape.setBounds(756, 185, 12, 60); activityProcessOrderShape.addChild(joinShape);
Create Object Node
The last node we need to create is the object node. To create object node:
// create object node & its view IObjectNode objectNode = IModelElementFactory.instance().createObjectNode(); objectNode.setName("Invoice"); activityProcessOrder.addChild(objectNode); IObjectNodeUIModel objectNodeShape = (IObjectNodeUIModel) diagramManager.createDiagramElement(diagram, objectNode); objectNodeShape.setBounds(333, 407, 80, 40); objectNodeShape.setRequestResetCaption(true); activityProcessOrderShape.addChild(objectNodeShape);
Create Control Flow and Object Flow
Once all the nodes are created we then move to create the connectors. There are 2 kind of connectors used in our diagram, the control flow between actions, initial node and final node, as well as the object flow between actions and object node, as well as parameter node. We start by create control flow between decision and Fill Order action.
// create control flow from decision to Fill Order IControlFlow flowDecisionFillOrder = IModelElementFactory.instance().createControlFlow(); flowDecisionFillOrder.setFrom(decision); flowDecisionFillOrder.setTo(actionFillOrder); // specify guard condition for control flow flowDecisionFillOrder.setGuard("order accepted"); IControlFlowUIModel flowDecisionFillOrderShape = (IControlFlowUIModel) diagramManager.createConnector(diagram, flowDecisionFillOrder, decisionShape, actionFillOrderShape, null); // specify the size and bounds of the control flow caption flowDecisionFillOrderShape.getCaptionUIModel().setBounds(380, 220, 100, 20);
Next we go to create to object flow between parameter node and Receive Order action.
// create object flow from Requested Order to Receive Order IActivityObjectFlow flowRequestedOrderReceivedOrder = IModelElementFactory.instance().createActivityObjectFlow(); // specify the from & to end flowRequestedOrderReceivedOrder.setFrom(paramRequestedOrder); flowRequestedOrderReceivedOrder.setTo(actionReceiveOrder); // create connector into diagram diagramManager.createConnector(diagram, flowRequestedOrderReceivedOrder, paramRequestedOrderShape, actionReceiveOrderShape, new Point[] {new Point(221, 215), new Point(255, 215)});
Again, it’s your turn to create the other connectors in the diagram.
Show up the diagram
When everything is ready we can show up the diagram.
// show up the diagram diagramManager.openDiagram(diagram);
Sample Plugin
The sample plugin attached in this article demonstrate how to create activity diagram using Open API. After you deploy the plugin into Visual Paradigm you can then click the plugin button in the application toolbar to trigger it.
Download Sample Plugin
You can click this link to download the sample plugin.
Related Know-how |
Related Link |
It would be great if this example could also include how to use swimlanes!
Hi John,
Do you mean a plugin sample that demonstrate the manipulation of swimlane with our Open API?
I’m creating an activity diagram from scratch and am trying to add swimlanes in addition to other elements. I’ve figured out how to create the Model Elements for the swimlanes and partitions, but when I try to create Diagram Elements for these, I only end up with a blank box for the Swimlane. I can’t seem to get the partitions to show properly.
Please reference to the article below about how to create swimlane using Open API.
https://knowhow.visual-paradigm.com/openapi/activity-diagram-swimlane/