Question & Answer
Question
How can you programmatically populate an Object diagram and show InstanceValues for the Slots of the InstanceSpecifications?
Cause
Properties of StructuralFeatures seen in Class Diagrams are represented by Slots in InstanceSpecifications with Object Diagrams.
In order to add different sorts of Values to the Slots you need to use the following method of the Interface Slot:
ValueSpecification createValue(java.lang.String name,
Type type,
org.eclipse.emf.ecore.EClass eClass)
In order to use this method, you must specify the EClass of the specific type of Value that you intend to create.
The available Value classes are the SubInterfaces of ValueSpecification, which you can find in the online help:
Eclipse documentation -> UML2 Developer Guide -> Reference->API Reference -> org.eclipse.uml2.uml -> Value Specification
org.eclipse.uml2.uml
Interface ValueSpecification
All Known Subinterfaces:
Duration, DurationInterval, Expression, InstanceValue, Interval,
LiteralBoolean, LiteralInteger, LiteralNull, LiteralSpecification,
LiteralString, LiteralUnlimitedNatural, OpaqueExpression,
StringExpression, TimeExpression, TimeInterval
In order to get the EClass reference, you use the method:
UMLPackage.eINSTANCE.getXXX()
where XXX is the desired subclass of ValueSpecification.
Typically, you will have to downcast the result of
createValue
to the same subclass of ValueSpecification.
Answer
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. |
|---|
The pluglet requires that you select one or more Packages in Project Explorer.
The pluglet will add an Object Diagram to each selected Package and save the model.
In each Object Diagram you will see the equivalent of performing the following manual operations:
- Create Class Class1
- Add three attributes called Attribute1, Attribute2, Attribute3 having the UML Primitive Types: Integer, Boolean, String
- Create Class Class2
- Draw a bidirectional association between Class1 and Class2
- Create an Object Diagram
- Drop Class1 and Class2 on the Object diagram
- Draw a link between them
- Set Values for the Slots of Attribute1, Attribute2, Attribute3
To use the following pluglet, follow the steps described in technote 1382031.
| Pluglet: CreateObjectDiagramPluglet |
| package com.ibm.rational.support; import java.io.IOException; import java.util.List; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.transaction.RecordingCommand; import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.gmf.runtime.diagram.ui.services.layout.LayoutType; import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.gmf.runtime.notation.Edge; import org.eclipse.gmf.runtime.notation.Node; import org.eclipse.uml2.uml.AggregationKind; import org.eclipse.uml2.uml.Association; import org.eclipse.uml2.uml.Class; import org.eclipse.uml2.uml.InstanceSpecification; import org.eclipse.uml2.uml.InstanceValue; import org.eclipse.uml2.uml.LiteralBoolean; import org.eclipse.uml2.uml.LiteralInteger; import org.eclipse.uml2.uml.LiteralString; import org.eclipse.uml2.uml.Package; import org.eclipse.uml2.uml.PrimitiveType; import org.eclipse.uml2.uml.Property; import org.eclipse.uml2.uml.Slot; import org.eclipse.uml2.uml.Type; import org.eclipse.uml2.uml.UMLPackage; import org.eclipse.uml2.uml.internal.impl.InstanceSpecificationImpl; import com.ibm.xtools.modeler.ui.UMLModeler; import com.ibm.xtools.pluglets.Pluglet; import com.ibm.xtools.umlnotation.UMLDiagramKind; public class CreateObjectDiagramPluglet extends Pluglet { /** * Create one Object diagram inside each selected package */ public void plugletmain(String[] args) { /* Perform remaining work within a Runnable */ try { // String undoLabel = "Manipulating Model"; TransactionalEditingDomain editDomain = UMLModeler .getEditingDomain(); editDomain.getCommandStack().execute( new RecordingCommand(editDomain) { protected void doExecute() { // Get selection final List<EObject> elements = UMLModeler.getUMLUIHelper() .getSelectedElements(); if (elements.size() == 0) { out.println("Cannot perform enumeration on current selection.\nPlease select a Package from the Project Explorer"); //$NON-NLS-1$ } else{ for(EObject eo:elements){ if(eo instanceof Package){ createObjectDiagram((Package) eo); try { UMLModeler.saveModelResource((Package)eo); } catch (IOException e) { out.println(e); } } } } } }); } catch (IllegalStateException e) { out.println("The operation was interrupted"); //$NON-NLS-1$ } } private void createObjectDiagram(Package m) { // create a class with various attributes Class c1 = (Class) m.createPackagedElement("Class1", UMLPackage.eINSTANCE.getClass_()); PrimitiveType ptInteger=(PrimitiveType) m.getMember("Integer"); Property prop00= c1.createOwnedAttribute("Attribute1",ptInteger); PrimitiveType ptBoolean=(PrimitiveType) m.getMember("Boolean"); Property prop01= c1.createOwnedAttribute("Attribute2",ptBoolean); PrimitiveType ptString=(PrimitiveType) m.getMember("String"); Property prop02= c1.createOwnedAttribute("Attribute3",ptString); //Create another class Class c2 = (Class) m.createPackagedElement("Class2", UMLPackage.eINSTANCE.getClass_()); //Create an association between the two classes String end1Name="class1"; int end1Lower=0; int end1Upper=1; Type end1Type=c1; String end2Name="class2"; int end2Lower=0; int end2Upper=1; Association assoc=c2.createAssociation(true, AggregationKind.NONE_LITERAL, end1Name, end1Lower, end1Upper, end1Type, true, AggregationKind.NONE_LITERAL, end2Name, end2Lower, end2Upper); //Create Class InstanceSpecifications //c1Instance InstanceSpecification c1Instance = (InstanceSpecificationImpl) m .createPackagedElement("Class1Instance", UMLPackage.eINSTANCE .getInstanceSpecification()); c1Instance.getClassifiers().add(c1); //Slots for c1Instance Slot c1Slot1=c1Instance.createSlot(); Property prop1=c1.getAttribute("class2", c2); c1Slot1.setDefiningFeature(prop1); InstanceValue ivc1Slot1=(InstanceValue)c1Slot1.createValue("", null, UMLPackage.eINSTANCE .getInstanceValue()); Slot c1Slot2=c1Instance.createSlot(); c1Slot2.setDefiningFeature(prop00); LiteralInteger ivc1Slot2=(LiteralInteger)c1Slot2.createValue("", ptInteger, UMLPackage.eINSTANCE.getLiteralInteger()); ivc1Slot2.setValue(10); Slot c1Slot3=c1Instance.createSlot(); c1Slot3.setDefiningFeature(prop01); LiteralBoolean ivc1Slot3=(LiteralBoolean)c1Slot3.createValue("", ptBoolean, UMLPackage.eINSTANCE.getLiteralBoolean()); ivc1Slot3.setValue(true); Slot c1Slot4=c1Instance.createSlot(); c1Slot4.setDefiningFeature(prop02); LiteralString ivc1Slot4=(LiteralString)c1Slot4.createValue("", ptString, UMLPackage.eINSTANCE.getLiteralString()); ivc1Slot4.setValue("One String"); //c2Instance InstanceSpecification c2Instance = (InstanceSpecificationImpl) m .createPackagedElement("Class2Instance", UMLPackage.eINSTANCE .getInstanceSpecification()); c2Instance.getClassifiers().add(c2); Slot c2Slot1=c2Instance.createSlot(); Property prop2=c2.getAttribute("class1", c1); c2Slot1.setDefiningFeature(prop2); InstanceValue ivc2Slot1=(InstanceValue)c2Slot1.createValue("", null, UMLPackage.eINSTANCE .getInstanceValue()); //set instances for the Values ivc1Slot1.setInstance(c2Instance); ivc2Slot1.setInstance(c1Instance); //Create Association InstanceSpecifocation InstanceSpecification assocInstance = (InstanceSpecificationImpl) m .createPackagedElement("Assoc", UMLPackage.eINSTANCE .getInstanceSpecification()); assocInstance.getClassifiers().add(assoc); Slot slot1=assocInstance.createSlot(); slot1.setDefiningFeature(prop1); slot1.setOwningInstance(assocInstance); InstanceValue iv1=(InstanceValue)slot1.createValue("", null, UMLPackage.eINSTANCE .getInstanceValue()); iv1.setInstance(c2Instance); Slot slot2=assocInstance.createSlot(); slot2.setDefiningFeature(prop2); slot2.setOwningInstance(assocInstance); InstanceValue iv2=(InstanceValue)slot2.createValue("", null, UMLPackage.eINSTANCE .getInstanceValue()); iv2.setInstance(c1Instance); //Create an Object Diagram Diagram d = UMLModeler.getUMLDiagramHelper().createDiagram(m, UMLDiagramKind.OBJECT_LITERAL); d.setName("My Object diagram"); //Create nodes to represent Class Instances Node vv1 = UMLModeler.getUMLDiagramHelper().createNode(d,c1Instance); Node vv2 = UMLModeler.getUMLDiagramHelper().createNode(d,c2Instance); //Create an Edge to represent the Association Instance Edge vv3 = UMLModeler.getUMLDiagramHelper().createEdge(vv2, vv1, assocInstance); // layout the diagram UMLModeler.getUMLDiagramHelper().layoutNodes(d.getChildren(), LayoutType.DEFAULT); UMLModeler.getUMLDiagramHelper().openDiagramEditor(d); } } |
The following result will be shown in the model:
Was this topic helpful?
Document Information
Modified date:
29 September 2018
UID
swg21445863