package tools.vitruv.extensions.constructionsimulation.traversal.util;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;

import tools.vitruv.framework.change.echange.EChange;
import tools.vitruv.framework.change.echange.feature.reference.InsertEReference;
import tools.vitruv.framework.change.echange.feature.reference.ReferenceFactory;

/**
 * A helper class that provides methods for creating atomic change models from EMF elements
 *
 * @author Sven Leonhardt
 */
public abstract class ChangeBuildHelper {

    /**
     * This method creates a list which contains a single EChange element.
     *
     * @param source
     *            : The element which was added
     * @return : EList containing one EChange element
     */
    protected static EChange createSingleAddNonRootEObjectInListChange(final EObject source) {

        final InsertEReference<EObject, EObject> change = ReferenceFactory.eINSTANCE
                .createInsertEReference();
        final EObject container = source.eContainer();

        final EReference containingReference = (EReference) source.eContainingFeature();

        // list of all contained elements in the container
        final Object contents = container.eGet(containingReference);

        change.setAffectedEObject(container);
        change.setIsCreate(true);

        // find index of current element in the container
        int index = -1;
        if (contents instanceof EList) {
            @SuppressWarnings("unchecked")
            final EList<EObject> contentsList = ((EList<EObject>) contents);
            index = contentsList.indexOf(source);
        } else {
            throw new IllegalStateException("ContainingReference must be of type EList");
        }

        change.setAffectedFeature(containingReference);
        change.setIndex(index);
        change.setNewValue(source);

        return change;
    }

}

@@ -241,16 +241,302 @@
 Change;%0A
+import tools.vitruv.framework.change.echange.compound.CompoundFactory;%0Aimport tools.vitruv.framework.change.echange.compound.CreateAndInsertNonRoot;%0Aimport tools.vitruv.framework.change.echange.eobject.CreateEObject;%0Aimport tools.vitruv.framework.change.echange.eobject.EobjectFactory;%0A
 import t
@@ -1228,17 +1228,23 @@
 Object%3E 
-c
+insertC
 hange = 
@@ -1588,33 +1588,39 @@
 ence);%0A%0A        
-c
+insertC
 hange.setAffecte
@@ -1652,34 +1652,8 @@
     
-change.setIsCreate(true);%0A
 %0A   
@@ -2060,33 +2060,39 @@
      %7D%0A%0A        
-c
+insertC
 hange.setAffecte
@@ -2122,33 +2122,39 @@
 rence);%0A        
-c
+insertC
 hange.setIndex(i
@@ -2168,17 +2168,23 @@
         
-c
+insertC
 hange.se
@@ -2215,16 +2215,425 @@
     
-return c
+final CreateEObject%3CEObject%3E createChange = EobjectFactory.eINSTANCE.createCreateEObject();%0A        createChange.setAffectedEObject(source);%0A%0A        final CreateAndInsertNonRoot%3CEObject, EObject%3E compositeChange = CompoundFactory.eINSTANCE.createCreateAndInsertNonRoot();%0A        compositeChange.setCreateChange(createChange);%0A        compositeChange.setInsertChange(insertChange);%0A        %0A        return compositeC
 hang

Make construction simulation compatible with changed change MM