Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Copy between resources - Dangling References
Copy between resources - Dangling References [message #1009516] Fri, 15 February 2013 09:07 Go to next message
Ralph P is currently offline Ralph PFriend
Messages: 30
Registered: September 2010
Location: Frankfurt, Germany
Member
Hello,

I've implemented a mechanism to copy EObjects from one resource into another (via Drag & Drop). I'm basically using EcoreUtil#copyAll(Collection) and AddCommands.

It works fine as long as there are no EObjects in the target model that have the same ID as the ones that should be copied.
If that happens, I am trying to replace the existing EObjects with the new ones, but I failing here.

First I tried RemoveCommand -> CreateCommand. This seems to work at first but on validation I get "The feature [...] contains a dangling reference [...]" on every other EObject that references the removed existing one.
Then I tried the ReplaceCommand - same Problem.

How can I replace an EObject and fix all references to it?
Is there a way to completely replace every feature of an EObject (including containments) by using SetCommand(s)?

Thanks in advance!

Best regards,
Ralph
Re: Copy between resources - Dangling References [message #1009570 is a reply to message #1009516] Fri, 15 February 2013 10:29 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Ralph,

Comments below.

On 15/02/2013 10:07 AM, Ralph P wrote:
> Hello,
>
> I've implemented a mechanism to copy EObjects from one resource into
> another (via Drag & Drop). I'm basically using
> EcoreUtil#copyAll(Collection) and AddCommands.
>
> It works fine as long as there are no EObjects in the target model
> that have the same ID as the ones that should be copied.
> If that happens, I am trying to replace the existing EObjects with the
> new ones, but I failing here.
>
> First I tried RemoveCommand -> CreateCommand. This seems to work at
> first but on validation I get "The feature [...] contains a dangling
> reference [...]" on every other EObject that references the removed
> existing one.
Yes, you'd need to use a DeleteCommand, but that's probably not going to
do what you want either; I assume you'd want all those other things to
reference the replacement...
> Then I tried the ReplaceCommand - same Problem.
Yes, the incoming references to the thing you've replaced will still dangle.
>
> How can I replace an EObject and fix all references to it?
> Is there a way to completely replace every feature of an EObject
> (including containments) by using SetCommand(s)?
Yes, you'd have to have to compose such a SetCommand for each feature.
>
> Thanks in advance!
I think you'd want something very similar to what DeleteCommand does,
but instead of deleting the reference, you'd want to use a
ReplaceCommand to replace it with a reference to the replacement.
>
> Best regards,
> Ralph
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Copy between resources - Dangling References [message #1009668 is a reply to message #1009570] Fri, 15 February 2013 13:48 Go to previous messageGo to next message
Ralph P is currently offline Ralph PFriend
Messages: 30
Registered: September 2010
Location: Frankfurt, Germany
Member
Hi Ed,

thanks for your helpful hints!!
It took me some tries but I finally have a solution that works like a charm.

I'm using the UsageCrossReferencer to find all references on the object that needs to be replaced (code below) and replace those references by using SetCommands.

EcoreUtil.UsageCrossReferencer.findAll(Collections.singleton(pExistingObject),
						pTargetEditingDomain.getResourceSet())


This leads to a command chain that looks like this:

  1. RemoveCommand for the object that needs to be replaced
  2. AddCommand to create the replacement
  3. Several SetCommands for every reference to the old object


Best regards,
Ralph
Re: Copy between resources - Dangling References [message #1009673 is a reply to message #1009668] Fri, 15 February 2013 13:58 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Ralph,

Just be careful with multi-valued references that you're modifying just
the one object, and not replacing all objects with the one new object.


On 15/02/2013 2:48 PM, Ralph P wrote:
> Hi Ed,
>
> thanks for your helpful hints!!
> It took me some tries but I finally have a solution that works like a
> charm.
>
> I'm using the UsageCrossReferencer to find all references on the
> object that needs to be replaced (code below) and replace those
> references by using SetCommands.
>
> EcoreUtil.UsageCrossReferencer.findAll(Collections.singleton(pExistingObject),
>
> pTargetEditingDomain.getResourceSet())
>
> This leads to a command chain that looks like this:
>
> RemoveCommand for the object that needs to be replaced
> AddCommand to create the replacement
> Several SetCommands for every reference to the old object
>
>
> Best regards,
> Ralph


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Copy between resources - Dangling References [message #1009680 is a reply to message #1009673] Fri, 15 February 2013 14:06 Go to previous messageGo to next message
Ralph P is currently offline Ralph PFriend
Messages: 30
Registered: September 2010
Location: Frankfurt, Germany
Member
I see... That's why the DeleteCommand uses the RemoveCommand on "if (eStructuralFeature.isMany() {...})", right?

I will test that as well, many thanks!
Re: Copy between resources - Dangling References [message #1010111 is a reply to message #1009680] Sat, 16 February 2013 15:24 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Ralph,

Comments below.

On 15/02/2013 3:06 PM, Ralph P wrote:
> I see... That's why the DeleteCommand uses the RemoveCommand on "if
> (eStructuralFeature.isMany() {...})", right?
Yes, but that will remove the one object from that one feature. So be
careful with your set command that you specify the index.
>
> I will test that as well, many thanks!


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] Questions about CDO capabilities
Next Topic:Transaction notifications for undo/redo
Goto Forum:
  


Current Time: Fri Mar 29 12:35:00 GMT 2024

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

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

Back to the top