Create Communication Diagram using Open API

Communication diagram is a kind of behavior diagram in Unified Modeling Language (UML) which helps in visualize the interactions within and between systems. The Communication diagram focus on how objects collaboration together in particular scenario. Instead of creating communication diagram manually, you can also create it programmatically using Open API in Visual Paradigm’s products.

Creating Communication Diagram

The communication diagram can be created by the DiagramManager, which is obtained from the ApplicationManager.

DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
IDiagramUIModel commDiagram = diagramManager.createDiagram(DiagramManager.DIAGRAM_TYPE_COMMUNICATION_DIAGRAM);

Creating Lifelines

The lifeline representing the object instance in the interaction. Once you have the diagram created, you can then create the lifelines on it. We first have to create its underlying model element using the IModelElementFactory.

IInteractionLifeLine lifeline_model = factory.createInteractionLifeLine();

After that we can specify the properties for the lifeline, such as name, documentation, etc. You can also specify the base classifier of the lifeline. The base classifier can be a simply string value represent its origin, or a class model element in your project)

lifeline_model.setName(%name%);
lifeline_model.setBaseClassifier(%String Value%); // Set base classifier with String value
lifeline_model.setBaseClassifier(IModelElement); // Set base classifier with IModelElement

When the lifeline model is ready, we can then put it into diagram by creating its view (IShapeUIModel) and specify its location.

IShapeUIModel lifeline_shape = (IShapeUIModel) diagramManager.createDiagramElement(commDiagram, lifeline_model);
lifeline_shape.setBounds(x, y, w, h);

Creating Messages

In communication diagram the message is not directly added to the lifelines, but is attached to a link between 2 lifelines. Same as the lifeline, we can ask the IModelElementFactory help us to create the link.

IInteractionLifeLineLink link_model = factory.createInteractionLifeLineLink();

And same as a normal relationship, we then have to specify the from & to end of the link.

link_model.setFrom(from_lifeline_model);link_model.setTo(from_lifeline_model);

Now we can create messages from factory, specify its properties as well as its from and to lifeline, and add it to the link.

IMessage msg = factory.createMessage();
msg.setName(%message_name%);
msg.setSequenceNumber(%sequence_number%); // specify the sequence number of the message
msg.setFrom(link.getFrom());  // obtain the from end element from the link
msg.setTo(link.getTo()); // obtain the to end element from the link
link.addMessage(msg); // add the message to link

Finally we can now create the link connector, the message shape in diagram, and add the message shape as the child of the link connector.

IDiagramElement linkConnector = diagramManager.createConnector(commDiagram, link, from_lifeline_shape, to_lifeline_shape, null);
IShapeUIModel msgShape = (IShapeUIModel) diagramManager.createDiagramElement (commDiagram, msg);
msgShape.setMetaModelElement (msg);
msgShape.setSize ( w, h );
msgShape.setLocation ( x, y );
msgShape.getCaptionUIModel().setBounds(x, y, w, h); // to set the caption position of the message
linkConnector.addChild(msgShape);

Now the diagram is ready and you can ask diagram manager open it for you.

diagramManager.openDiagram(commDiagram);
The generated communication diagram

The generated communication diagram

Sample Plugin

The sample plugin attached in this article demonstrate how you can create the Communication Diagram in your project. After you deploy the plugin into VP applications, you can then click on the plugin button in the application toolbar to trigger it.

Execute Generate Communication Diagram Plugin

Execute Create Communication Diagram Plugin

Download Sample Plugin

You can click this link to download the sample plugin.

Related Know-how

Related Links

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply