Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Exception raised when applying stereotype
Exception raised when applying stereotype [message #1821084] Wed, 05 February 2020 10:52 Go to next message
jo ber is currently offline jo berFriend
Messages: 30
Registered: August 2018
Member
Hi

When I'm assigning a stereotype to a property, the following exception is raised. I imagine that I'm missing something, but can't figure out what. See code below and project in attachment.

public class Test {

	private static final ResourceSet RESOURCE_SET;
	private static final ExtensibleURIConverterImpl converter = new ExtensibleURIConverterImpl();

	private static final URI ulModel = URI.createURI("MyModel.uml");
    private static final URI upModel = URI.createFileURI("model/MyModel.uml");

	private static final URI ulProfile = URI.createURI("MyModel.profile.uml");
    private static final URI upProfile = URI.createFileURI("model/MyModel.profile.uml");
    
	static {
		RESOURCE_SET = new ResourceSetImpl();
		UMLResourcesUtil.init(RESOURCE_SET);

		converter.getURIMap().put(ulModel, upModel);
		converter.getURIMap().put(ulProfile, upProfile);
	}
    
	public static void main(String[] args) {
		Model model = (Model)RESOURCE_SET.getResource(converter.normalize(ulModel), true).getContents().get(0);
		Profile profile = (Profile)RESOURCE_SET.getResource(converter.normalize(ulProfile), true).getContents().get(0);
		profile.define();
		model.applyProfile(profile);

		Class myClass = (Class) model.getOwnedType("My Class");
		Property myProperty = (Property) myClass.getOwnedAttribute("My Attribute", null);

		Stereotype s = profile.createOwnedStereotype("My Stereotype", false);
		myProperty.applyStereotype(s);
	}

}


The following exception is raised:
Exception in thread "main" java.lang.IllegalArgumentException: stereotype "My Model::My Stereotype" has no Ecore definition
	at org.eclipse.uml2.uml.internal.operations.ElementOperations.getDefinition(ElementOperations.java:1276)
	at org.eclipse.uml2.uml.internal.operations.ElementOperations.applyStereotype(ElementOperations.java:1496)
	at org.eclipse.uml2.uml.internal.impl.ElementImpl.applyStereotype(ElementImpl.java:504)
	at test.Test.main(Test.java:43)


Thank you for your help.
Kind regards

[Updated on: Wed, 05 February 2020 10:53]

Report message to a moderator

Re: Exception raised when applying stereotype [message #1821085 is a reply to message #1821084] Wed, 05 February 2020 11:02 Go to previous messageGo to next message
Rafael Chaves is currently offline Rafael ChavesFriend
Messages: 10
Registered: August 2015
Junior Member
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles#Defining_Profiles

I believe it is because you created a new stereotype *after* defining the profile. The stereotype should already have been created at the time you define the profile (defining a profile is a final operation you perform on a profile, before it is ready to be applied to packages).

Moreover, if you are loading a persisted profile, it probably should have the stereotype already created (before it was persisted).

Re: Exception raised when applying stereotype [message #1821086 is a reply to message #1821084] Wed, 05 February 2020 11:06 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 929
Registered: February 2011
Senior Member
Hi,

The issue is that you're trying to create a new Stereotype (meta-element), instead of applying an existing Stereotype:

Stereotype s = profile.createOwnedStereotype("My Stereotype", false);
myProperty.applyStereotype(s);


Instead, you should be looking up your stereotype (by name) in your profile:

Stereotype s = profile.getOwnedStereotype("Extended Class");
myProperty.applyStereotype(s);


However, your Stereotype "My Stereotype" has been defined as a Class Stereotype (i.e. a Stereotype that should be applied on a UML Class), so you won't be able to apply it to a Property.

You can also retrieve the Stereotype from the target element to which you want to apply it, for example:

Stereotype s = myClass.getApplicableStereotype("Extended Class");
myClass.applyStereotype(s);


In any case, this line should be removed:

profile.define();


The profile should be defined once when you create or modify it; this is not something that you should do at runtime/when using the profile.

HTH,
Camille


Camille Letavernier
Re: Exception raised when applying stereotype [message #1821087 is a reply to message #1821086] Wed, 05 February 2020 11:29 Go to previous messageGo to next message
jo ber is currently offline jo berFriend
Messages: 30
Registered: August 2018
Member
Thank you Camille, this helped me.
Now, I'm facing another issue. I want to persist the model with the class which contain the application of the stereotype in a new file. However, when persisted, the file doesn't contain the application of the stereotype with the value I've specified:

public class Test {

	private static final ResourceSet RESOURCE_SET;
	private static final ExtensibleURIConverterImpl converter = new ExtensibleURIConverterImpl();

	private static final URI ulModel = URI.createURI("MyModel.uml");
    private static final URI upModel = URI.createFileURI("model/MyModel.uml");

	private static final URI ulProfile = URI.createURI("MyModel.profile.uml");
    private static final URI upProfile = URI.createFileURI("model/MyModel.profile.uml");

	private static final URI ulNewModel = URI.createURI("NewModel.uml");
    private static final URI upNewModel = URI.createFileURI("model/NewModel.uml");

	static {
		RESOURCE_SET = new ResourceSetImpl();
		UMLResourcesUtil.init(RESOURCE_SET);

		converter.getURIMap().put(ulModel, upModel);
		converter.getURIMap().put(ulProfile, upProfile);
		converter.getURIMap().put(ulNewModel, upNewModel);
	}
    
	public static void main(String[] args) {
		Model model = (Model)RESOURCE_SET.getResource(converter.normalize(ulModel), true).getContents().get(0);
		Profile profile = (Profile)RESOURCE_SET.getResource(converter.normalize(ulProfile), true).getContents().get(0);
		model.applyProfile(profile);

		Class myClass = (Class) model.getOwnedType("My Class");

		Stereotype s = profile.getOwnedStereotype("Extended Class");
		myClass.applyStereotype(s);
		myClass.setValue(s, "Name", "Class with stereotype");

		try {
			Resource newModel = RESOURCE_SET.createResource(converter.normalize(ulNewModel));
			newModel.getContents().add(model);
			newModel.save(null);
		} catch(Exception e) {
		}
	}
}
Re: Exception raised when applying stereotype [message #1821090 is a reply to message #1821087] Wed, 05 February 2020 12:02 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 929
Registered: February 2011
Senior Member
Hi,

By default, Stereotypes are stored in the Resource next to the UML Element they are applied to. However, in your case, you create a new Resource just before saving, and only add the UML Model (Which doesn't include the Stereotypes). So only the UML Model is saved.

Since you load the model from an existing Resource, you should keep using that resource instead.

So you can replace this:

Model model = (Model)RESOURCE_SET.getResource(converter.normalize(ulModel), true).getContents().get(0);


With this:

Resource umlResource = RESOURCE_SET.getResource(converter.normalize(ulModel), true);
Model model = (Model)umlResource.getContents().get(0);


And then just save that resource at the end. Instead of:

Resource newModel = RESOURCE_SET.createResource(converter.normalize(ulNewModel));
newModel.getContents().add(model);
newModel.save(null);


Just save the existing resource as-is:

umlResource.save(null);


Edit: if it is really your intention to create a new Resource (And not modify the existing one), you can still do that; but you need to add *all* elements from the original resource to the new one:

Resource newModel = RESOURCE_SET.createResource(converter.normalize(ulNewModel));
newModel.getContents().addAll(umlResource.getContents());
newModel.save(null);



HTH,
Camille


Camille Letavernier

[Updated on: Wed, 05 February 2020 12:04]

Report message to a moderator

Re: Exception raised when applying stereotype [message #1821132 is a reply to message #1821090] Thu, 06 February 2020 07:44 Go to previous message
jo ber is currently offline jo berFriend
Messages: 30
Registered: August 2018
Member
thank you for the clear and exhaustive answer!
Previous Topic:Business Key
Next Topic:Forum on Privacy and Data Protection Challenges in System Development
Goto Forum:
  


Current Time: Tue Oct 27 07:49:53 GMT 2020

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

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

Back to the top