Exception raised when applying stereotype [message #1821084] |
Wed, 05 February 2020 10:52 |
jo ber Messages: 94 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 #1821086 is a reply to message #1821084] |
Wed, 05 February 2020 11:06 |
Camille Letavernier Messages: 952 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:
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 |
jo ber Messages: 94 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 |
Camille Letavernier Messages: 952 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:
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04589 seconds