Home » Modeling » EMF » Serialization problem concerning Maps
| | |
Re: Serialization problem concerning Maps [message #553098 is a reply to message #553052] |
Mon, 16 August 2010 13:27 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------070502020401070906050607
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Udo,
Comments below.
Udo wrote:
> Hi Ed,
> comments below.
>
> Ed Merks wrote on Thu, 12 August 2010 12:01
>> Udo,
>>
>> Comments below.
>>
>> Udo wrote:
>> > I want to serialize my model to XMI. In my model there are maps
>> which > I want to serialize, too. I want to save the key and the
>> value of each > map entry. How can I do this?
>> >
>> > I use the resource set. I do it like that:
>> >
>> >
>> > ResourceSet resourceSet = new ResourceSetImpl();
>> > resourceSet.getResourceFactoryRegistry().getExtensionToFacto
>> ryMap().put( "xmi", > new XMIResourceFactoryImpl());
>> > Resource resource =
>> resourceSet.createResource(URI.createFileURI("some > file name"));
>> > resource.getContents().add(form);
>> > resource.save(null);
>> >
>> >
>> > So far no problem but my maps store only the keys.
>> >
>> > One map is defined as Java interface like this:
>> >
>> >
>> > /**
>> > * @model
>> > */
>> > public interface Variables extends EObject {
>> >
>> > /**
>> > * @return
>> > * @model mapType="HashMap" keyType="String"
>> valueType="VartabEntry" > containment="true"
>> > */
>> > EMap<String, VartabEntry> getVartabEntriesMap();
>> Did you look at the FAQ how to do this?
>>
>> As I understand the FAQ I did the right thing with my "@model"
>> annotations.
The FAQ looked like this.
/**
* @model keyType="java.lang.String" valueType="java.lang.String"
*/
Map<String, String> getScoScoMap();
And EAnnotation.details looks like this:
/**
* @model
mapType="org.eclipse.emf.ecore.EStringToStringMapEntry<org.eclipse.emf.ecore.EString,
org.eclipse.emf.ecore.EString>"
*/
EMap<String, String> getDetails();
Things to note are fully qualified names. In fact, note that in
EAnnotation, the fully qualified names are Ecore names, not Java names.
There's also no use of containment='true'.
>> EMF generated the class EStringToVartabEntryMapEntryImpl.
Yes, but there is no EStringToVartabMapEntry interface. I.e., there's
nothing like your VartabEntry.
>>
>> Do I have to add some methods to that generated class?
>>
>> My ecore model looks like this (for the generated class):
>>
>>
>> <eClassifiers xsi:type="ecore:EClass"
>> name="EStringToVartabEntryMapEntry"
>> instanceClassName="java.util.Map$Entry">
>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="key"
>> eType="ecore:EDataType
>> platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//E String "/>
>> <eStructuralFeatures xsi:type="ecore:EReference" name="value"
>> eType="#//VartabEntry"/>
>> </eClassifiers>
>>
>>
>> Is this correct?
You don't want the value to be of type VartabEntry though do you? I
assume you want it to be string like in the FAQ example and in EAnnotation.
>>
>> >
>> >
>> > And VartabEntry is defined like this:
>> >
>> >
>> > /**
>> > * @model
>> > */
>> > public interface VartabEntry extends EObject {
>> > /**
>> > * @return
>> > * @model
>> > */
>> > String getEnglishName();
>> >
>> > /**
>> > * @return
>> > * @model
>> > */
>> > String getName();
>> >
>> > ... and more attributes.
>> >
>> You'd not define an explicit interface for the map entry. Did you
>> look at how EAnnotation.getDetails works?
>>
>> I do not understand, why I should not define an interface for my
>> "value" type. What should I implement to serialize that correctly. I
>> saw that EAnnotation.getDetails tries to get the serialized form of
>> my value type but where do I specify it?
>> >
>> > And I got following XMI part in my file:
>> >
>> >
>> > <variables>
>> > <vartabEntriesMap key="0:grust"/>
>> > <vartabEntriesMap key="0:gruppe"/>
>> > <vartabEntriesMap key="0:grbez"/>
>> > <vartabEntriesMap key="0:dnr"/>
>> > <vartabEntriesMap key="0:dbez"/>
>> > <vartabEntriesMap key="0:ozeilen"/>
>> > <vartabEntriesMap key="0:sn"/>
>> >
>> > ..
>> > </variables>
>> >
>> >
>> >
>> > How can I tell EMF to store also the VartabEntry instances in the
>> XMI > file?
>> >
>> > With regards,
>> > Udo
>> >
>
>
--------------070502020401070906050607
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Udo,<br>
<br>
Comments below. <br>
<br>
Udo wrote:
<blockquote cite="mid:i4b0ge$jc7$1@build.eclipse.org" type="cite">Hi
Ed, <br>
comments below.
<br>
<br>
Ed Merks wrote on Thu, 12 August 2010 12:01
<br>
<blockquote type="cite">Udo,
<br>
<br>
Comments below.
<br>
<br>
Udo wrote:
<br>
> I want to serialize my model to XMI. In my model there are maps
which > I want to serialize, too. I want to save the key and the
value of each > map entry. How can I do this?
<br>
>
<br>
> I use the resource set. I do it like that:
<br>
>
<br>
>
<br>
> ResourceSet resourceSet = new ResourceSetImpl();
<br>
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto
ryMap().put( "xmi", > new XMIResourceFactoryImpl());
<br>
> Resource resource =
resourceSet.createResource(URI.createFileURI("some > file name"));
<br>
> resource.getContents().add(form);
<br>
> resource.save(null);
<br>
>
<br>
>
<br>
> So far no problem but my maps store only the keys.
<br>
>
<br>
> One map is defined as Java interface like this:
<br>
>
<br>
>
<br>
> /**
<br>
> * @model
<br>
> */
<br>
> public interface Variables extends EObject {
<br>
>
<br>
> /**
<br>
> * @return
<br>
> * @model mapType="HashMap" keyType="String"
valueType="VartabEntry" > containment="true"
<br>
> */
<br>
> EMap<String, VartabEntry> getVartabEntriesMap();
<br>
Did you look at the FAQ how to do this?
<br>
<br>
As I understand the FAQ I did the right thing with my "@model"
annotations. <br>
</blockquote>
</blockquote>
The FAQ looked like this.<br>
<blockquote>
<pre class="source-java5"><span class="coMULTI">/**
* @model keyType="java.lang.String" valueType="java.lang.String"
*/</span>
Map<String, String> getScoScoMap<span class="br0">(</span><span
class="br0">)</span>;
</pre>
</blockquote>
And EAnnotation.details looks like this:<br>
<blockquote> /**<br>
* @model
mapType=" org.eclipse.emf.ecore.EStringToStringMapEntry<org.ecl ipse.emf.ecore.EString,
org.eclipse.emf.ecore.EString>"<br>
*/<br>
EMap<String, String> getDetails();<br>
</blockquote>
Things to note are fully qualified names. In fact, note that in
EAnnotation, the fully qualified names are Ecore names, not Java
names. There's also no use of containment='true'.<br>
<blockquote cite="mid:i4b0ge$jc7$1@build.eclipse.org" type="cite">
<blockquote type="cite"> EMF generated the class
EStringToVartabEntryMapEntryImpl.
<br>
</blockquote>
</blockquote>
Yes, but there is no EStringToVartabMapEntry interface. I.e., there's
nothing like your VartabEntry.
<blockquote cite="mid:i4b0ge$jc7$1@build.eclipse.org" type="cite">
<blockquote type="cite"><br>
Do I have to add some methods to that generated class?
<br>
<br>
My ecore model looks like this (for the generated class):
<br>
<br>
<br>
<eClassifiers xsi:type="ecore:EClass"
name="EStringToVartabEntryMapEntry"
instanceClassName="java.util.Map$Entry">
<br>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="key"
eType="ecore:EDataType
platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//E String "/>
<br>
<eStructuralFeatures xsi:type="ecore:EReference" name="value"
eType="#//VartabEntry"/>
<br>
</eClassifiers>
<br>
<br>
<br>
Is this correct?
<br>
</blockquote>
</blockquote>
You don't want the value to be of type VartabEntry though do you? I
assume you want it to be string like in the FAQ example and in
EAnnotation.<br>
<blockquote cite="mid:i4b0ge$jc7$1@build.eclipse.org" type="cite">
<blockquote type="cite"><br>
>
<br>
>
<br>
> And VartabEntry is defined like this:
<br>
>
<br>
>
<br>
> /**
<br>
> * @model
<br>
> */
<br>
> public interface VartabEntry extends EObject {
<br>
> /**
<br>
> * @return
<br>
> * @model
<br>
> */
<br>
> String getEnglishName();
<br>
>
<br>
> /**
<br>
> * @return
<br>
> * @model
<br>
> */
<br>
> String getName();
<br>
>
<br>
> ... and more attributes.
<br>
>
<br>
You'd not define an explicit interface for the map entry. Did you
look at how EAnnotation.getDetails works?
<br>
<br>
I do not understand, why I should not define an interface for my
"value" type. What should I implement to serialize that correctly. I
saw that EAnnotation.getDetails tries to get the serialized form of my
value type but where do I specify it?
<br>
>
<br>
> And I got following XMI part in my file:
<br>
>
<br>
>
<br>
> <variables>
<br>
> <vartabEntriesMap key="0:grust"/>
<br>
> <vartabEntriesMap key="0:gruppe"/>
<br>
> <vartabEntriesMap key="0:grbez"/>
<br>
> <vartabEntriesMap key="0:dnr"/>
<br>
> <vartabEntriesMap key="0:dbez"/>
<br>
> <vartabEntriesMap key="0:ozeilen"/>
<br>
> <vartabEntriesMap key="0:sn"/>
<br>
>
<br>
> ..
<br>
> </variables>
<br>
>
<br>
>
<br>
>
<br>
> How can I tell EMF to store also the VartabEntry instances in the
XMI > file?
<br>
>
<br>
> With regards,
<br>
> Udo
<br>
>
<br>
</blockquote>
<br>
<br>
</blockquote>
</body>
</html>
--------------070502020401070906050607--
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: Serialization problem concerning Maps [message #553961 is a reply to message #553948] |
Thu, 19 August 2010 14:18 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------020801050208000902000106
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Udo,
Comments below.
Udo wrote:
> Hi Ed,
>
> I think you got me wrong.
>
> I do want to have a map which has keys of type String and values of
> type VartabEntry.
Oh.
> The only thing which I do not understand is how do I tell the
> generated class EStringToVartabEntryMapEntryImpl to serialize its keys
> AND values. It serializes only its keys, which are serialized correctly.
I see nothing that should prevent that feature from being serialized.
You're sure it's actually populated with non-null values?
>
> Or can I not use a map like I want to use?
The Change model has maps with references so it should work...
* @model
mapType="org.eclipse.emf.ecore.change.EObjectToChangesMapEntry <org.eclipse.emf.ecore.EObject,
org.eclipse.emf.ecore.change.FeatureChange>"
* @generated
*/
EMap<EObject, EList<FeatureChange>> getObjectChanges();
Check during serialization if eIsSet is being called for the value
feature of the map entry and if it's returning true. It should be
serialized in that case.
>
> Regards,
> Udo
>
--------------020801050208000902000106
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Udo,<br>
<br>
Comments below.<br>
<br>
Udo wrote:
<blockquote cite="mid:i4jcsc$agh$1@build.eclipse.org" type="cite">Hi
Ed,
<br>
<br>
I think you got me wrong.
<br>
<br>
I do want to have a map which has keys of type String and values of
type VartabEntry.</blockquote>
Oh.<br>
<blockquote cite="mid:i4jcsc$agh$1@build.eclipse.org" type="cite"> The
only thing which I do not understand is how do I tell the generated
class EStringToVartabEntryMapEntryImpl to serialize its keys AND
values. It serializes only its keys, which are serialized correctly.
<br>
</blockquote>
I see nothing that should prevent that feature from being serialized.
You're sure it's actually populated with non-null values?<br>
<blockquote cite="mid:i4jcsc$agh$1@build.eclipse.org" type="cite"><br>
Or can I not use a map like I want to use?
<br>
</blockquote>
The Change model has maps with references so it should work...<br>
<blockquote> * @model
mapType=" org.eclipse.emf.ecore.change.EObjectToChangesMapEntry< ;org.eclipse.emf.ecore.EObject,
org.eclipse.emf.ecore.change.FeatureChange>"<br>
* @generated<br>
*/<br>
EMap<EObject, EList<FeatureChange>> getObjectChanges();<br>
</blockquote>
Check during serialization if eIsSet is being called for the value
feature of the map entry and if it's returning true. It should be
serialized in that case.<br>
<blockquote cite="mid:i4jcsc$agh$1@build.eclipse.org" type="cite"><br>
Regards,
<br>
Udo
<br>
<br>
</blockquote>
</body>
</html>
--------------020801050208000902000106--
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | |
Re: Serialization problem concerning Maps [message #638266 is a reply to message #638066] |
Wed, 10 November 2010 14:38 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
Udo,
Comments below.
Udo Walker wrote:
> Hi Ed,
>
> I found a solution but I don't know how to do it with annotations in
> my interfaces.
You can always generate the code to see what @model annotations are
generated. Delete the method and let it me regenerated if necessary.
>
> If I open the *.ecore file and go to the value feature of the map and
> change the containment from (default) "false" to "true" then the value
> which is my from EMF generated object is also saved.
>
> How can I tell the EMF generator with Java annotations that the value
> of my map needs the containment attribute set to "true"?
I'm not sure what you did that made if false. There is a FAQ about this.
>
> Here is the snippet from my ecore file:
>
> <eClassifiers xsi:type="ecore:EClass" name="VartabEntryMap"
> instanceClassName="java.util.Map$Entry">
> <eStructuralFeatures
> xsi:type="ecore:EAttribute"
> name="key"
> eType="ecore:EDataType
> http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> <eStructuralFeatures
> xsi:type="ecore:EReference"
> name="value"
> eType="#//VartabEntry"
> containment="true"/>
> </eClassifiers>
>
> The second last line is the important one. How to do that in Java
> annotations?
>
> Udo
>
>
> Am 27.08.2010 16:12, schrieb Ed Merks:
>> Udo,
>>
>> The debugger is doing to answer your questions faster than me. Monitor
>> what happens in XMLSaveImpl.saveFeatures for your map entry instances.
>> Pay particular attention to the processing of the features where there
>> is a guard like this
>>
>> if (kind != TRANSIENT && shouldSaveFeature(o, f))
>>
>>
>> Udo wrote:
>>> Hi Ed,
>>>
>>> Quote:
>>>> Check during serialization if eIsSet is being called for the value
>>>> feature of the map entry and if it's returning true. It should be
>>>> serialized in that case.
>>>
>>>
>>> I checked the method eIsSet of my EStringToVartabEntryMapEntryImpl and
>>> it returns true. I did this by overwriting the method eIsSet in that
>>> class and checking the name of the feature and value of the
>>> super.eIsSet() method. The value feature returns true.
>>>
>>> My output still looks like this:
>>>
>>>
>>> <variables>
>>> <vartabEntriesMap key="0:grust"/>
>>> <vartabEntriesMap key="0:gruppe"/>
>>> <vartabEntriesMap key="0:grbez"/>
>>> <vartabEntriesMap key="0:dnr"/>
>>> <vartabEntriesMap key="0:dbez"/>
>>> <vartabEntriesMap key="0:ozeilen"/>
>>> ..
>>>
>>>
>>> Still only keys.
>>> Do you have any ideas what I could do wrong?
>>>
>>>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Thu Mar 28 18:10:57 GMT 2024
Powered by FUDForum. Page generated in 0.06519 seconds
|