Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc) » How to copy eResources such that xmi ids are maintained across resources
How to copy eResources such that xmi ids are maintained across resources [message #115948] |
Wed, 26 March 2008 15:40  |
Eclipse User |
|
|
|
Originally posted by: kelapure.us.ibm.com
How do I copy an eResource (say oldResource) to a new eResource (say
newResource) such that the newResource keeps the same xmi ids of the model
eobjects persisted in the oldResource.
This is what I am currently doing ..
Resource oldResource = myEObject.eResource();
ResourceSet resourceSet = oldResource.getResourceSet();
URI originalUri = oldResource.getURI();
Resource newResource = resourceSet.createResource(originalUri);
newResource.setURI(newURI);
newResource.getContents().add(newDD);
newResource().save(new HashMap());
After creating and persisting the newResource I dont see the XMI ids of
the eObjects in oldResource copy over to the newResource.
Can someone please help. What do I need to do here ...?
Thanks in advance,
Rohit Kelapure
|
|
| | |
Re: How to copy eResources such that xmi ids are maintained across resources [message #115987 is a reply to message #115974] |
Wed, 26 March 2008 16:57   |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
Rohit,
Comments below.
Rohit Kelapure wrote:
> Ed,
>
> Thanks for the prompt response.
> I cant change the URI of the existing resource because my user
> application still needs access to the unchanged existing resource.
If you renamed the resource, that only affects the in-memory copy, not
what's out there in the backing store. In fact, you could still load
the old resource using the old URI and they you'd have two copies with
the same IDs but with different resource URIs.
>
> I want to copy the existing resource into a new resource and then add
> more model objects to the new resource and persist the new resource.
> When I persist the new resource I need the eobjects copied over from
> the existing resource to retain their xmi IDs.
Now I'm confused. If you are going to overwrite the existing resource,
then why change the URI? I must be missing something...
>
> Do I need to reflectively iterate over my existing Resource get the
> IDs of the eObjects and then set them on the equivalent eObjects in
> the new Resource. Is there an EMF API call that does this ...?
You could use Resource.getAllContents() to walk the tree, but a map from
EObject to ID, and then use that map to set the IDs of all the objects,
but I'm really wondering if you aren't doing something completely
unnecessary.
>
> --Thanks,
> Rohit
>
|
|
| |
Re: How to copy eResources such that xmi ids are maintained across resources [message #116024 is a reply to message #115987] |
Wed, 26 March 2008 22:29   |
Eclipse User |
|
|
|
Originally posted by: kelapure.us.ibm.com
Ed,
I cannot in any way change or overwrite the eObjects of the existing
resource. I need to create an exact in-memory copy of the existing
resource and make modifications to the copied resource.
Say..
Original/Existing resource is persisted as web.xml
New/Copied resource will be persisted as web_merged.xml
web_merged.xml consumes ALL the eObjects contained in web.xml. Other
eObjects will be added to the web_merged new resource.
Say web.xml contains...
<ejb-ref id="EJBRef_Stateful">
<ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
.....
</ejb-ref>
web_merged.xml ALSO needs to contain the same id for the ejb-ref eObject
<ejb-ref id="EJBRef_Stateful">
<ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
.....
</ejb-ref>
Do you think Resource.getAllContents()... is the way to go here ...?
--Thanks,
Rohit Kelapure
Ed Merks wrote:
> Rohit,
>
> Comments below.
>
> Rohit Kelapure wrote:
>> Ed,
>>
>> Thanks for the prompt response.
>> I cant change the URI of the existing resource because my user
>> application still needs access to the unchanged existing resource.
> If you renamed the resource, that only affects the in-memory copy, not
> what's out there in the backing store. In fact, you could still load
> the old resource using the old URI and they you'd have two copies with
> the same IDs but with different resource URIs.
>>
>> I want to copy the existing resource into a new resource and then add
>> more model objects to the new resource and persist the new resource.
>> When I persist the new resource I need the eobjects copied over from
>> the existing resource to retain their xmi IDs.
> Now I'm confused. If you are going to overwrite the existing resource,
> then why change the URI? I must be missing something...
>>
>> Do I need to reflectively iterate over my existing Resource get the
>> IDs of the eObjects and then set them on the equivalent eObjects in
>> the new Resource. Is there an EMF API call that does this ...?
> You could use Resource.getAllContents() to walk the tree, but a map from
> EObject to ID, and then use that map to set the IDs of all the objects,
> but I'm really wondering if you aren't doing something completely
> unnecessary.
>>
>> --Thanks,
>> Rohit
>>
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #116156 is a reply to message #116024] |
Thu, 27 March 2008 11:27   |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
Rohit,
Comments below.
Rohit Kelapure wrote:
>
> Ed,
>
> I cannot in any way change or overwrite the eObjects of the existing
> resource. I need to create an exact in-memory copy of the existing
> resource and make modifications to the copied resource.
Well, what you've shown is moving the EObjects from one resource to
another, so you've already violated your one constraint. You'd need to
make a copy of the original resource. How about if you create another
resource with the same old URI, load it, and then rename that duplicate
resource's URI? Then you'll have two copies and the IDs will be what
you want. Failing that, you'd need to use an EcoreUtil.Copier to copy
the EObjects.
>
>
> Say..
> Original/Existing resource is persisted as web.xml
> New/Copied resource will be persisted as web_merged.xml
>
> web_merged.xml consumes ALL the eObjects contained in web.xml. Other
> eObjects will be added to the web_merged new resource.
>
> Say web.xml contains...
> <ejb-ref id="EJBRef_Stateful">
> <ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
> <ejb-ref-type>Session</ejb-ref-type>
> .....
> </ejb-ref>
>
> web_merged.xml ALSO needs to contain the same id for the ejb-ref eObject
> <ejb-ref id="EJBRef_Stateful">
> <ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
> <ejb-ref-type>Session</ejb-ref-type>
> .....
> </ejb-ref>
>
> Do you think Resource.getAllContents()... is the way to go here ...?
>
> --Thanks,
> Rohit Kelapure
>
>
> Ed Merks wrote:
>> Rohit,
>>
>> Comments below.
>>
>> Rohit Kelapure wrote:
>>> Ed,
>>>
>>> Thanks for the prompt response.
>>> I cant change the URI of the existing resource because my user
>>> application still needs access to the unchanged existing resource.
>> If you renamed the resource, that only affects the in-memory copy,
>> not what's out there in the backing store. In fact, you could still
>> load the old resource using the old URI and they you'd have two
>> copies with the same IDs but with different resource URIs.
>>>
>>> I want to copy the existing resource into a new resource and then
>>> add more model objects to the new resource and persist the new
>>> resource. When I persist the new resource I need the eobjects copied
>>> over from the existing resource to retain their xmi IDs.
>> Now I'm confused. If you are going to overwrite the existing
>> resource, then why change the URI? I must be missing something...
>>>
>>> Do I need to reflectively iterate over my existing Resource get the
>>> IDs of the eObjects and then set them on the equivalent eObjects in
>>> the new Resource. Is there an EMF API call that does this ...?
>> You could use Resource.getAllContents() to walk the tree, but a map
>> from EObject to ID, and then use that map to set the IDs of all the
>> objects, but I'm really wondering if you aren't doing something
>> completely unnecessary.
>>>
>>> --Thanks,
>>> Rohit
>>>
|
|
| |
Re: How to copy eResources such that xmi ids are maintained across resources [message #116221 is a reply to message #116208] |
Thu, 27 March 2008 14:36  |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
Rohit,
Comments below.
Rohit Kelapure wrote:
> Thanks for guiding me ...This is what I have now ...
>
> //oldDD and newDD EObjects are copies of one another created using
> //ECoreUtil.copy()
> public void updateCopiedDDResource(EObject oldDD, EObject newDD){
> Resource oldResource = oldDD.eResource();
> ResourceSet resourceSet = oldResource.getResourceSet();
> URI originalUri = oldResource.getURI();
> Resource newResource = resourceSet.createResource(originalUri);
What if you just called newResource.load()?
> newResource.setURI(getMergedURI(originalURI));
This newDD is a copy?
> newResource.getContents().add(newDD);
> }
>
> Problem with this approach is that the
> Contents of ((XMLResource) oldResource).getEObjectToIDMap() and
> ((XMLResource) newResource).getEObjectToIDMap() don't contain the same
> type of EObjects.
Exactly.
If you look at how EcoreUtil.copy is implemented, you'll see this:
Copier copier = new Copier();
EObject result = copier.copy(eObject);
copier.copyReferences();
return result;
Note that the copier is a map that maps the original object to its copy,
so that would allow you to walk the original resource's objects, get the
ID for each, map that object to its copy, and then set the ID for the
copy in the new resource.
>
> This what I had intended to do copy the IDs over ...
> for (Map.Entry<EObject, String> entry : oldEObjectToID.entrySet()) {
> EObject eObj = entry.getKey();
> String id = entry.getValue();
> for (EObject eObjKey : newEObjectToID.keySet()) {
> if (EcoreUtil.equals(eObj, eObjKey)){
EObjects are only .equals if they are ==.
> if (null != id){
> (XMLResource) newResource).setID(eObjKey, id);
> }
> }
> }
> }
>
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #615996 is a reply to message #115948] |
Wed, 26 March 2008 15:54  |
Eclipse User |
|
|
|
Rohit,
This is purely an EMF question best asked on the EMF newsgroup. I'm not
sure why you wouldn't just change the URI of the existing resource.
XMLResource has methods like getID and setID that can be used to
determine the ID of the objects. You'd have to get to the old IDs
before you remove the objects from the old resource...
Rohit Kelapure wrote:
> How do I copy an eResource (say oldResource) to a new eResource (say
> newResource) such that the newResource keeps the same xmi ids of the
> model eobjects persisted in the oldResource.
>
> This is what I am currently doing ..
> Resource oldResource = myEObject.eResource();
> ResourceSet resourceSet = oldResource.getResourceSet();
> URI originalUri = oldResource.getURI();
> Resource newResource = resourceSet.createResource(originalUri);
> newResource.setURI(newURI);
> newResource.getContents().add(newDD);
> newResource().save(new HashMap());
>
> After creating and persisting the newResource I dont see the XMI ids
> of the eObjects in oldResource copy over to the newResource.
>
> Can someone please help. What do I need to do here ...?
>
> Thanks in advance,
> Rohit Kelapure
>
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #615997 is a reply to message #115963] |
Wed, 26 March 2008 16:40  |
Eclipse User |
|
|
|
Ed,
Thanks for the prompt response.
I cant change the URI of the existing resource because my user application
still needs access to the unchanged existing resource.
I want to copy the existing resource into a new resource and then add more
model objects to the new resource and persist the new resource. When I
persist the new resource I need the eobjects copied over from the existing
resource to retain their xmi IDs.
Do I need to reflectively iterate over my existing Resource get the IDs of
the eObjects and then set them on the equivalent eObjects in the new
Resource. Is there an EMF API call that does this ...?
--Thanks,
Rohit
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #615998 is a reply to message #115974] |
Wed, 26 March 2008 16:57  |
Eclipse User |
|
|
|
Rohit,
Comments below.
Rohit Kelapure wrote:
> Ed,
>
> Thanks for the prompt response.
> I cant change the URI of the existing resource because my user
> application still needs access to the unchanged existing resource.
If you renamed the resource, that only affects the in-memory copy, not
what's out there in the backing store. In fact, you could still load
the old resource using the old URI and they you'd have two copies with
the same IDs but with different resource URIs.
>
> I want to copy the existing resource into a new resource and then add
> more model objects to the new resource and persist the new resource.
> When I persist the new resource I need the eobjects copied over from
> the existing resource to retain their xmi IDs.
Now I'm confused. If you are going to overwrite the existing resource,
then why change the URI? I must be missing something...
>
> Do I need to reflectively iterate over my existing Resource get the
> IDs of the eObjects and then set them on the equivalent eObjects in
> the new Resource. Is there an EMF API call that does this ...?
You could use Resource.getAllContents() to walk the tree, but a map from
EObject to ID, and then use that map to set the IDs of all the objects,
but I'm really wondering if you aren't doing something completely
unnecessary.
>
> --Thanks,
> Rohit
>
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #615999 is a reply to message #115974] |
Wed, 26 March 2008 17:11  |
Eclipse User |
|
|
|
Originally posted by: cdamus.ca.ibm.com
Hi, Rohit,
Here's an idea:
1. Change the URI of your resource to the new URI.
2. Attach a ChangeRecorder to all of the contents of the resource.
3. Start recording.
4. Add your objects, make other changes.
5. Stop recording.
6. Save the resource.
7. Apply your change description obtained at step 5.
8. Change the URI back to its previous value.
9. Load the new resource from the new URI.
10. Voila! Your original resource is restored and you have a copy.
HTH,
Christian
Rohit Kelapure wrote:
> Ed,
>
> Thanks for the prompt response.
>
> I cant change the URI of the existing resource because my user application
> still needs access to the unchanged existing resource.
>
> I want to copy the existing resource into a new resource and then add more
> model objects to the new resource and persist the new resource. When I
> persist the new resource I need the eobjects copied over from the existing
> resource to retain their xmi IDs.
>
> Do I need to reflectively iterate over my existing Resource get the IDs of
> the eObjects and then set them on the equivalent eObjects in the new
> Resource. Is there an EMF API call that does this ...?
>
> --Thanks,
> Rohit
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #616001 is a reply to message #115987] |
Wed, 26 March 2008 22:29  |
Eclipse User |
|
|
|
Ed,
I cannot in any way change or overwrite the eObjects of the existing
resource. I need to create an exact in-memory copy of the existing
resource and make modifications to the copied resource.
Say..
Original/Existing resource is persisted as web.xml
New/Copied resource will be persisted as web_merged.xml
web_merged.xml consumes ALL the eObjects contained in web.xml. Other
eObjects will be added to the web_merged new resource.
Say web.xml contains...
<ejb-ref id="EJBRef_Stateful">
<ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
.....
</ejb-ref>
web_merged.xml ALSO needs to contain the same id for the ejb-ref eObject
<ejb-ref id="EJBRef_Stateful">
<ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
.....
</ejb-ref>
Do you think Resource.getAllContents()... is the way to go here ...?
--Thanks,
Rohit Kelapure
Ed Merks wrote:
> Rohit,
>
> Comments below.
>
> Rohit Kelapure wrote:
>> Ed,
>>
>> Thanks for the prompt response.
>> I cant change the URI of the existing resource because my user
>> application still needs access to the unchanged existing resource.
> If you renamed the resource, that only affects the in-memory copy, not
> what's out there in the backing store. In fact, you could still load
> the old resource using the old URI and they you'd have two copies with
> the same IDs but with different resource URIs.
>>
>> I want to copy the existing resource into a new resource and then add
>> more model objects to the new resource and persist the new resource.
>> When I persist the new resource I need the eobjects copied over from
>> the existing resource to retain their xmi IDs.
> Now I'm confused. If you are going to overwrite the existing resource,
> then why change the URI? I must be missing something...
>>
>> Do I need to reflectively iterate over my existing Resource get the
>> IDs of the eObjects and then set them on the equivalent eObjects in
>> the new Resource. Is there an EMF API call that does this ...?
> You could use Resource.getAllContents() to walk the tree, but a map from
> EObject to ID, and then use that map to set the IDs of all the objects,
> but I'm really wondering if you aren't doing something completely
> unnecessary.
>>
>> --Thanks,
>> Rohit
>>
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #616011 is a reply to message #116024] |
Thu, 27 March 2008 11:27  |
Eclipse User |
|
|
|
Rohit,
Comments below.
Rohit Kelapure wrote:
>
> Ed,
>
> I cannot in any way change or overwrite the eObjects of the existing
> resource. I need to create an exact in-memory copy of the existing
> resource and make modifications to the copied resource.
Well, what you've shown is moving the EObjects from one resource to
another, so you've already violated your one constraint. You'd need to
make a copy of the original resource. How about if you create another
resource with the same old URI, load it, and then rename that duplicate
resource's URI? Then you'll have two copies and the IDs will be what
you want. Failing that, you'd need to use an EcoreUtil.Copier to copy
the EObjects.
>
>
> Say..
> Original/Existing resource is persisted as web.xml
> New/Copied resource will be persisted as web_merged.xml
>
> web_merged.xml consumes ALL the eObjects contained in web.xml. Other
> eObjects will be added to the web_merged new resource.
>
> Say web.xml contains...
> <ejb-ref id="EJBRef_Stateful">
> <ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
> <ejb-ref-type>Session</ejb-ref-type>
> .....
> </ejb-ref>
>
> web_merged.xml ALSO needs to contain the same id for the ejb-ref eObject
> <ejb-ref id="EJBRef_Stateful">
> <ejb-ref-name>ejb/BasicCMTRemote</ejb-ref-name>
> <ejb-ref-type>Session</ejb-ref-type>
> .....
> </ejb-ref>
>
> Do you think Resource.getAllContents()... is the way to go here ...?
>
> --Thanks,
> Rohit Kelapure
>
>
> Ed Merks wrote:
>> Rohit,
>>
>> Comments below.
>>
>> Rohit Kelapure wrote:
>>> Ed,
>>>
>>> Thanks for the prompt response.
>>> I cant change the URI of the existing resource because my user
>>> application still needs access to the unchanged existing resource.
>> If you renamed the resource, that only affects the in-memory copy,
>> not what's out there in the backing store. In fact, you could still
>> load the old resource using the old URI and they you'd have two
>> copies with the same IDs but with different resource URIs.
>>>
>>> I want to copy the existing resource into a new resource and then
>>> add more model objects to the new resource and persist the new
>>> resource. When I persist the new resource I need the eobjects copied
>>> over from the existing resource to retain their xmi IDs.
>> Now I'm confused. If you are going to overwrite the existing
>> resource, then why change the URI? I must be missing something...
>>>
>>> Do I need to reflectively iterate over my existing Resource get the
>>> IDs of the eObjects and then set them on the equivalent eObjects in
>>> the new Resource. Is there an EMF API call that does this ...?
>> You could use Resource.getAllContents() to walk the tree, but a map
>> from EObject to ID, and then use that map to set the IDs of all the
>> objects, but I'm really wondering if you aren't doing something
>> completely unnecessary.
>>>
>>> --Thanks,
>>> Rohit
>>>
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #616015 is a reply to message #116156] |
Thu, 27 March 2008 13:32  |
Eclipse User |
|
|
|
Thanks for guiding me ...This is what I have now ...
//oldDD and newDD EObjects are copies of one another created using
//ECoreUtil.copy()
public void updateCopiedDDResource(EObject oldDD, EObject newDD){
Resource oldResource = oldDD.eResource();
ResourceSet resourceSet = oldResource.getResourceSet();
URI originalUri = oldResource.getURI();
Resource newResource = resourceSet.createResource(originalUri);
newResource.setURI(getMergedURI(originalURI));
newResource.getContents().add(newDD);
}
Problem with this approach is that the
Contents of ((XMLResource) oldResource).getEObjectToIDMap() and
((XMLResource) newResource).getEObjectToIDMap() don't contain the same
type of EObjects.
This what I had intended to do copy the IDs over ...
for (Map.Entry<EObject, String> entry : oldEObjectToID.entrySet()) {
EObject eObj = entry.getKey();
String id = entry.getValue();
for (EObject eObjKey : newEObjectToID.keySet()) {
if (EcoreUtil.equals(eObj, eObjKey)){
if (null != id){
(XMLResource) newResource).setID(eObjKey, id);
}
}
}
}
|
|
|
Re: How to copy eResources such that xmi ids are maintained across resources [message #616016 is a reply to message #116208] |
Thu, 27 March 2008 14:36  |
Eclipse User |
|
|
|
Rohit,
Comments below.
Rohit Kelapure wrote:
> Thanks for guiding me ...This is what I have now ...
>
> //oldDD and newDD EObjects are copies of one another created using
> //ECoreUtil.copy()
> public void updateCopiedDDResource(EObject oldDD, EObject newDD){
> Resource oldResource = oldDD.eResource();
> ResourceSet resourceSet = oldResource.getResourceSet();
> URI originalUri = oldResource.getURI();
> Resource newResource = resourceSet.createResource(originalUri);
What if you just called newResource.load()?
> newResource.setURI(getMergedURI(originalURI));
This newDD is a copy?
> newResource.getContents().add(newDD);
> }
>
> Problem with this approach is that the
> Contents of ((XMLResource) oldResource).getEObjectToIDMap() and
> ((XMLResource) newResource).getEObjectToIDMap() don't contain the same
> type of EObjects.
Exactly.
If you look at how EcoreUtil.copy is implemented, you'll see this:
Copier copier = new Copier();
EObject result = copier.copy(eObject);
copier.copyReferences();
return result;
Note that the copier is a map that maps the original object to its copy,
so that would allow you to walk the original resource's objects, get the
ID for each, map that object to its copy, and then set the ID for the
copy in the new resource.
>
> This what I had intended to do copy the IDs over ...
> for (Map.Entry<EObject, String> entry : oldEObjectToID.entrySet()) {
> EObject eObj = entry.getKey();
> String id = entry.getValue();
> for (EObject eObjKey : newEObjectToID.keySet()) {
> if (EcoreUtil.equals(eObj, eObjKey)){
EObjects are only .equals if they are ==.
> if (null != id){
> (XMLResource) newResource).setID(eObjKey, id);
> }
> }
> }
> }
>
|
|
|
Goto Forum:
Current Time: Mon May 12 12:51:39 EDT 2025
Powered by FUDForum. Page generated in 0.05205 seconds
|