Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Loading a model from XMI into an already existing object(EMF Persistence question)
Loading a model from XMI into an already existing object [message #1268556] Mon, 10 March 2014 17:04 Go to next message
Ken Keefe is currently offline Ken KeefeFriend
Messages: 38
Registered: September 2009
Member
Hello. I am using EMF persistence to save a load my models. I am wondering if it is possible to load a model from an XMI file and set the fields of an already created object.

So, right now, I see how to do this:

resource.load(null);
MyObject mo = (MyObject)resource.getContents().get(0);


This will create a brand new MyObject object. What I'd like to do is be able to pass it a MyObject object and have it overwrite any fields it reads from the XMI in the resource.

Maybe something like:

resource.load(mo);

Re: Loading a model from XMI into an already existing object [message #1268569 is a reply to message #1268556] Mon, 10 March 2014 17:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Ken,

Comments below.

On 10/03/2014 6:04 PM, Ken Mising name wrote:
> Hello. I am using EMF persistence to save a load my models. I am
> wondering if it is possible to load a model from an XMI file and set
> the fields of an already created object.
No, not really. Deserialization creates new objects...
>
> So, right now, I see how to do this:
>
> resource.load(null);
> MyObject mo = (MyObject)resource.getContents().get(0);
>
> This will create a brand new MyObject object. What I'd like to do is
> be able to pass it a MyObject object and have it overwrite any fields
> it reads from the XMI in the resource.
You can transfer the values programmatically from that new object using
the generated APIs, or reflectively using eGet and eSet on all the
features...
>
> Maybe something like:
>
> resource.load(mo);
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Loading a model from XMI into an already existing object [message #1268587 is a reply to message #1268569] Mon, 10 March 2014 18:03 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

A neat trick that the UML2 project's package merger uses is to
initialize an EcoreUtil.Copier with a mapping of existing objects to
"copies" that aren't actually copies but pre-existing objects. Then
running the copier transfers the values of features and copies
contained objects from the "original" objects to the "copies".

So, feeding a (loaded-object, mo) mapping into a copier and performing
the copy might be a place to start ...

HTH,

Christian


On 2014-03-10 17:25:56 +0000, Ed Merks said:

> Ken,
>
> Comments below.
>
> On 10/03/2014 6:04 PM, Ken Mising name wrote:
>> Hello. I am using EMF persistence to save a load my models. I am
>> wondering if it is possible to load a model from an XMI file and set
>> the fields of an already created object.
> No, not really. Deserialization creates new objects...
>>
>> So, right now, I see how to do this:
>>
>> resource.load(null);
>> MyObject mo = (MyObject)resource.getContents().get(0);
>>
>> This will create a brand new MyObject object. What I'd like to do is be
>> able to pass it a MyObject object and have it overwrite any fields it
>> reads from the XMI in the resource.
> You can transfer the values programmatically from that new object using
> the generated APIs, or reflectively using eGet and eSet on all the
> features...
>>
>> Maybe something like:
>>
>> resource.load(mo);
Re: Loading a model from XMI into an already existing object [message #1268594 is a reply to message #1268587] Mon, 10 March 2014 18:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Christian,

That's a good trick. One aspect to be careful of in any case is to
unset a feature that won't otherwise be set when the new object (or
object being copied) has eIsSet false for that feature...

On 10/03/2014 7:03 PM, Christian W. Damus wrote:
> A neat trick that the UML2 project's package merger uses is to
> initialize an EcoreUtil.Copier with a mapping of existing objects to
> "copies" that aren't actually copies but pre-existing objects. Then
> running the copier transfers the values of features and copies
> contained objects from the "original" objects to the "copies".
>
> So, feeding a (loaded-object, mo) mapping into a copier and performing
> the copy might be a place to start ...
>
> HTH,
>
> Christian
>
>
> On 2014-03-10 17:25:56 +0000, Ed Merks said:
>
>> Ken,
>>
>> Comments below.
>>
>> On 10/03/2014 6:04 PM, Ken Mising name wrote:
>>> Hello. I am using EMF persistence to save a load my models. I am
>>> wondering if it is possible to load a model from an XMI file and set
>>> the fields of an already created object.
>> No, not really. Deserialization creates new objects...
>>>
>>> So, right now, I see how to do this:
>>>
>>> resource.load(null);
>>> MyObject mo = (MyObject)resource.getContents().get(0);
>>>
>>> This will create a brand new MyObject object. What I'd like to do is
>>> be able to pass it a MyObject object and have it overwrite any
>>> fields it reads from the XMI in the resource.
>> You can transfer the values programmatically from that new object
>> using the generated APIs, or reflectively using eGet and eSet on all
>> the features...
>>>
>>> Maybe something like:
>>>
>>> resource.load(mo);
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Loading a model from XMI into an already existing object [message #1268601 is a reply to message #1268594] Mon, 10 March 2014 18:21 Go to previous messageGo to next message
Ken Keefe is currently offline Ken KeefeFriend
Messages: 38
Registered: September 2009
Member
Thank you all for the quick reply. I'll look into these solutions.

Ken
Re: Loading a model from XMI into an already existing object [message #1268631 is a reply to message #1268594] Mon, 10 March 2014 19:23 Go to previous message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Ed,

Indeed, UML2's package merger is a zoo of special-case method overrides
for reasons just like this.

As usual, the problem with any generic solution is that ... it is generic. :-D

cW


On 2014-03-10 18:09:35 +0000, Ed Merks said:

> Christian,
>
> That's a good trick. One aspect to be careful of in any case is to
> unset a feature that won't otherwise be set when the new object (or
> object being copied) has eIsSet false for that feature...
>
> On 10/03/2014 7:03 PM, Christian W. Damus wrote:
>> A neat trick that the UML2 project's package merger uses is to
>> initialize an EcoreUtil.Copier with a mapping of existing objects to
>> "copies" that aren't actually copies but pre-existing objects. Then
>> running the copier transfers the values of features and copies
>> contained objects from the "original" objects to the "copies".
>>
>> So, feeding a (loaded-object, mo) mapping into a copier and performing
>> the copy might be a place to start ...
>>
>> HTH,
>>
>> Christian
>>
>>
>> On 2014-03-10 17:25:56 +0000, Ed Merks said:
>>
>>> Ken,
>>>
>>> Comments below.
>>>
>>> On 10/03/2014 6:04 PM, Ken Mising name wrote:
>>>> Hello. I am using EMF persistence to save a load my models. I am
>>>> wondering if it is possible to load a model from an XMI file and set
>>>> the fields of an already created object.
>>> No, not really. Deserialization creates new objects...
>>>>
>>>> So, right now, I see how to do this:
>>>>
>>>> resource.load(null);
>>>> MyObject mo = (MyObject)resource.getContents().get(0);
>>>>
>>>> This will create a brand new MyObject object. What I'd like to do is be
>>>> able to pass it a MyObject object and have it overwrite any fields it
>>>> reads from the XMI in the resource.
>>> You can transfer the values programmatically from that new object using
>>> the generated APIs, or reflectively using eGet and eSet on all the
>>> features...
>>>>
>>>> Maybe something like:
>>>>
>>>> resource.load(mo);
Previous Topic:[XCORE] How to refer to a particular attribute
Next Topic:Getting the org.eclipse.emf.ecore.resource.Resource from the Active Editor
Goto Forum:
  


Current Time: Thu Apr 25 12:06:06 GMT 2024

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

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

Back to the top