]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java
Three-way comparison: utilise old changes in the update step
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / editor / ModelUpdateEditor.java
index 860057d240f6d9763c6659f62fed9b9f876a05e8..0881a7bbd44aa5ecf6286a97cb7a96632b36893a 100644 (file)
@@ -4,8 +4,8 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Stack;
 import java.util.Map.Entry;
+import java.util.Stack;
 
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.JFaceResources;
@@ -83,6 +83,7 @@ public abstract class ModelUpdateEditor extends Composite{
        private UpdateList updateList;
        
        private GraphChanges changes2;
+       private GraphChanges changes3;
        private UpdateTree updateTree2;
        private UpdateList updateList2;
        
@@ -401,23 +402,16 @@ public abstract class ModelUpdateEditor extends Composite{
                
                addFilters(filters);
 
-               Resource r1 = uei.getR1();
-               Resource r2 = uei.getR2();
-               Resource r3 = uei.getR3();
+               Resource oldModel = uei.getR1(); // old model that is being updated, contains user made changes
+               Resource newModel = uei.getR2(); // new model, 
+               Resource originalModel = uei.getR3(); // original old model without user made changes 
 
                try {
                        
-                       Pair<GraphComparator,String> result = getChanges(r1,r2);
-                       GraphComparator comparator  = result.first;
-                       if (result.second != null)
-                               showWarning(result.second);
-                       comparator.test(getSession());
-                       changes = comparator.getChanges();
-                       changes = getSession().syncRequest(new FilterChangesRead(changes));
-                       updateTree = getUpdateTree(changes);
-                       updateList = getUpdateList(changes);
-                       if (r3 != null) {
-                               Pair<GraphComparator,String> result2 = getChanges(r1,r3);
+                       if (originalModel != null) {
+                               // tree way comparison
+                               // compare the original and the old model
+                               Pair<GraphComparator,String> result2 = getChanges(originalModel, oldModel);
                                GraphComparator comparator2  = result2.first;
                                if (result2.second != null)
                                        showWarning(result2.second);
@@ -427,8 +421,63 @@ public abstract class ModelUpdateEditor extends Composite{
                                updateTree2 = getUpdateTree(changes2);
                                updateList2 = getUpdateList(changes2);
                                
+                               // compare the original and the new model
+                               Pair<GraphComparator,String> result3 = getChanges(originalModel,newModel);
+                               GraphComparator comparator3  = result3.first;
+                               if (result3.second != null)
+                                       showWarning(result3.second);
+                               comparator3.test(getSession());
+                               changes3 = comparator3.getChanges();
+                               changes3 = getSession().syncRequest(new FilterChangesRead(changes3));
+                       }
+                       
+                       Pair<GraphComparator,String> result = getChanges(oldModel,newModel);
+                       GraphComparator comparator  = result.first;
+                       if (result.second != null)
+                               showWarning(result.second);
+                       if (originalModel != null) {
+                               // three-way comparison: use change information to configure
+                               // the comparison between the old and the new model.
+                               
+                               // 1. map comparable resources
+                               for (Entry<Resource, Resource> origToOld : changes2.getComparable().getEntries()) {
+                                       Resource oldR = origToOld.getValue();
+                                       Resource newR = changes3.getComparable().getRight(origToOld.getKey());
+                                       if (newR != null) {
+                                               comparator.addComparableResources(oldR, newR);
+                                       }
+                               }
+                               // 2. mark removed resources as distinct, so that comparison does not pair them
+                               for (Statement s : changes2.getDeletions()) {
+                                       if (changes3.getComparable().containsLeft(s.getObject()))
+                                               comparator.addNonMatchedRight(changes3.getComparable().getRight(s.getObject()));
+                               }
+                               
+                               for (Statement s : changes3.getDeletions()) {
+                                       if (changes2.getComparable().containsLeft(s.getObject()))
+                                               comparator.addNonMatchedLeft(changes2.getComparable().getRight(s.getObject()));
+                               }
+                               if (uei.isNewDistinct()) {
+                                       // 3. mark added resources as distinct, so that comparison does not pair them
+                                       for (Statement s : changes2.getAdditions()) {
+                                               comparator.addNonMatchedLeft(s.getObject());
+                                       }
+                                       
+                                       for (Statement s : changes3.getAdditions()) {
+                                               comparator.addNonMatchedRight(s.getObject());
+                                       }
+                               }
+                       }
+                       comparator.test(getSession());
+                       changes = comparator.getChanges();
+                       changes = getSession().syncRequest(new FilterChangesRead(changes));
+                       updateTree = getUpdateTree(changes);
+                       updateList = getUpdateList(changes);
+                       
+                       if (originalModel != null) {
                                createDefaultSelections();
                        }
+                       
                } catch (DatabaseException e) {
                        Text text = new Text(this, SWT.MULTI);
                        text.setText(e.getMessage());
@@ -529,6 +578,47 @@ public abstract class ModelUpdateEditor extends Composite{
                        }
                });
        }
+       
+       protected void createDefaultSelections() {
+               // select all changes
+               for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
+                       op.getValue().select(true);
+               }
+               
+               
+               for (Pair<Statement, Statement> pair : updateList.getChanges()) {
+                       updateList.addSelected(pair);
+               }
+               
+               // preserve user-made changes (by removing selections)
+               for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
+                       UpdateOp op2 = updateTree2.getUpdateOps().getUpdateOp(op.getKey());
+                       if (op2 == null) {
+                               if (changes3.getComparable().containsRight(op.getKey())){
+                                       op2 = updateTree2.getUpdateOps().getUpdateOp(changes3.getComparable().getLeft(op.getKey()));
+                               }
+                       }
+                       if (op2 != null && op.getValue().getClass() == op2.getClass()) {
+                               op.getValue().select(false);
+                       }
+               }
+               
+               for (Pair<Statement, Statement> pair : updateList.getChanges()) {
+                       if (pair.first != null) {
+                               boolean found = false;
+                               for (Pair<Statement, Statement> pair2 : updateList2.getChanges()) {
+                                       if (pair.first.equals(pair2.first)) {
+                                               found = true;
+                                               break;
+                                       }
+                               }
+                               if (found) {
+                                       updateList.removeSelected(pair);
+                               }
+                       }
+               }
+               
+       }
 
        
        /**
@@ -565,42 +655,7 @@ public abstract class ModelUpdateEditor extends Composite{
        public interface ChangeFilter {
                public boolean accept(ReadGraph g, Pair<Statement, Statement> change) throws DatabaseException;
        }
-       
-       
-       protected void createDefaultSelections() {
-               for (Entry<Resource, UpdateOp> op : updateTree.getUpdateOps().getResourceMap().entrySet()) {
-                       UpdateOp op2 = updateTree2.getUpdateOps().getUpdateOp(op.getKey());
-                       if (op2 == null) {
-                               op.getValue().select(true);
-                       }
-               }
-               
-               for (Entry<Statement, UpdateOp> op : updateTree.getUpdateOps().getStatementMap().entrySet()) {
-                       UpdateOp op2 = updateTree2.getUpdateOps().getUpdateOp(op.getKey());
-                       if (op2 == null) {
-                               op.getValue().select(true);
-                       }
-               }
-               
-               for (Pair<Statement, Statement> pair : updateList.getChanges()) {
-                       if (pair.first != null) {
-                               boolean found = false;
-                               for (Pair<Statement, Statement> pair2 : updateList2.getChanges()) {
-                                       if (pair.first.equals(pair2.first)) {
-                                               found = true;
-                                               break;
-                                       }
-                               }
-                               if (!found) {
-                                       updateList.addSelected(pair);
-                               }
-                       } else {
-                               updateList.addSelected(pair);
-                       }
-               }
-       }
-       
-       
+
        
        /**
         *