Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EcoreUtil.Copier and change of explicit UUIDs
EcoreUtil.Copier and change of explicit UUIDs [message #1795557] Tue, 25 September 2018 09:12 Go to next message
Nieves Salor is currently offline Nieves SalorFriend
Messages: 19
Registered: September 2013
Junior Member
Hi,
I am trying to clone an ecore resource (which may have both input/output cross dependencies with other resources)
Now I am doing EcoreUtil.copy(rsc.getContents().get(0)) for copying the root and it works great. However I realised the ID fields of the objects (which are UUIDs) is also copied, so basically it is the same in the originalRsc and the cloneRsc.
I know this makes sense because at the end I am using explicit ID field as part of my model. And also that it is not a problem within EMF to identify based on the fragment which object I am taking about even if 2 objects in different resources have the same ID.

However, I would need the IDs are changed and not copied when the cloning is done because I have the requirement of ID uniquess across resources in my app.
I know I can use a Copier (I guess copying not only the root object, but copying allContents) and then the clonedRsc.setID(oldObject, newUUID) method.
My questions are:


  • I traverse only all the entries without checking type?
  • Do I need for references as well?
  • Do I need to explicitly copy getAllContents instead of getContents?
  • If I use the EcoreUtils.UUID.generate, the resulting will be a non-used one from the loaded resources? because I don't see anything in the code that guarantees that.

    Thanks in advance

Re: EcoreUtil.Copier and change of explicit UUIDs [message #1795567 is a reply to message #1795557] Tue, 25 September 2018 11:13 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
You could do something like this:
	public static <T extends EObject> T copy(T eObject) {
		EcoreUtil.Copier copier = new EcoreUtil.Copier() {
			@Override
			protected void copyAttributeValue(EAttribute eAttribute, EObject eObject, Object value, Setting setting) {
				if (eAttribute == MyPackage.Literals.MY_OBJECT__ID) {
					value = EcoreUtil.generateUUID();
				}
				super.copyAttributeValue(eAttribute, eObject, value, setting);
			}

		};
		EObject result = copier.copy(eObject);
		copier.copyReferences();

		@SuppressWarnings("unchecked")
		T t = (T) result;
		return t;
	}
I.e., specialize the Copier and look for the specific attribute for which you don't want the value to be the same as the original but rather a new UUID.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EcoreUtil.Copier and change of explicit UUIDs [message #1795620 is a reply to message #1795567] Wed, 26 September 2018 09:05 Go to previous message
Nieves Salor is currently offline Nieves SalorFriend
Messages: 19
Registered: September 2013
Junior Member
Thanks Ed!
I will test it :)
Previous Topic:Measuring execution time and memory usage in QVTD
Next Topic:Remove item from list using its containment Setting
Goto Forum:
  


Current Time: Thu Mar 28 15:34:28 GMT 2024

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

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

Back to the top