Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Moving model elements into a new model instance
Moving model elements into a new model instance [message #481203] Thu, 20 August 2009 07:04 Go to next message
Jevon  is currently offline Jevon Friend
Messages: 164
Registered: July 2009
Senior Member
Hi,

I wanted to write a command to move an EMF model element instance into a
new file, yet keep all of its references from the old file(s) -- a cheap
way of refactoring a model instance. I found that the following code
does this:

EObject object = ...;
TransactionalEditingDomain domain = ...;

// within a GMF AbstractTransactionalCommand
URI uri = ...;
Resource modelResource = domain.getResourceSet().createResource(uri);
modelResource.getContents().add(object);
modelResource.save(...);

It seems that's all I need to do! I don't have to modify the source
model instance in any way, and the new model remain in sync with the
original model (i.e. I can edit properties/connections of one and they
reflect on the other). It also works recursively, and works beautifully
with GMF.

My question is, is this expected behaviour? It seemed to work too
easily. :-)

Thanks,

Jevon
Re: Moving model elements into a new model instance [message #481257 is a reply to message #481203] Thu, 20 August 2009 09:17 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Jevon,

Comments below.

Jevon Wright wrote:
> Hi,
>
> I wanted to write a command to move an EMF model element instance into
> a new file, yet keep all of its references from the old file(s) -- a
> cheap way of refactoring a model instance. I found that the following
> code does this:
>
> EObject object = ...;
> TransactionalEditingDomain domain = ...;
>
> // within a GMF AbstractTransactionalCommand
> URI uri = ...;
> Resource modelResource = domain.getResourceSet().createResource(uri);
> modelResource.getContents().add(object);
> modelResource.save(...);
>
> It seems that's all I need to do! I don't have to modify the source
> model instance in any way, and the new model remain in sync with the
> original model (i.e. I can edit properties/connections of one and they
> reflect on the other). It also works recursively, and works
> beautifully with GMF.
>
> My question is, is this expected behaviour? It seemed to work too
> easily. :-)
Other than the fact that object.eContainer might be != null before you
add it to the resource contents and be == null after, I'd certainly
expect it to work. If object.eContainmentReference().isResolveProxies()
is true, then I'd expect the eContainer to be preserved.
>
> Thanks,
>
> Jevon


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Moving model elements into a new model instance [message #481269 is a reply to message #481257] Thu, 20 August 2009 09:23 Go to previous message
Jevon  is currently offline Jevon Friend
Messages: 164
Registered: July 2009
Senior Member
Ed,

Thanks for the info, that's amazing! The Ecore editor automatically sets
Resolve Proxies to 'true' by default, it seems.

Jevon

Ed Merks wrote:
> Jevon,
>
> Comments below.
>
> Jevon Wright wrote:
>> Hi,
>>
>> I wanted to write a command to move an EMF model element instance into
>> a new file, yet keep all of its references from the old file(s) -- a
>> cheap way of refactoring a model instance. I found that the following
>> code does this:
>>
>> EObject object = ...;
>> TransactionalEditingDomain domain = ...;
>>
>> // within a GMF AbstractTransactionalCommand
>> URI uri = ...;
>> Resource modelResource = domain.getResourceSet().createResource(uri);
>> modelResource.getContents().add(object);
>> modelResource.save(...);
>>
>> It seems that's all I need to do! I don't have to modify the source
>> model instance in any way, and the new model remain in sync with the
>> original model (i.e. I can edit properties/connections of one and they
>> reflect on the other). It also works recursively, and works
>> beautifully with GMF.
>>
>> My question is, is this expected behaviour? It seemed to work too
>> easily. :-)
> Other than the fact that object.eContainer might be != null before you
> add it to the resource contents and be == null after, I'd certainly
> expect it to work. If object.eContainmentReference().isResolveProxies()
> is true, then I'd expect the eContainer to be preserved.
>>
>> Thanks,
>>
>> Jevon
Previous Topic:IMPORTs in "Getting Started with UML2" caused errors
Next Topic:Load / Reference Resource programmatically
Goto Forum:
  


Current Time: Fri Apr 26 18:32:57 GMT 2024

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

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

Back to the top