Creating Object Diagram using Open API
In Unified Modeling Language the Class Diagram is a typical diagram for model the static structure of a system. The Class Diagram gives developer a clear picture about how the classes are related to each other. But it only model about the static structure, not about the running instance. For the running instance the Class Diagram is realized by the Object Diagram. In this article we will show you how to create object diagram with Open API.
This is the object diagram we are going to create. It only have 2 Instance Specification and a link between them. Each Instance Specification have the slot defined reflect the features on the base classifier.
Create Base Classifiers
Let’s start by create the base classifiers for our Instance Specification. We only need to create them in model repository and there is no need to create them into diagram. We first create the classes.
// create base classifiers Person and its attribute IClass classPerson = IModelElementFactory.instance().createClass(); classPerson.setName("Person"); IAttribute attrPersonName = classPerson.createAttribute(); attrPersonName.setName("name"); attrPersonName.setType("String");// create base classifiers Car and its attributes IClass classCar = IModelElementFactory.instance().createClass(); classCar.setName("Car"); IAttribute attrCarBrand = classCar.createAttribute(); attrCarBrand.setName("brand"); attrCarBrand.setType("String"); IAttribute attrCarType = classCar.createAttribute(); attrCarType.setName("type"); attrCarType.setType("String");
After create the classes we then create an association between the 2 classes.
// create association between Person and Car IAssociation association = IModelElementFactory.instance().createAssociation(); association.setName("owns"); association.setFrom(classPerson); association.setTo(classCar);
Create Blank Object Diagram
Now we are ready to create our Object Diagram. We can use the DiagramManager.createDiagram to create a blank object diagram:
// create blank object diagram DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager(); IObjectDiagramUIModel diagram = (IObjectDiagramUIModel) diagramManager.createDiagram(IDiagramTypeConstants.DIAGRAM_TYPE_OBJECT_DIAGRAM);
Creating Instance Specification and Slots
Next we try to create the Instance Specification.
// create instance specification model for Person IInstanceSpecification objPerson = IModelElementFactory.instance().createInstanceSpecification(); objPerson.setName("John Doe"); // specify the name of the instance specification model objPerson.addClassifier(classPerson); // specify base classifier for the instance specification
Once the Instance Specification is being created, we can then define the slot to represent the features in the Instance Specification.
// create slot for name attribute of Person instance specification ISlot slotPersonName = IModelElementFactory.instance().createSlot(); slotPersonName.setFeature(attrPersonName); // specify the slot is reference to the name attribute of Person class // create value specification for the slot ICompositeValueSpecification valueSpecPersonName = IModelElementFactory.instance().createCompositeValueSpecification(); valueSpecPersonName.setValue("John Doe"); // specify the value slotPersonName.addValue(valueSpecPersonName); // add value specification to slot objPerson.addSlot(slotPersonName); // add the slot to Person instance specification
After the Instance Specification model is created, we can then create a view for it in our diagram.
// create view for instance specification of Person on diagram IInstanceSpecificationUIModel objPersonShape = (IInstanceSpecificationUIModel) diagramManager.createDiagramElement(diagram, objPerson); objPersonShape.setBounds(100, 105, 110, 45); // specify the location for instance specification of Person objPersonShape.setRequestResetCaption(true); // set to automatic calculate the initial caption position
OK, now it’s your turn to create Instance Specification for the Car class.
Creating Link between Instance Specification
After both Instance Specification being created, we can then create a link between them.
// create link model between instance specification of Person and Car ILink link = IModelElementFactory.instance().createLink(); // set the from end to instance specification of Person link.setFrom(objPerson); // set the to end to instance specification of Car link.setTo(objCar); link.addClassifier(association); // set the association as its base classifier// create view of the link in diagram ILinkUIModel linkShape = (ILinkUIModel) diagramManager.createConnector(diagram, link, objPersonShape, objCarShape, new Point[] {new Point(210, 130), new Point(400, 130)}); linkShape.setRequestResetCaption(true);
Show up the diagram
Finally 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 object 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 Links |
Leave a Reply
Want to join the discussion?Feel free to contribute!