Home » Modeling » UML2 » getAppliedStereotypes is empty but the stereotype has been applied
getAppliedStereotypes is empty but the stereotype has been applied [message #1586406] |
Mon, 26 January 2015 21:08 |
Ernesto Posse Messages: 438 Registered: March 2011 |
Senior Member |
|
|
I'm trying to get a standalone application to load UML models which have a profile applied (UMLRealTime), and while I am able to initialize the relevant registries and load a model, I see that the stereotypes are not applied, or they appear as not being applied.
I'm attaching the sample main java file (ProfileTest.java), the sample model I'm trying to load (EmptyCapsule.uml) and the profile definition (uml-rt.profile.uml).
The sample model consists of a unique element (a Class) which has been applied the "Capsule" stereotype. The XMI shows both the profile application and the stereotype application.
However, upon loading the model, whenever I do
element.getAppliedStereotypes()
I get an empty list.
Stepping through the execution, I see that at some point,
UMLUtil.getProfile(pkg, ctx)
gets invoked where pkg is the UMLRealTimePackage instance. This in turn invokes
resourceSet.getEObject(location, ...)
where location is "pathmap://UML_RT_PROFILE/uml-rt.profile.uml#UMLRealTime", which seems correct (the name of the root element in the profile is 'UMLRealTime').
Eventually this leads to
org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.getEObjectByID(id)
where id is "UMLRealTime". This method looks into its "idToEObjectMap" for the id, but the id is not there, which means that the call UMLUtil.getProfile(pkg, ctx) returns null, leading to an empty list.
So why is this happening? Why does 'getProfile' fail to find the profile? What am I missing?
Any clues are appreciated. Thanks.
[Updated on: Tue, 27 January 2015 21:44] Report message to a moderator
|
|
|
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1588678 is a reply to message #1586406] |
Wed, 28 January 2015 02:55 |
|
Hi, Ernesto,
The URI with which the UML API is trying to locate the UML definition
of the profile is invalid:
"pathmap://UML_RT_PROFILE/uml-rt.profile.uml#UMLRealTime" has the wrong
fragment. The XMI ID of the profile element is
"_1h74oEeVEeO0lv5O1DTHOQ", so that needs to be the URI fragment.
Change line 33 of your ProfileTest.java from
static String UMLRT_ROOT_URI = UMLRT_PATHMAP + "#UMLRealTime";
to
static String UMLRT_ROOT_URI = UMLRT_PATHMAP + "#_1h74oEeVEeO0lv5O1DTHOQ";
And it should work better for you. (I haven't tested it)
HTH,
Christian
On 2015-01-26 21:08:46 +0000, Ernesto Posse said:
> I'm trying to get a standalone application to load UML models which
> have a profile applied (UMLRealTime), and while I am able to initialize
> the relevant registries and load a model, I see that the stereotypes
> are not applied, or they appear as not being applied.
>
> I'm attaching the sample main java file (ProfileTest.java), the sample
> model I'm trying to load (EmptyCapsule.uml) and the profile definition
> (uml-rt.profile.uml).
>
> The sample model consists of a unique element (a Class) which has been
> applied the "Capsule" stereotype. The XMI shows both the profile
> application and the stereotype application.
>
> However, upon loading the model, whenever I do
>
> element.getAppliedStereotypes()
>
> I get an empty list.
>
> Stepping through the execution, I see that at some point,
>
> UMLUtil.getProfile(pkg, ctx)
>
> gets invoked where pkg is the UMLRealTimePackage instance. This in turn invokes
>
> resourceSet.getEObject(location, ...)
>
> where location is
> "pathmap://UML_RT_PROFILE/uml-rt.profile.uml#UMLRealTime", which seems
> correct (the name of the root element in the profile is 'UMLRealTime').
>
> Eventually this leads to
>
> [code]org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.getEObjectByID(id)/code]
>
> where id is "UMLRealTime". This method looks into its "idToEObjectMap"
> for the id, but the id is not there, which means that the call
> UMLUtil.getProfile(pkg, ctx) returns null, leading to an empty list.
>
> So why is this happening? Why does 'getProfile' fail to find the
> profile? What am I missing?
>
> Any clues are appreciated. Thanks.
>
>
>
>
>
> <image><image><image>
|
|
| |
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1589889 is a reply to message #1589703] |
Wed, 28 January 2015 18:18 |
|
Hi, Ernesto,
That is how every HREF URI works in EMF's resources: the resource URI
plus a fragment provided by the resource. For some resources, this can
be an ID, for some a segmented path, and for others something
completely different. The EMF API documentation covers it pretty well,
as do probably lots of article on-line.
Cheers,
Christian
On 2015-01-28 15:55:30 +0000, Ernesto Posse said:
> Yes! It works! Thanks Christian.
>
> I didn't know that you had to use the root fragment and that it had to
> be the id rather than the symbolic name. Is this documented, if so
> where?
|
|
| |
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1590279 is a reply to message #1590147] |
Wed, 28 January 2015 23:22 |
|
Hi, Ernesto,
Ah, I see. Right, the problem here is that the URI was in the profile
location map. Indeed, that API is not much documented (is the
org.eclipse.uml2.uml.dynamic_package extension point documented that
corresponds to this API? I'm not sure)
I think the best documentation in this case is the examples of profile
registrations in the plugin.xml of the org.eclipse.uml2.uml.resources
plug-in, itself. :-)
Sorry that I misunderstood your question.
Christian
On 2015-01-28 21:41:14 +0000, Ernesto Posse said:
> Thanks Christian.
>
> Sorry if it appears as I didn't read the docs, but it doesn't strike me
> as obvious.
>
> The fact that I had to use that when calling
>
> UMLPlugin.getEPackageNsURIToProfileLocationMap()
>
> doesn't seem obvious to me. At least UMLPlugin doesn't provide
> documentation about it. And I didn't see anything about it on
> https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles
>
> or anywhere on
>
> https://wiki.eclipse.org/MDT/UML2
>
> I was aware of the general form of URIs, and I've seen tutorials on
> loading EMF resources, but I still haven't found a place where it says
> that to load a UML2 resource with a profile, the "Profile location" in
> the map from EPackage NsURI to profile location has to provide the URI
> of the profile with the id of the root in it. I'm not sure that's
> obvious.
> Anyway, thank you very much for your help.
|
|
| |
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1752622 is a reply to message #1752312] |
Wed, 25 January 2017 15:47 |
|
I don't see any use of resources nor resource sets in your code attachment, so I don't know whether you are loading resources into a resource set. That is important for resolution of dependencies between resources and especially for tracing from the Ecore representation of a static profile such as SysML to its UML representation and vice versa.
In general, you should edit UML models in a ResourceSet for the most complete and correct support for profiles.
HTH,
Christian
|
|
| | |
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1753166 is a reply to message #1753092] |
Thu, 02 February 2017 11:04 |
German Vega Messages: 104 Registered: December 2015 Location: Grenoble, France |
Senior Member |
|
|
Hello
In your previous post you say :
Quote:
For others working with the same use case (i.e. generate model rather than load model) the key seems to be adding the stereotype application to the resource.
For example even when configured properly it isn't sufficient to call requirement.setBase_Class(tstClass). You must also call resource.getContents().add(requirement). Immediately after the call to add things show up where they belong. Until then only the mapping from requirement to tstClass will be visible.
You don't usually have to do all of this manually, the method Element.applyStereotype () takes care of all of that.
The problem with your code seems to be that at the moment when you apply the stereotype your model is not added to any Resource, then applyStereotype can not know to which resource to add the stereotype application.
You have to, in order :
1) create the model (applying the required profiles )
Model model = SysmlResourcesUtilities.createSysmlModel2();
System.out.println("AppliedProfiles: " + model.getAppliedProfiles());
2) Add it to a Resource
ResourceSet set = new ResourceSetImpl();
SysmlResourcesUtilities.init(set);
Resource resource = set.createResource(uriSysmlModel);
EList<EObject> contents = resource.getContents();
contents.add(model);
3) Apply the stereotypes
Package reqtPackage = SysmlResourcesUtilities.getPackage(model,"System::Requirements");
reqt = createRequirement2(reqtPackage,"Test Requirement2");
reqt = createRequirement2(reqtPackage,"Test Requirement3");
4) Save the resource
The stereotype application is simply
public static Requirement createRequirement2(Package pkg,String name) {
Class baseClass = pkg.createOwnedClass(name,false);
System.out.println("Stereos: " + baseClass.getApplicableStereotypes());
Stereotype stereo = baseClass.getApplicableStereotype("SysML::Requirements::Requirement");
Requirement result = (Requirement)baseClass.applyStereotype(stereo);
System.out.println("Reqt: " + result);
System.out.println("Applicable Stereos: " + baseClass.getApplicableStereotypes());
System.out.println("Applied Stereos: " + baseClass.getAppliedStereotypes());
return result;
}
I have not actually tested your code, but that is what I have done in the past in my projects and I am sure it works (so don't hesitate to ask me for more details if needed).
Hope it helps
|
|
|
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1753239 is a reply to message #1753092] |
Thu, 02 February 2017 20:51 |
German Vega Messages: 104 Registered: December 2015 Location: Grenoble, France |
Senior Member |
|
|
Hello
I am not familiar with Sysml so I didn't notice before that the profile is statically defined, and has nested profiles.
This complicates a little more the manual registration in Standalone programs. Not only you have to register the static packages, and do the URI mapping, as you did, but you also have to do the mapping between the static packages nsURI and the loaded profile URI ... for the profile and its nested profiles.
In Eclipse this is done with the org.eclipse.uml2.uml.generated_package extension point, in standalone programs you use the method UMLPlugin.getEPackageNsURIToProfileLocationMap
For details on the sysml case, see the long discussion in https://www.eclipse.org/forums/index.php/m/1451276/
I took the liberty of modifying your code, clean it up to simplify all the registering, and added comments for helping you understand what I did. I also generalize it a little to show how other stereotypes may work. I attach the new version to this message.
I have tested it and is working in my configuration (Neon versions of UML and Papyrus)
Regards
|
|
|
Goto Forum:
Current Time: Thu Sep 26 06:21:47 GMT 2024
Powered by FUDForum. Page generated in 0.07462 seconds
|