|
Re: Does ChangeRecorder record inverted ChangeKind about ResourceChange? [message #1149269 is a reply to message #1149226] |
Tue, 22 October 2013 02:24 |
Ed Merks Messages: 33113 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,
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Powered by
FUDForum. Page generated in 0.04360 seconds