Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Applying stereotypes programmatically
Applying stereotypes programmatically [message #810669] Thu, 01 March 2012 11:57 Go to next message
Martin is currently offline MartinFriend
Messages: 35
Registered: July 2009
Member
Hi, I have a problem applying stereotypes programmatically. I get a
java.lang.IllegalArgumentException.

Here are is my output:
----------------------
org.eclipse.uml2.uml.internal.impl.StereotypeImpl@4b73469c (name:
Context2, visibility: <unset>) (isLeaf: false, isAbstract: false)
(isActive: false)
org.eclipse.uml2.uml.internal.impl.StereotypeImpl@4b73469c (name:
Context2, visibility: <unset>) (isLeaf: false, isAbstract: false)
(isActive: false)
Exception in thread "main" java.lang.IllegalArgumentException:
org.eclipse.uml2.uml.internal.impl.StereotypeImpl@4b73469c (name:
Context2, visibility: <unset>) (isLeaf: false, isAbstract: false)
(isActive: false)
at
org.eclipse.uml2.uml.internal.operations.ElementOperations.applyStereotype(ElementOperations.java:1413)
at
org.eclipse.uml2.uml.internal.impl.ElementImpl.applyStereotype(ElementImpl.java:510)
at Test2.main(Test2.java:32)
----------------------

Here is my Java-file: (Test2.java)
----------------------
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;


public class Test2 {
public static void main(String[] args) {
UMLPackage.eINSTANCE.eClass();
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
m.put("http://www.eclipse.org/uml2/2.0.0/UML", UMLPackage.eINSTANCE);
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource =
resourceSet.getResource(URI.createURI("testm.uml"), true);
Model uml = (Model) resource.getContents().get(0);
Profile p = (Profile) uml.getAllAppliedProfiles().get(0);
Class system = (Class) uml.getOwnedElements().get(0);
Stereotype s = system.getAppliedStereotypes().get(0);
System.out.println(s);
Class c = uml.createOwnedClass("RVM", true);
Stereotype st = p.getOwnedStereotype("Context2");
System.out.println(st);
c.applyStereotype(st);
}
}
----------------------

Here is my UML model: (testm.uml)
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xmlns:profile="http:///schemas/profile/_G0T9cGOCEeGrzYhqs1PliA/0"
xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML"
xsi:schemaLocation="http:///schemas/profile/_G0T9cGOCEeGrzYhqs1PliA/0
testp2.profile.uml#_G0Xn0WOCEeGrzYhqs1PliA">
<uml:Model xmi:id="_PjiXYGOCEeGrzYhqs1PliA" name="model">
<packagedElement xmi:type="uml:Class"
xmi:id="_QAXdUGOCEeGrzYhqs1PliA" name="ClassA"/>
<profileApplication xmi:id="_SelMMGOCEeGrzYhqs1PliA">
<eAnnotations xmi:id="_Sft0oGOCEeGrzYhqs1PliA"
source="http://www.eclipse.org/uml2/2.0.0/UML">
<references xmi:type="ecore:EPackage"
href="testp2.profile.uml#_G0Xn0WOCEeGrzYhqs1PliA"/>
</eAnnotations>
<appliedProfile href="testp2.profile.uml#__4KDoGOBEeGrzYhqs1PliA"/>
</profileApplication>
</uml:Model>
<profile:Context2 xmi:id="_Tg7aAGOCEeGrzYhqs1PliA"
base_Class="_QAXdUGOCEeGrzYhqs1PliA"/>
</xmi:XMI>
---------------------

Here is my profile: (testp2.profile.uml)
-------------------
<?xml version="1.0" encoding="UTF-8"?>
<uml:Profile xmi:version="2.1"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML"
xmi:id="__4KDoGOBEeGrzYhqs1PliA" name="profile"
metaclassReference="_EErucGOCEeGrzYhqs1PliA">
<eAnnotations xmi:id="_G0Xn0GOCEeGrzYhqs1PliA"
source="http://www.eclipse.org/uml2/2.0.0/UML">
<contents xmi:type="ecore:EPackage"
xmi:id="_G0Xn0WOCEeGrzYhqs1PliA" name="profile"
nsURI="http:///schemas/profile/_G0T9cGOCEeGrzYhqs1PliA/0"
nsPrefix="profile">
<eAnnotations xmi:id="_G0ZdAGOCEeGrzYhqs1PliA"
source="PapyrusVersion">
<details xmi:id="_G0ZdAWOCEeGrzYhqs1PliA" key="Version"
value="0.0.1"/>
<details xmi:id="_G0ZdAmOCEeGrzYhqs1PliA" key="Comment" value=""/>
<details xmi:id="_G0ZdA2OCEeGrzYhqs1PliA" key="Copyright"
value=""/>
<details xmi:id="_G0ZdBGOCEeGrzYhqs1PliA" key="Date"
value="2012-03-01"/>
<details xmi:id="_G0ZdBWOCEeGrzYhqs1PliA" key="Author" value=""/>
</eAnnotations>
<eClassifiers xmi:type="ecore:EClass"
xmi:id="_G0Xn0mOCEeGrzYhqs1PliA" name="Context2">
<eAnnotations xmi:id="_G0Xn02OCEeGrzYhqs1PliA"
source="http://www.eclipse.org/uml2/2.0.0/UML"
references="_BfiRIGOCEeGrzYhqs1PliA"/>
<eStructuralFeatures xmi:type="ecore:EReference"
xmi:id="_G0Xn1GOCEeGrzYhqs1PliA" name="base_Class" ordered="false"
lowerBound="1">
<eType xmi:type="ecore:EClass"
href="http://www.eclipse.org/uml2/3.0.0/UML#//Class"/>
</eStructuralFeatures>
</eClassifiers>
</contents>
</eAnnotations>
<elementImport xmi:id="_EErucGOCEeGrzYhqs1PliA" alias="Class">
<importedElement xmi:type="uml:Class"
href="pathmap://UML_METAMODELS/UML.metamodel.uml#Class"/>
</elementImport>
<packagedElement xmi:type="uml:Stereotype"
xmi:id="_BfiRIGOCEeGrzYhqs1PliA" name="Context2">
<ownedAttribute xmi:id="_FNRHIGOCEeGrzYhqs1PliA" name="base_Class"
association="_FNRHIWOCEeGrzYhqs1PliA">
<type xmi:type="uml:Class"
href="pathmap://UML_METAMODELS/UML.metamodel.uml#Class"/>
</ownedAttribute>
</packagedElement>
<packagedElement xmi:type="uml:Extension"
xmi:id="_FNRHIWOCEeGrzYhqs1PliA" name="E_Context2_Class0"
memberEnd="_FNRHImOCEeGrzYhqs1PliA _FNRHIGOCEeGrzYhqs1PliA">
<ownedEnd xmi:type="uml:ExtensionEnd"
xmi:id="_FNRHImOCEeGrzYhqs1PliA" name="extension_Context2"
type="_BfiRIGOCEeGrzYhqs1PliA" aggregation="composite"
association="_FNRHIWOCEeGrzYhqs1PliA"/>
</packagedElement>
</uml:Profile>
-------------------

Thanks in advance.
Re: Applying stereotypes programmatically [message #810740 is a reply to message #810669] Thu, 01 March 2012 13:50 Go to previous messageGo to next message
Martin is currently offline MartinFriend
Messages: 35
Registered: July 2009
Member
I solved the problem by adding this:

URI baseUri =
URI.createURI("jar:file:/C:<PATH>\\org.eclipse.uml2.uml.resources-3.1.0.v201005031530.jar!/");
URIConverter.URI_MAP.put(URI.createURI( UMLResource.LIBRARIES_PATHMAP ),
baseUri.appendSegment( "libraries" ).appendSegment( "" ));
URIConverter.URI_MAP.put(URI.createURI( UMLResource.METAMODELS_PATHMAP
), baseUri.appendSegment( "metamodels" ).appendSegment( "" ));
URIConverter.URI_MAP.put(URI.createURI( UMLResource.PROFILES_PATHMAP ),
baseUri.appendSegment( "profiles" ).appendSegment( "" ));

On 01.03.2012 12:57, Martin wrote:
> Hi, I have a problem applying stereotypes programmatically. I get a
> java.lang.IllegalArgumentException.
>
> Here are is my output:
> ----------------------
> org.eclipse.uml2.uml.internal.impl.StereotypeImpl@4b73469c (name:
> Context2, visibility: <unset>) (isLeaf: false, isAbstract: false)
> (isActive: false)
> org.eclipse.uml2.uml.internal.impl.StereotypeImpl@4b73469c (name:
> Context2, visibility: <unset>) (isLeaf: false, isAbstract: false)
> (isActive: false)
> Exception in thread "main" java.lang.IllegalArgumentException:
> org.eclipse.uml2.uml.internal.impl.StereotypeImpl@4b73469c (name:
> Context2, visibility: <unset>) (isLeaf: false, isAbstract: false)
> (isActive: false)
> at
> org.eclipse.uml2.uml.internal.operations.ElementOperations.applyStereotype(ElementOperations.java:1413)
>
> at
> org.eclipse.uml2.uml.internal.impl.ElementImpl.applyStereotype(ElementImpl.java:510)
>
> at Test2.main(Test2.java:32)
> ----------------------
>
> Here is my Java-file: (Test2.java)
> ----------------------
> import java.util.Map;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.uml2.uml.Class;
> import org.eclipse.uml2.uml.Model;
> import org.eclipse.uml2.uml.Profile;
> import org.eclipse.uml2.uml.Stereotype;
> import org.eclipse.uml2.uml.UMLPackage;
> import org.eclipse.uml2.uml.resource.UMLResource;
>
>
> public class Test2 {
> public static void main(String[] args) {
> UMLPackage.eINSTANCE.eClass();
> Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
> Map<String, Object> m = reg.getExtensionToFactoryMap();
> m.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
> m.put("http://www.eclipse.org/uml2/2.0.0/UML", UMLPackage.eINSTANCE);
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.getResource(URI.createURI("testm.uml"),
> true);
> Model uml = (Model) resource.getContents().get(0);
> Profile p = (Profile) uml.getAllAppliedProfiles().get(0);
> Class system = (Class) uml.getOwnedElements().get(0);
> Stereotype s = system.getAppliedStereotypes().get(0);
> System.out.println(s);
> Class c = uml.createOwnedClass("RVM", true);
> Stereotype st = p.getOwnedStereotype("Context2");
> System.out.println(st);
> c.applyStereotype(st);
> }
> }
> ----------------------
>
> Here is my UML model: (testm.uml)
> ---------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <xmi:XMI xmi:version="2.1"
> xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
> xmlns:profile="http:///schemas/profile/_G0T9cGOCEeGrzYhqs1PliA/0"
> xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML"
> xsi:schemaLocation="http:///schemas/profile/_G0T9cGOCEeGrzYhqs1PliA/0
> testp2.profile.uml#_G0Xn0WOCEeGrzYhqs1PliA">
> <uml:Model xmi:id="_PjiXYGOCEeGrzYhqs1PliA" name="model">
> <packagedElement xmi:type="uml:Class" xmi:id="_QAXdUGOCEeGrzYhqs1PliA"
> name="ClassA"/>
> <profileApplication xmi:id="_SelMMGOCEeGrzYhqs1PliA">
> <eAnnotations xmi:id="_Sft0oGOCEeGrzYhqs1PliA"
> source="http://www.eclipse.org/uml2/2.0.0/UML">
> <references xmi:type="ecore:EPackage"
> href="testp2.profile.uml#_G0Xn0WOCEeGrzYhqs1PliA"/>
> </eAnnotations>
> <appliedProfile href="testp2.profile.uml#__4KDoGOBEeGrzYhqs1PliA"/>
> </profileApplication>
> </uml:Model>
> <profile:Context2 xmi:id="_Tg7aAGOCEeGrzYhqs1PliA"
> base_Class="_QAXdUGOCEeGrzYhqs1PliA"/>
> </xmi:XMI>
> ---------------------
>
> Here is my profile: (testp2.profile.uml)
> -------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <uml:Profile xmi:version="2.1"
> xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
> xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML"
> xmi:id="__4KDoGOBEeGrzYhqs1PliA" name="profile"
> metaclassReference="_EErucGOCEeGrzYhqs1PliA">
> <eAnnotations xmi:id="_G0Xn0GOCEeGrzYhqs1PliA"
> source="http://www.eclipse.org/uml2/2.0.0/UML">
> <contents xmi:type="ecore:EPackage" xmi:id="_G0Xn0WOCEeGrzYhqs1PliA"
> name="profile" nsURI="http:///schemas/profile/_G0T9cGOCEeGrzYhqs1PliA/0"
> nsPrefix="profile">
> <eAnnotations xmi:id="_G0ZdAGOCEeGrzYhqs1PliA" source="PapyrusVersion">
> <details xmi:id="_G0ZdAWOCEeGrzYhqs1PliA" key="Version" value="0.0.1"/>
> <details xmi:id="_G0ZdAmOCEeGrzYhqs1PliA" key="Comment" value=""/>
> <details xmi:id="_G0ZdA2OCEeGrzYhqs1PliA" key="Copyright" value=""/>
> <details xmi:id="_G0ZdBGOCEeGrzYhqs1PliA" key="Date" value="2012-03-01"/>
> <details xmi:id="_G0ZdBWOCEeGrzYhqs1PliA" key="Author" value=""/>
> </eAnnotations>
> <eClassifiers xmi:type="ecore:EClass" xmi:id="_G0Xn0mOCEeGrzYhqs1PliA"
> name="Context2">
> <eAnnotations xmi:id="_G0Xn02OCEeGrzYhqs1PliA"
> source="http://www.eclipse.org/uml2/2.0.0/UML"
> references="_BfiRIGOCEeGrzYhqs1PliA"/>
> <eStructuralFeatures xmi:type="ecore:EReference"
> xmi:id="_G0Xn1GOCEeGrzYhqs1PliA" name="base_Class" ordered="false"
> lowerBound="1">
> <eType xmi:type="ecore:EClass"
> href="http://www.eclipse.org/uml2/3.0.0/UML#//Class"/>
> </eStructuralFeatures>
> </eClassifiers>
> </contents>
> </eAnnotations>
> <elementImport xmi:id="_EErucGOCEeGrzYhqs1PliA" alias="Class">
> <importedElement xmi:type="uml:Class"
> href="pathmap://UML_METAMODELS/UML.metamodel.uml#Class"/>
> </elementImport>
> <packagedElement xmi:type="uml:Stereotype"
> xmi:id="_BfiRIGOCEeGrzYhqs1PliA" name="Context2">
> <ownedAttribute xmi:id="_FNRHIGOCEeGrzYhqs1PliA" name="base_Class"
> association="_FNRHIWOCEeGrzYhqs1PliA">
> <type xmi:type="uml:Class"
> href="pathmap://UML_METAMODELS/UML.metamodel.uml#Class"/>
> </ownedAttribute>
> </packagedElement>
> <packagedElement xmi:type="uml:Extension"
> xmi:id="_FNRHIWOCEeGrzYhqs1PliA" name="E_Context2_Class0"
> memberEnd="_FNRHImOCEeGrzYhqs1PliA _FNRHIGOCEeGrzYhqs1PliA">
> <ownedEnd xmi:type="uml:ExtensionEnd" xmi:id="_FNRHImOCEeGrzYhqs1PliA"
> name="extension_Context2" type="_BfiRIGOCEeGrzYhqs1PliA"
> aggregation="composite" association="_FNRHIWOCEeGrzYhqs1PliA"/>
> </packagedElement>
> </uml:Profile>
> -------------------
>
> Thanks in advance.
Re: Applying stereotypes programmatically [message #1028039 is a reply to message #810740] Wed, 27 March 2013 19:03 Go to previous message
Yousr CHELLY is currently offline Yousr CHELLYFriend
Messages: 4
Registered: March 2013
Junior Member
thanks martin i used your code and it works like maaaagic Smile Smile Smile
Previous Topic:Referencing UML profiles and schemaLocation
Next Topic:UMLModeler class and shared elements
Goto Forum:
  


Current Time: Wed Apr 24 23:33:59 GMT 2024

Powered by FUDForum. Page generated in 0.03053 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top