Persisting EAnnotations into an XMI file [message #1697243] |
Tue, 02 June 2015 12:11  |
Eclipse User |
|
|
|
Hi everyone
How do I get EAnnotations persisted into an XMI file as this example with an instance of an Eclipse UML Class shows:
org.eclipse.uml2.uml.Class _class = UMLFactory.eINSTANCE.createClass();
_class.setName("MyClass");
EcoreUtil.setAnnotation(_class, "myAnnotation", "myKey", "myValue");
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(URI.createFileURI("model.xmi"));
resource.getContents().add(_class);
try {
resource.save(new HashMap<String, String>());
} catch (IOException e) {
// Handle exception
}
Resulting XMI file:
<uml:Class xmi:version="20131001" xmlns:xmi=... xmlns:uml="http://www.eclipse.org/uml2/5.0.0/UML" xmi:id="_QHJAcQgwEeWapOrjTLkrLA" name="MyClass">
<eAnnotations xmi:id="_QHKOkAgwEeWapOrjTLkrLA" source="http://myAnnotation">
<details xmi:id="_QHKOkQgwEeWapOrjTLkrLA" key="myKey" value="myValue"/>
</eAnnotations>
</uml:Class>
However, this only seems to work with UML objects (instances of EClasses defined within the Eclipse UML meta model).
If I define my own EClass in my own Ecore model and although I set "org.eclipse.emf.ecore.impl.EModelElementImpl" as value for the "Root Extends Class" property of the corresponding generator model (which basically adds the capability to the generated classes to hold EAnnotations), EAnnotations are not persisted for any instance of that EClass.
<myModel:MyClass xmi:version="2.0" xmlns:xmi=... xmlns:myModel="http://myModel" id="myInstance"/>
^^^^^^^^^ eAnnotations element is missing!
Any hints?
Thanks!
|
|
|
Re: Persisting EAnnotations into an XMI file [message #1697259 is a reply to message #1697243] |
Tue, 02 June 2015 15:10   |
Eclipse User |
|
|
|
Comments below.
On 02/06/2015 7:58 PM, diophant diophant wrote:
> Hi everyone
>
> How do I get EAnnotations persisted into an XMI file as this example
> with an instance of an Eclipse UML Class shows:
>
> org.eclipse.uml2.uml.Class _class = UMLFactory.eINSTANCE.createClass();
> _class.setName("MyClass");
> EcoreUtil.setAnnotation(_class, "myAnnotation", "myKey", "myValue");
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource =
> resourceSet.createResource(URI.createFileURI("model.xmi"));
> resource.getContents().add(_class);
> try {
> resource.save(new HashMap<String, String>());
> } catch (IOException e) {
> // Handle exception
> }
>
> Resulting XMI file:
>
> <uml:Class xmi:version="20131001" xmlns:xmi=...
> xmlns:uml="http://www.eclipse.org/uml2/5.0.0/UML"
> xmi:id="_QHJAcQgwEeWapOrjTLkrLA" name="MyClass">
> <eAnnotations xmi:id="_QHKOkAgwEeWapOrjTLkrLA"
> source="http://myAnnotation">
> <details xmi:id="_QHKOkQgwEeWapOrjTLkrLA" key="myKey"
> value="myValue"/>
> </eAnnotations>
> </uml:Class>
>
> However, this only seems to work with UML objects (instances of
> EClasses defined within the Eclipse UML meta model).
It only works for subclasses of EModelElement.
> If I define my own EClass in my own Ecore model and although I set
> "org.eclipse.emf.ecore.impl.EModelElementImpl" as value for the "Root
> Extends Class"
You must not do that. The Root Extends Class must be purely an
implementation class that doesn't have modeled state.
> property of the corresponding generator model (which basically adds
> the capability to the generated classes to hold EAnnotations),
> EAnnotations are not persisted for any instance of that EClass.
You can extend EModelElement explicitly, but that's not recommend.
>
> <myModel:MyClass xmi:version="2.0" xmlns:xmi=...
> xmlns:myModel="http://myModel" id="myInstance"/>
> ^^^^^^^^^ eAnnotations element is missing!
>
> Any hints?
Best you define your own annotation representation in your own model,
i.e., copy the pattern that you see in Ecore, but don't extend Ecore.
>
> Thanks!
|
|
|
|
Powered by
FUDForum. Page generated in 0.03356 seconds