Home » Modeling » EMF » Copying non-containment references
Copying non-containment references [message #661777] |
Sun, 27 March 2011 15:04  |
Eclipse User |
|
|
|
I'm using EMF's ECoreUtil.Copier class to copy EObjects around. For the most part, out of the box, it works exactly as I need it to. The one thing I need that its not doing by default is to be able to copy objects that are referred to by non-containment. So, for example;
1. eObjA, eObjB and eObjC are EObjects
2. The files are all persisted in XMI format in a directory structure in dir1/
3. eObjA has a 'containment' reference to eObjB and eObjB has a 'non-containment' reference to eObjC.
I do a:
eObjA_copy = EcoreUtil.copyAll(eObjA);
then I do a:
AddCommand myAddCommand = AddCommand.create(/*stuff in here*/);
if (myAddCommand.canExecute()){
myEditingDomain.getCommandStack().execute (myAddCommand);
}
This works well by persisting all the copies into my new dir structure, called dir2/.
Everything looks good except for the fact that the new eObjB is pointing to the original eObjC object in dir1/. I want the eObjC object to be copied over to the new dir structure (dir2/) and then have eObjB point to that new copied object. This is the way all the containment references are working. I just need that to apply to the non-containment references as well. I think I need to override methods in the Copier class but not exactly sure where.
Any help would be greatly appreciated.
Thanks,
Vinny
[Updated on: Mon, 28 March 2011 08:15] by Moderator
|
|
|
Re: Copying non-containment references [message #661796 is a reply to message #661777] |
Sun, 27 March 2011 18:22   |
Eclipse User |
|
|
|
Vinny,
Collect all the root objects you want copied and then call copyAll with
that. E.g., if you all the files, copy the contents of all the
resources with a single call to copyAll. It might be easier to use
EcoreUtil.Copier directly, again, calling copy on each root and finally
calling copyReferences to hook up all the cross references.
Vinny wrote:
> I'm using EMF's ECoreUtil.Copier class to copy EObjects around. For
> the most part, out of the box, it works exactly as I need it to. The
> one thing I need that its not doing by default is to be able to copy
> objects that are referred to by non-containment. So, for example;
> 1. eObjA, eObjB and eObjC are EObjects
> 2. The files are all persisted in XMI format in a directory structure
> in dir1/
> 3. eObjA has a 'containment' reference to eObjB and eObjB has a
> 'non-containment' reference to eObjC.
> I do a:
> eObjA_copy = EcoreUtil.copyAll(A);
>
> then I do a:
>
> AddCommand myAddCommand = AddCommand.create(/*stuff in here*/);
> if (myAddCommand.canExecute()){
>
> myEditingDomain.getCommandStack().execute (myAddCommand);
>
> }
>
> This works well by persisting all the copies into my new dir
> structure, called dir2/.
> Everything looks good except for the fact that the new eObjB is
> pointing to the original eObjC object in dir1/. I want the eObjC
> object to be copied over to the new dir structure (dir2/) and then
> have eObjB point to that new copied object. This is the way all the
> containment references are working. I just need that to apply to the
> non-containment references as well. I think I need to override
> methods in the Copier class but not exactly sure where.
> Any help would be greatly appreciated.
>
> Thanks,
>
>
> Vinny
|
|
| |
Re: Copying non-containment references [message #661979 is a reply to message #661880] |
Mon, 28 March 2011 13:37   |
Eclipse User |
|
|
|
This is a multi-part message in MIME format.
--------------050405060409080605060003
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Vinny,
All I know about your add command is /*stuff in here*/ so I can't
possibly know what you're doing. You really need to consult with your
best friend, the debugger, to find out more about what's going on, i.e.,
why canExecute is returning false...
Vinny wrote:
> Thanks Ed. I'm trying that but after I add (even one) extra node to
> my collection, I get canExecute() returning false, so I cannot execute
> the command to see if the copy worked as expected. I'm tracing
> through it now but if you have any ideas, let me know.
--------------050405060409080605060003
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">
Vinny,<br>
<br>
All I know about your add command is /<b class="moz-txt-star"><span
class="moz-txt-tag">*</span>stuff in here<span class="moz-txt-tag">*</span></b>/
so I can't possibly know what you're doing. You really need to consult
with your best friend, the debugger, to find out more about what's
going on, i.e., why canExecute is returning false...<br>
<br>
<br>
Vinny wrote:
<blockquote cite="mid:impt1g$prn$1@news.eclipse.org" type="cite">Thanks
Ed. I'm trying that but after I add (even one) extra node to my
collection, I get canExecute() returning false, so I cannot execute the
command to see if the copy worked as expected. I'm tracing through it
now but if you have any ideas, let me know.
<br>
</blockquote>
</body>
</html>
--------------050405060409080605060003--
|
|
| |
Re: Copying non-containment references [message #662168 is a reply to message #662118] |
Tue, 29 March 2011 11:24   |
Eclipse User |
|
|
|
Vinny,
Comments below.
Vinny wrote:
> canExecute() is falling because, after adding eObjC to my collection
> before calling copy, the resulting object list that I pass to
> AddCommand contains mixed object types. Namely eObjC does not fit the
> feature type that I'm calling AddCommand with, so it fails at:
>
> if (!feature.getEType().isInstance(object)){
> return false;
> }
>
> in AddCommand.prepare().
That's what I would expect.
>
>
> I can call AddCommand.create() on eObjC separately to create it as the
> correct owner/feature type, but my original issue still stands, where
> eObjB is pointing to the original eObjC object.
It seems clear you need to separate out of the process of copying, from
the process of adding the results to various targets. Note that the
Copier is a Map from original objects to their copies, so you could use
that to first copy everything, and then deal with how you want to handle
the copies in terms of add commands.
>
>
|
|
| |
Re: Copying non-containment references [message #662218 is a reply to message #662188] |
Tue, 29 March 2011 14:30  |
Eclipse User |
|
|
|
I got it:
//create copier instance
Copier copier = new Copier(true, true);
//make the copies of eObjC. This puts the eObjC instances in the copier's map
Collection<EObject> copiedEObjC = copier.copyAll(eObjC);
//Need to actually create the new eObjC
Command createCopiedEObjCCommand = AddCommand.create(myEditingDomain, owner, featureOfOwner, copiedEObjC,CommandParameter.NO_INDEX);
if (createCopiedEObjCCommand.canExecute()) {
myEditingDomain.getCommandStack().execute(createCopiedEObjCC ommand);
}
//make the copy of the root
Collection<EObject> copiedEObjA = copier.copyAll(eObjA);
//sych up references. Tracing this through shows that eObjB points to newly copied eObjC (exactly what I wanted!)
copier.copyReferences();
//Need to actually create the new eObjA
Command createCopiedEObjACommand = AddCommand.create(myEditingDomain, owner, featureOfOwner, copiedEObjA,CommandParameter.NO_INDEX);
if (createCopiedEObjACommand.canExecute()) {
myEditingDomain.getCommandStack().execute(createCopiedEObjAC ommand);
}
At this point, the new dir2/ structure looks correct.
Thank you Ed for the ideas. I appreciate your time.
|
|
|
Goto Forum:
Current Time: Wed Jul 23 10:45:46 EDT 2025
Powered by FUDForum. Page generated in 0.04697 seconds
|