Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 Go to next message
Ernesto Posse is currently offline Ernesto PosseFriend
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 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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 #1589703 is a reply to message #1588678] Wed, 28 January 2015 15:55 Go to previous messageGo to next message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 438
Registered: March 2011
Senior Member
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 #1589889 is a reply to message #1589703] Wed, 28 January 2015 18:18 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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 #1590147 is a reply to message #1589889] Wed, 28 January 2015 21:41 Go to previous messageGo to next message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 438
Registered: March 2011
Senior Member
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 #1590279 is a reply to message #1590147] Wed, 28 January 2015 23:22 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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 #1752312 is a reply to message #1590279] Fri, 20 January 2017 21:31 Go to previous messageGo to next message
Matt Fischer is currently offline Matt FischerFriend
Messages: 5
Registered: April 2013
Junior Member
I'm not sure if I have a related problem or not. I am experiencing similar behavior, but I am not loading the model from file. I'm creating it in code. I use the applyProfile and applyStereotype methods to build the model.

The applyProfile method seems to work as when I query the model using getAppliedProfiles I get the list that I applied. When I use the applyStereotype method it returns the expected EObject, but when I query getAppliedStereotypes and/or getStereotypeApplications they both return empty lists.

Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1752622 is a reply to message #1752312] Wed, 25 January 2017 15:47 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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 #1752641 is a reply to message #1752622] Wed, 25 January 2017 17:28 Go to previous messageGo to next message
Matt Fischer is currently offline Matt FischerFriend
Messages: 5
Registered: April 2013
Junior Member
Thanks for the reply.

It seems that my use case is backwards from every example I've found. All my Googling has lead me to examples where people are loading from a file. My current use case is to build the Papyrus model programmatically and save it to a file.

I have attached both the example and the utilities source files.

The example should do the following.
1) Call create model: which creates a generic structure and applies the Papyrus SysML profile.

2) Call createRequirement to generate a couple of test requirements. I have tried two different methods, neither of which work.

3) Save the model using the utility save method, which: creates and initializes the ResourceSet; creates the Resource; adds the model to the Resource; recursively looks for applied Stereotypes and attempts to separately add those to the Resource (necessary since the Stereotype applications aren't saved on the model, but the root xmi:XMI object); and finally calls the Resource save method.

Everything runs without error, but there are a couple issues:
1) During runtime none of the Requirement baseClass object reflect the applied stereotypes.
2) None of the Requirement objects get added to the Resource file.
3) If I don't manually add the SysML profile to the generated model Class objects do list any of the applicable stereotypes after calling setBase_Class on the Requirement. (which kind of makes sense, no auto applying the profile... Hence the second createModel and createRequirement)

Is there an example of programmatically creating Papyrus models? All I found were plain UML2 or loading from file.
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1753092 is a reply to message #1752641] Wed, 01 February 2017 19:36 Go to previous messageGo to next message
Matt Fischer is currently offline Matt FischerFriend
Messages: 5
Registered: April 2013
Junior Member
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.

Even following the previous examples that work great for loading from a file getAppliedStereotypes and getStereotypeApplications return empty lists until you add the StereotypeApplication to the resource. It immediately becomes visible adding 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.
Re: getAppliedStereotypes is empty but the stereotype has been applies [message #1753166 is a reply to message #1753092] Thu, 02 February 2017 11:04 Go to previous messageGo to next message
German Vega is currently offline German VegaFriend
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
resource.save(null);


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 Go to previous message
German Vega is currently offline German VegaFriend
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
Previous Topic:XPath query for list of UML class attributes that are not associations
Next Topic:Cannot get Instances of Applied Stereotypes
Goto Forum:
  


Current Time: Tue Apr 16 13:29:13 GMT 2024

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

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

Back to the top