Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Custom serialization
Custom serialization [message #425661] Thu, 04 December 2008 11:40 Go to next message
Rodolphe Beck is currently offline Rodolphe BeckFriend
Messages: 18
Registered: July 2009
Junior Member
Hello,

I have to save my objects in a particular format, therefore I must
customize the way EMF objects are serialized.

I first tried to get in memory access to the object, but I couldn't find a
way to do it.
Then I wanted to reload the .XMI and iterate on it to recover my objects,
but it still doesn't work.

Here is my code :
http://pastebin.com/m286c17ba

As advised in another post here, I looked at the XyzExample.java in *test.

It works in the test, not in my plugin, why ?

Thanks.
Re: Custom serialization [message #425666 is a reply to message #425661] Thu, 04 December 2008 12:57 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Rodolphe,

Comments below.

Rodolphe Beck wrote:
> Hello,
>
> I have to save my objects in a particular format, therefore I must
> customize the way EMF objects are serialized.
>
> I first tried to get in memory access to the object, but I couldn't
> find a way to do it.
> Then I wanted to reload the .XMI and iterate on it to recover my
> objects, but it still doesn't work.
What specifically is the symptom of "not working"?
>
> Here is my code :
> http://pastebin.com/m286c17ba
It doesn't seem good to use "toOSString" since that will use "\" on
Windows rather than "/" as is proper for a URI.
>
> As advised in another post here, I looked at the XyzExample.java in
> *test.
>
> It works in the test, not in my plugin, why ?
Again, the symptoms of not working are kind of important to understand.

I wonder what you use wsLocation for? Likely no good will come from
accessing the physical OS-specific file path for the workspace...
>
> Thanks.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Custom serialization [message #425674 is a reply to message #425666] Thu, 04 December 2008 14:18 Go to previous messageGo to next message
Rodolphe Beck is currently offline Rodolphe BeckFriend
Messages: 18
Registered: July 2009
Junior Member
Ok my message wass totaly unclear sorry about that...

I changed my code into :
http://pastebin.com/m7ebe537d

And I managed to have my resource. Then, when I wanted to get the content
of my file, so I tried :
TreeIterator<EObject> ti = instanceResource.getAllContents();
But my treeiterator was empty...

With : EList<EObject> list = instanceResource.getContents();
It works.

By the way, is it possible to access the objects directly in memory, so I
don't have to parse the file again ?

Thanks
Re: Custom serialization [message #425682 is a reply to message #425674] Thu, 04 December 2008 15:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Rodolphe,

Comments below.


Rodolphe Beck wrote:
> Ok my message wass totaly unclear sorry about that...
>
> I changed my code into :
> http://pastebin.com/m7ebe537d
>
> And I managed to have my resource. Then, when I wanted to get the
> content of my file, so I tried :
> TreeIterator<EObject> ti = instanceResource.getAllContents();
> But my treeiterator was empty...
>
> With : EList<EObject> list = instanceResource.getContents();
> It works.
That doesn't make must sense. The former walks the whole tree and the
latter walks just the roots, so the former should always include the
latter...
>
> By the way, is it possible to access the objects directly in memory,
> so I don't have to parse the file again ?
Only by loading a resource will have have an in-memory representation.
You can keep it around as long as you need it.
>
> Thanks
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Custom serialization [message #425694 is a reply to message #425682] Fri, 05 December 2008 08:29 Go to previous messageGo to next message
Rodolphe Beck is currently offline Rodolphe BeckFriend
Messages: 18
Registered: July 2009
Junior Member
Ed Merks wrote:

> Rodolphe,

> Comments below.


> Rodolphe Beck wrote:
>> Ok my message wass totaly unclear sorry about that...
>>
>> I changed my code into :
>> http://pastebin.com/m7ebe537d
>>
>> And I managed to have my resource. Then, when I wanted to get the
>> content of my file, so I tried :
>> TreeIterator<EObject> ti = instanceResource.getAllContents();
>> But my treeiterator was empty...
>>
>> With : EList<EObject> list = instanceResource.getContents();
>> It works.
> That doesn't make must sense. The former walks the whole tree and the
> latter walks just the roots, so the former should always include the
> latter...
Yes, but in my case it doesn't :)
>>
>> By the way, is it possible to access the objects directly in memory,
>> so I don't have to parse the file again ?
> Only by loading a resource will have have an in-memory representation.
> You can keep it around as long as you need it.
Sure, but when you create an element of your diagram, before you type
CTRL+s to serialize the element into the diagram file, this element should
be somewhere in memory. I couldn't find a way to load it directly from
here.
>>
>> Thanks
>>
Re: Custom serialization [message #425697 is a reply to message #425694] Fri, 05 December 2008 09:36 Go to previous messageGo to next message
Rodolphe Beck is currently offline Rodolphe BeckFriend
Messages: 18
Registered: July 2009
Junior Member
Well, I tried to make a RCP application from my plugin, and I had to
change many things :

First to get the path of the file from the current editor, the former
method :
http://pastebin.com/m530a0516

Did not work as my fName was null.

So I changed it into :
http://pastebin.com/m290ee44a

It looks nasty, is there a cleaner way to do this ?

Next, when I try to load the content of the file :
http://pastebin.com/m2e69cf03

list, and ti are both null...

What am I doing wrong please ?

Thanks again.
Re: Custom serialization [message #425699 is a reply to message #425697] Fri, 05 December 2008 10:38 Go to previous messageGo to next message
Rodolphe Beck is currently offline Rodolphe BeckFriend
Messages: 18
Registered: July 2009
Junior Member
Ok, it is because my domain file is not in the same place as my diagram
file.

So I activated the option "Same file for diagram and model", everything
works perfectly.

Thanks again for your help.
Re: Custom serialization [message #425703 is a reply to message #425694] Fri, 05 December 2008 11:06 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Rodolphe,

Comments below.


Rodolphe Beck wrote:
> Ed Merks wrote:
>
>> Rodolphe,
>
>> Comments below.
>
>
>> Rodolphe Beck wrote:
>>> Ok my message wass totaly unclear sorry about that...
>>>
>>> I changed my code into :
>>> http://pastebin.com/m7ebe537d
>>>
>>> And I managed to have my resource. Then, when I wanted to get the
>>> content of my file, so I tried :
>>> TreeIterator<EObject> ti = instanceResource.getAllContents();
>>> But my treeiterator was empty...
>>>
>>> With : EList<EObject> list = instanceResource.getContents();
>>> It works.
>> That doesn't make must sense. The former walks the whole tree and
>> the latter walks just the roots, so the former should always include
>> the latter...
> Yes, but in my case it doesn't :)
So you claim, but it seems impossible to me for ti.hasNext() not to
return precisely the value of
instanceResource.getContents().iterator().hasNext() the first time it's
called because that's how the tree iterator is implemented...
>>>
>>> By the way, is it possible to access the objects directly in memory,
>>> so I don't have to parse the file again ?
>> Only by loading a resource will have have an in-memory
>> representation. You can keep it around as long as you need it.
> Sure, but when you create an element of your diagram, before you type
> CTRL+s to serialize the element into the diagram file, this element
> should be somewhere in memory.
I'm very confused by how this relates to not parsing the file again though.
> I couldn't find a way to load it directly from here.
If you have it in memory, why do you need to load it. There's clearly
something I'm completely missing in your description...
>>>
>>> Thanks
>>>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Custom serialization [message #425704 is a reply to message #425697] Fri, 05 December 2008 11:15 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Rodolphe,

Comments below.

Rodolphe Beck wrote:
> Well, I tried to make a RCP application from my plugin, and I had to
> change many things :
Does your RCP application use a workspace? Most RCP applications don't...
>
> First to get the path of the file from the current editor, the former
> method :
> http://pastebin.com/m530a0516
Likely you don't have a workspace and the editor input isn't an
IFileEditorInput.
>
> Did not work as my fName was null.
>
> So I changed it into :
> http://pastebin.com/m290ee44a
>
> It looks nasty, is there a cleaner way to do this ?
If you look in a generated EMF editor you'll see code like this:

public void createModel()
{
URI resourceURI = EditUIUtil.getURI(getEditorInput());

I'm really not sure what led you to the assumption that a URI is just a
file system path. Did you look at the actual value of the URI that's
returned. My sense is that you didn't ever look at the value. Are you
using the Eclipse debugger to step through your logic? You ought to
because it will answers questions more quickly than me.
>
> Next, when I try to load the content of the file :
> http://pastebin.com/m2e69cf03
>
> list, and ti are both null...
It's impossible for the list or the tree iterator to be null. The list
might be empty and the tree iterator might return hasNext false. This
kind of imprecise description of your problems aren't going to help get
the answers...
>
> What am I doing wrong please ?
Given that EMF generates an RCP editor, wouldn't it be good to look at a
working generated example?
>
> Thanks again.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Custom serialization [message #425705 is a reply to message #425699] Fri, 05 December 2008 11:16 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Rodolphe,

I'm doubtful that the code you showed works well, but that's nice if it
does...


Rodolphe Beck wrote:
> Ok, it is because my domain file is not in the same place as my
> diagram file.
>
> So I activated the option "Same file for diagram and model",
> everything works perfectly.
>
> Thanks again for your help.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Custom serialization [message #425738 is a reply to message #425703] Fri, 05 December 2008 17:50 Go to previous messageGo to next message
Rodolphe Beck is currently offline Rodolphe BeckFriend
Messages: 18
Registered: July 2009
Junior Member
Ed Merks wrote:

> Rodolphe,

> Comments below.


> Rodolphe Beck wrote:
>> Ed Merks wrote:
>>
>>> Rodolphe,
>>
>>> Comments below.
>>
>>
>>> Rodolphe Beck wrote:
>>>> Ok my message wass totaly unclear sorry about that...
>>>>
>>>> I changed my code into :
>>>> http://pastebin.com/m7ebe537d
>>>>
>>>> And I managed to have my resource. Then, when I wanted to get the
>>>> content of my file, so I tried :
>>>> TreeIterator<EObject> ti = instanceResource.getAllContents();
>>>> But my treeiterator was empty...
>>>>
>>>> With : EList<EObject> list = instanceResource.getContents();
>>>> It works.
>>> That doesn't make must sense. The former walks the whole tree and
>>> the latter walks just the roots, so the former should always include
>>> the latter...
>> Yes, but in my case it doesn't :)
> So you claim, but it seems impossible to me for ti.hasNext() not to
> return precisely the value of
> instanceResource.getContents().iterator().hasNext() the first time it's
> called because that's how the tree iterator is implemented...
The proof then :
http://img229.imageshack.us/my.php?image=treeiteratorfu3.jpg

My final code :

http://pastebin.com/m503cce28

About the debug thing, that is how I solved my problems actually.

Concerning the in memory access of objects, I was thinking of accessing
the same registry as EMF/GMF.

Here I create a diagram with EMF/GMF, when I hit ctrl+s my objects are
serialized from "somewhere in memory" to my diagram file.
Then when I want to export in another format, I parse the diagram file
manually to get the objects and serialize them in another format.
In memory access would be accessing the objects withour re-parsing the
file (as it is already managed by EMF/GMF)
Re: Custom serialization [message #425742 is a reply to message #425738] Fri, 05 December 2008 19:07 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Rodolphe,

Comments below.

Rodolphe Beck wrote:
> Ed Merks wrote:
>
>> Rodolphe,
>
>> Comments below.
>
>
>> Rodolphe Beck wrote:
>>> Ed Merks wrote:
>>>
>>>> Rodolphe,
>>>
>>>> Comments below.
>>>
>>>
>>>> Rodolphe Beck wrote:
>>>>> Ok my message wass totaly unclear sorry about that...
>>>>>
>>>>> I changed my code into :
>>>>> http://pastebin.com/m7ebe537d
>>>>>
>>>>> And I managed to have my resource. Then, when I wanted to get the
>>>>> content of my file, so I tried :
>>>>> TreeIterator<EObject> ti = instanceResource.getAllContents();
>>>>> But my treeiterator was empty...
>>>>>
>>>>> With : EList<EObject> list = instanceResource.getContents();
>>>>> It works.
>>>> That doesn't make must sense. The former walks the whole tree and
>>>> the latter walks just the roots, so the former should always
>>>> include the latter...
>>> Yes, but in my case it doesn't :)
>> So you claim, but it seems impossible to me for ti.hasNext() not to
>> return precisely the value of
>> instanceResource.getContents().iterator().hasNext() the first time
>> it's called because that's how the tree iterator is implemented...
> The proof then :
> http://img229.imageshack.us/my.php?image=treeiteratorfu3.jpg
Well, the TreeIterator is actually implements as a list (stack) of
iterators, so looking at the toString version before calling hasNext
will show you the same thing as an empty list, but that's not proof that
hasNext will return false nor that next() will not return the first
object in the root of the resource.
>
> My final code :
>
> http://pastebin.com/m503cce28
>
> About the debug thing, that is how I solved my problems actually.
>
> Concerning the in memory access of objects, I was thinking of
> accessing the same registry as EMF/GMF.
>
> Here I create a diagram with EMF/GMF, when I hit ctrl+s my objects are
> serialized from "somewhere in memory" to my diagram file.
From the objects contained in the resource managed by the editor.
> Then when I want to export in another format, I parse the diagram file
> manually to get the objects and serialize them in another format.
> In memory access would be accessing the objects withour re-parsing the
> file (as it is already managed by EMF/GMF)
You just need to get your hands on the resource objects in that already
open editor...


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Metamodel registered multiple times
Next Topic:[Teneo / CDO] how db notifications are listened to ?
Goto Forum:
  


Current Time: Tue Apr 16 14:45:22 GMT 2024

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

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

Back to the top