Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Copying non-containment references
Copying non-containment references [message #661777] Sun, 27 March 2011 19:04 Go to next message
Vinny  is currently offline Vinny Friend
Messages: 20
Registered: July 2009
Junior Member
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 12:15]

Report message to a moderator

Re: Copying non-containment references [message #661796 is a reply to message #661777] Sun, 27 March 2011 22:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30546
Registered: July 2009
Senior Member
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 #661880 is a reply to message #661796] Mon, 28 March 2011 12:03 Go to previous messageGo to next message
Vinny  is currently offline Vinny Friend
Messages: 20
Registered: July 2009
Junior Member
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.
Re: Copying non-containment references [message #661979 is a reply to message #661880] Mon, 28 March 2011 17:37 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30546
Registered: July 2009
Senior Member
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 #662118 is a reply to message #661979] Tue, 29 March 2011 12:39 Go to previous messageGo to next message
Vinny  is currently offline Vinny Friend
Messages: 20
Registered: July 2009
Junior Member
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().


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.

Re: Copying non-containment references [message #662168 is a reply to message #662118] Tue, 29 March 2011 15:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30546
Registered: July 2009
Senior Member
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 #662188 is a reply to message #662168] Tue, 29 March 2011 16:22 Go to previous messageGo to next message
Vinny  is currently offline Vinny Friend
Messages: 20
Registered: July 2009
Junior Member
Yes, this is exactly where I'm going with this now. The issue I'm seeing is, walking through the debugger, is with the copyReferences() call.

I have:
- eObjA, eObjB and eObjC are EObjects
- eObjA has a 'containment' reference to eObjB and eObjB has a 'non-containment' reference to eObjC.

I'm doing:

myCollection.add(eObjC); //this is the line I added
myCollection.add(eObjA);

Then I do a:

Collection<EObject> copiedMyCollection = ECoreUtil.copyAll(myCollection);

tracing through the call:

copier.copyReferences();

shows that eObjB still gets set up to pointing to the original eObjC object. There is no copying of eObjC happening. I figured by adding eObjC to my collection before I copied the collection, would make the reference point to that object and not the original. What I need is for eObjC to be copied over to the new resource area and then have eObjB point to it. I can do all this through added logic but don't want to add lots of unique code if the copier class will do it for me.


Thanks again for the ideas.
Re: Copying non-containment references [message #662218 is a reply to message #662188] Tue, 29 March 2011 18:30 Go to previous message
Vinny  is currently offline Vinny Friend
Messages: 20
Registered: July 2009
Junior Member
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.



Previous Topic:Creating child objects of a type from another model?
Next Topic:[CDO] recover from lost connection
Goto Forum:
  


Current Time: Wed Oct 16 06:13:49 GMT 2019

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

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

Back to the top