Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Does ChangeRecorder record inverted ChangeKind about ResourceChange?
Does ChangeRecorder record inverted ChangeKind about ResourceChange? [message #1149226] Tue, 22 October 2013 01:44 Go to next message
Hiroki Kondo is currently offline Hiroki KondoFriend
Messages: 47
Registered: July 2009
Member
Hi,

I'd like to use ChangeRecorder to recode the changes to the model.
When I ADD a model to resource, ResourceChange#getKind() returns "REMOVE".
Why does it return "REMOVE"? If it correct behavior, how should I use ChangeRecorder?
If ResourceChange is deprecated, What classes should I use?

I wrote a test code about this in JUnit4.

package org.eclipse.emf.ecore.change.util;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.change.ChangeDescription;
import org.eclipse.emf.ecore.change.ChangeKind;
import org.eclipse.emf.ecore.change.ListChange;
import org.eclipse.emf.ecore.change.ResourceChange;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.junit.Test;

public class ChangeRecorderLearningTest {
	
	@Test
	public void addAModelToResource() throws Exception {
		Resource resource = new ResourceImpl(URI.createURI("foo"));
		ChangeRecorder recorder = new ChangeRecorder(resource);
		EcoreFactory factory = EcoreFactory.eINSTANCE;
		EPackage pack = factory.createEPackage();
		resource.getContents().add(pack);
		ChangeDescription description = recorder.endRecording();

		EList<ResourceChange> resourceChanges = description.getResourceChanges();
		for (ResourceChange resourceChange : resourceChanges) {
			EList<ListChange> listChanges = resourceChange.getListChanges();
			for (ListChange listChange : listChanges) {
				assertThat(listChange.getKind(),is(ChangeKind.ADD_LITERAL)); // FAIL
			}
		}
	}
}


Best Regards,
Re: Does ChangeRecorder record inverted ChangeKind about ResourceChange? [message #1149269 is a reply to message #1149226] Tue, 22 October 2013 02:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30372
Registered: July 2009
Senior Member
Hiroki,

Comments below.

On 22/10/2013 3:44 AM, Hiroki Kondo wrote:
> Hi,
>
> I'd like to use ChangeRecorder to recode the changes to the model.
> When I ADD a model to resource, ResourceChange#getKind() returns
> "REMOVE".
> Why does it return "REMOVE"? If it correct behavior, how should I use
> ChangeRecorder?
Keep in mind that a change recorder produces a delta relative to the
current, i.e., final state, of the model so it's a reverse delta if you
want to interpret it relative to the original state of the model. So
yes, every add to the model should produce a remove in the deleta and
every remove from the model should produce an add in the delta.

You can have a look at
org.eclipse.emf.test.core.change.ChangeDescriptionReverseTest.TestHelper.doit()
to see how to compute a forward delta, i.e., like this. So it's
important to call setEObjectToProxyURIMap so it's populated during
change recording and to call copyAndReverse on the description to
reverse it (without actually reversing the changes to the model's state).

abstract class TestHelper
{
void doit() throws Exception
{
ResourceSet originalResourceSet = new ResourceSetImpl();

loadResources(originalResourceSet);

Map<EObject, URI> eObjectToProxyURIMap = new HashMap<EObject, URI>();
ChangeRecorder changeRecorder = new ChangeRecorder();
changeRecorder.setRecordingTransientFeatures(false);
changeRecorder.setEObjectToProxyURIMap(eObjectToProxyURIMap);
changeRecorder.beginRecording(Collections.singleton(originalResourceSet));

makeChanges();

ChangeDescription changeDescription = changeRecorder.endRecording();
changeDescription.copyAndReverse(eObjectToProxyURIMap);


> If ResourceChange is deprecated, What classes should I use?
>
> I wrote a test code about this in JUnit4.
>
>
> package org.eclipse.emf.ecore.change.util;
>
> import static org.hamcrest.CoreMatchers.is;
> import static org.junit.Assert.assertThat;
>
> import org.eclipse.emf.common.util.EList;
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.ecore.EcoreFactory;
> import org.eclipse.emf.ecore.change.ChangeDescription;
> import org.eclipse.emf.ecore.change.ChangeKind;
> import org.eclipse.emf.ecore.change.ListChange;
> import org.eclipse.emf.ecore.change.ResourceChange;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
> import org.junit.Test;
>
> public class ChangeRecorderLearningTest {
>
> @Test
> public void addAModelToResource() throws Exception {
> Resource resource = new ResourceImpl(URI.createURI("foo"));
> ChangeRecorder recorder = new ChangeRecorder(resource);
> EcoreFactory factory = EcoreFactory.eINSTANCE;
> EPackage pack = factory.createEPackage();
> resource.getContents().add(pack);
> ChangeDescription description = recorder.endRecording();
>
> EList<ResourceChange> resourceChanges =
> description.getResourceChanges();
> for (ResourceChange resourceChange : resourceChanges) {
> EList<ListChange> listChanges =
> resourceChange.getListChanges();
> for (ListChange listChange : listChanges) {
> assertThat(listChange.getKind(),is(ChangeKind.ADD_LITERAL)); // FAIL
> }
> }
> }
> }
>
>
> Best Regards,
Re: Does ChangeRecorder record inverted ChangeKind about ResourceChange? [message #1149311 is a reply to message #1149269] Tue, 22 October 2013 03:04 Go to previous message
Hiroki Kondo is currently offline Hiroki KondoFriend
Messages: 47
Registered: July 2009
Member
Hi Ed,

Thank you for the details. I understand the behavior.

Best Regards,
Previous Topic:Multiple Inheritance
Next Topic:XSD to ECore generation: 'group' attribute for every 'xsd:choice'
Goto Forum:
  


Current Time: Sun Aug 25 05:28:47 GMT 2019

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

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

Back to the top