]> 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 94b24619675049ff0bf1489fd4c4fcbd4935a1eb..0881a7bbd44aa5ecf6286a97cb7a96632b36893a 100644 (file)
@@ -4,6 +4,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map.Entry;
 import java.util.Stack;
 
 import org.eclipse.jface.resource.ImageDescriptor;
@@ -40,27 +41,25 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
-import org.eclipse.ui.IEditorInput;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.Session;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.exception.DoesNotContainValueException;
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
-import org.simantics.db.exception.ServiceException;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.request.Read;
 import org.simantics.interop.test.GraphChanges;
 import org.simantics.interop.test.GraphComparator;
 import org.simantics.interop.update.Activator;
+import org.simantics.interop.update.model.UpdateList;
 import org.simantics.interop.update.model.UpdateNode;
 import org.simantics.interop.update.model.UpdateNode.Status;
 import org.simantics.interop.update.model.UpdateOp;
 import org.simantics.interop.update.model.UpdateTree;
 import org.simantics.interop.utils.TableUtils;
-import org.simantics.ui.workbench.ResourceEditorPart2;
+import org.simantics.ui.SimanticsUI;
 import org.simantics.utils.datastructures.Callback;
 import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.ui.ExceptionUtils;
@@ -72,10 +71,8 @@ import org.simantics.utils.ui.ExceptionUtils;
  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
  *
  */
-public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
+public abstract class ModelUpdateEditor extends Composite{
 
-       
-       private Composite composite;
        private Composite errorComposite;
        
        private CheckboxTreeViewer changeBrowser;
@@ -83,6 +80,12 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
        
        private GraphChanges changes;
        private UpdateTree updateTree;
+       private UpdateList updateList;
+       
+       private GraphChanges changes2;
+       private GraphChanges changes3;
+       private UpdateTree updateTree2;
+       private UpdateList updateList2;
        
        private Button updateAllButton;
        private Button updateSelectedButton;
@@ -96,30 +99,26 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
        private Color containsColor;
        private Color deletedColor;
        private Color addedColor;
-       
-       private HashSet<Pair<Statement, Statement>> selected = new HashSet<Pair<Statement,Statement>>();
+
        
        private HashSet<UpdateNode> selectedStructure = new HashSet<UpdateNode>();
        
        private List<ChangeFilter> filters = new ArrayList<ChangeFilter>();
        
-       public ModelUpdateEditor() {
+       public ModelUpdateEditor(Composite parent) {
+               super(parent,SWT.NONE);
                checked = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/tick.png"));
                unchecked = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/cross.png"));
                warning = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/error.png"));
-       }
-       
-       @Override
-       public void createPartControl(Composite parent) {
+
                
                containsColor = new Color(parent.getDisplay(), new RGB(255,255,220));
                deletedColor = new Color(parent.getDisplay(), new RGB(255,220,220));
                addedColor = new Color(parent.getDisplay(), new RGB(220,255,220));
                
-               composite = new Composite(parent, SWT.NONE);
-               composite.setLayout(new GridLayout(1,false));
+               this.setLayout(new GridLayout(1,false));
                
-               errorComposite = new Composite(composite, SWT.BORDER);
+               errorComposite = new Composite(this, SWT.BORDER);
                GridData data = new GridData();
                data.grabExcessHorizontalSpace = true;
                data.grabExcessVerticalSpace = false;
@@ -130,14 +129,14 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                
                errorComposite.setVisible(false);
 
-               IEditorInput input = getEditorInput();
-               if (!(input instanceof UpdateEditorInput)) {
-                       Label label = new Label(composite, SWT.NONE);
-                       label.setText("Unknown input.");
-                       return;
-               }
-               
-               Composite fillComposite = new Composite(composite, SWT.NONE);
+//             IEditorInput input = getEditorInput();
+//             if (!(input instanceof UpdateEditorInput)) {
+//                     Label label = new Label(composite, SWT.NONE);
+//                     label.setText("Unknown input.");
+//                     return;
+//             }
+               
+               Composite fillComposite = new Composite(this, SWT.NONE);
                data = new GridData();
                data.grabExcessHorizontalSpace = true;
                data.grabExcessVerticalSpace = true;
@@ -169,7 +168,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                                                node.getOp().select(Boolean.TRUE.equals(event.getChecked()));
                                                
                                        }
-                                       updateSelection();
+                                       refreshChecked();
                                        
                                }
                        });
@@ -181,7 +180,8 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                                                
                                                @Override
                                                public void run() {
-                                                       updateSelection();
+                                                       // TreeViewer uses lazy load, checked states must be updated when the tree is expanded. 
+                                                       refreshChecked();
                                                }
                                        });
                                        
@@ -218,26 +218,25 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                        oldValue.setLabelProvider(getLabelProvider(4));
                        newValue.setLabelProvider(getLabelProvider(5));
                        
-                       selection.setLabelProvider(new SelectionLabelProvider(selected));
+                       selection.setLabelProvider(new SelectionLabelProvider());
                        selection.getColumn().addSelectionListener(new SelectionAdapter() {
                                @Override
                                public void widgetSelected(SelectionEvent e) {
-                                       if (changes.getModifications().size() > 0) {
-                                               if (selected.contains(changes.getModifications().get(0))) {
-                                                       for (Pair<Statement, Statement> nr : changes.getModifications())
-                                                               selected.remove(nr);
+                                       if (updateList.getChanges().size() > 0) {
+                                               if (updateList.getSelected().size() > 0) {
+                                                       updateList.clearSelected();
                                                } else {
-                                                       for (Pair<Statement, Statement> nr : changes.getModifications())
-                                                               selected.add(nr);
+                                                       for (Pair<Statement, Statement> nr : updateList.getChanges())
+                                                               updateList.addSelected(nr);
                                                }
                                                changeViewer.refresh();
                                        }
                                }
                        });
-                       selection.setEditingSupport(new SelectionEditingSupport(changeViewer, selected));
+                       selection.setEditingSupport(new SelectionEditingSupport(changeViewer));
                
                }
-               Composite buttonComposite = new Composite(composite, SWT.NONE);
+               Composite buttonComposite = new Composite(this, SWT.NONE);
                
                data = new GridData();
                data.grabExcessHorizontalSpace = true;
@@ -262,7 +261,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                updateAllButton.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
-                               updateAll();
+                               applyAll();
                        }
                });
                updateSelectedButton = new Button(buttonComposite, SWT.PUSH);
@@ -270,12 +269,13 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                updateSelectedButton.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
-                               updateSelected();
+                               applySelected();
                        }
                });
-
-               load();
-               
+       }
+       
+       protected Session getSession() {
+               return SimanticsUI.getSession();
        }
        
        protected String getColumntTitle(int i) {
@@ -299,8 +299,11 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
        }
        
        protected abstract ColumnLabelProvider getLabelProvider(int i);
-       protected abstract Pair<GraphComparator,Boolean> getChanges(Resource r1, Resource r2)  throws DatabaseException;
+       protected abstract Pair<GraphComparator,String> getChanges(Resource r1, Resource r2)  throws DatabaseException;
        protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException;
+       protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException {
+               return new UpdateList(changes.getModifications());
+       }
        
        protected void addFilters(List<ChangeFilter> filters) {
                
@@ -310,6 +313,22 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                return changes;
        }
        
+       public UpdateTree getUpdateTree() {
+               return updateTree;
+       }
+       
+       public UpdateList getUpdateList() {
+               return updateList;
+       }
+       
+       public CheckboxTreeViewer getChangeBrowser() {
+               return changeBrowser;
+       }
+       
+       public TableViewer getChangeViewer() {
+               return changeViewer;
+       }
+       
        private void showWarning(String text) {
                errorComposite.setVisible(true);
                
@@ -318,11 +337,22 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                label = new Label(errorComposite, SWT.NONE);
                label.setText(text);
                //this.setStatusMessage("Update contains structural changes (new or deleted symbols), please create a new model.");
-               composite.layout(true);
+               this.layout(true);
+       }
+       
+       
+       private List<ICheckStateListener> checkStateListeners = new ArrayList<>();
+       
+       
+       public void addCheckStateListener(ICheckStateListener listener) {
+               checkStateListeners.add(listener);
+       }
        
+       public void removeCheckStateListener(ICheckStateListener listener) {
+               checkStateListeners.remove(listener);
        }
        
-       private void updateSelection() {
+       public void refreshChecked() {
                Stack<UpdateNode> nodeStack = new Stack<UpdateNode>();
                nodeStack.push((UpdateNode)updateTree.getRootNode());
                while (!nodeStack.isEmpty()) {
@@ -360,46 +390,112 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                }
                
                changeBrowser.refresh();
+               for (ICheckStateListener l : checkStateListeners) {
+                       l.checkStateChanged(new CheckStateChangedEvent(changeBrowser, null, false));
+               }
+               changeViewer.refresh();
        }
        
        
        
-       private void load() {
+       public void load(UpdateEditorInput uei) {
                
                addFilters(filters);
 
-               UpdateEditorInput uei = (UpdateEditorInput)getEditorInput();
-               Resource r1 = uei.getR1();
-               Resource r2 = uei.getR2();
-               
+               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,Boolean> result = getChanges(r1,r2);
+                       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);
+                               comparator2.test(getSession());
+                               changes2 = comparator2.getChanges();
+                               changes2 = getSession().syncRequest(new FilterChangesRead(changes2));
+                               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)
-                               showWarning("Structural symbols have been changed. Model update is not able to update these, please create a new model.");
+                       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(composite, SWT.MULTI);
+                       Text text = new Text(this, SWT.MULTI);
                        text.setText(e.getMessage());
                        e.printStackTrace();
                        return;
                }
        
-
-
-               changeViewer.setInput(changes.getModifications());
-               changeBrowser.setInput(updateTree);
-               updateSelection();
+               setInputs();
+               
+               refreshChecked();
        }
        
+       protected void setInputs() {
+               changeViewer.setInput(updateList.getChanges());
+               changeBrowser.setInput(updateTree);
+       }
        
-       
-       private void updateAll() {
+       private void applyAll() {
                updateAllButton.setEnabled(false);
                updateSelectedButton.setEnabled(false);
                getSession().asyncRequest(new WriteRequest(){
@@ -407,11 +503,10 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                        public void perform(WriteGraph graph) throws DatabaseException {
                                Layer0Utils.addCommentMetadata(graph, "Apply all model updates");
                                graph.markUndoPoint();
-                               for (Pair<Statement, Statement> mod : changes.getModifications()) {
+                               for (Pair<Statement, Statement> mod : updateList.getChanges()) {
                                        applyLiteralChange(graph, mod);
                                }
-                               selected.clear();
-                               changes.getModifications().clear();
+                               updateList.clear();
                                
                                updateTree.getUpdateOps().applyAll(graph);
                                
@@ -422,7 +517,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                                                
                                                updateAllButton.setEnabled(true);
                                                updateSelectedButton.setEnabled(true);
-                                               updateSelection();
+                                               refreshChecked();
                                                changeViewer.refresh();
                                        }
                                });
@@ -438,10 +533,13 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                });
        }
        
-       private void applyLiteralChange(WriteGraph graph, Pair<Statement, Statement> mod) throws DoesNotContainValueException, ServiceException, ManyObjectsForFunctionalRelationException {
-               
+       protected void applyLiteralChange(WriteGraph graph, Pair<Statement, Statement> mod) throws DatabaseException {
+               if (mod.second == null) {
+                       graph.deny(mod.first);
+                       return;
+               } 
                Resource s = changes.getComparable().getLeft(mod.second.getSubject());
-               Resource pred = mod.first.getPredicate();
+               Resource pred = mod.second.getPredicate();
                if (graph.hasValue(mod.second.getObject())) {
                        Object value = graph.getValue(mod.second.getObject());
                        graph.claimLiteral(s, pred, value);
@@ -452,7 +550,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                
        }
        
-       private void updateSelected() {
+       private void applySelected() {
                updateAllButton.setEnabled(false);
                updateSelectedButton.setEnabled(false);
                getSession().asyncRequest(new WriteRequest(){
@@ -460,11 +558,10 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                        public void perform(WriteGraph graph) throws DatabaseException {
                                Layer0Utils.addCommentMetadata(graph, "Apply selected model updates");
                                graph.markUndoPoint();
-                               for (Pair<Statement, Statement> mod : selected) {
-                                       changes.getModifications().remove(mod);
+                               for (Pair<Statement, Statement> mod : updateList.getSelected()) {
+                                       updateList.removeChange(mod);
                                        applyLiteralChange(graph, mod);
                                }
-                               selected.clear();
                                
                                updateTree.getUpdateOps().applySelected(graph);
                                
@@ -475,18 +572,54 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                                                changeViewer.refresh();
                                                updateAllButton.setEnabled(true);
                                                updateSelectedButton.setEnabled(true);
-                                               updateSelection();
+                                               refreshChecked();
                                        }
                                });
                        }
                });
        }
-
-       @Override
-       public void setFocus() {
-               composite.setFocus();
-
+       
+       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);
+                               }
+                       }
+               }
+               
        }
+
        
        /**
         * Filters changes:
@@ -522,9 +655,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
        public interface ChangeFilter {
                public boolean accept(ReadGraph g, Pair<Statement, Statement> change) throws DatabaseException;
        }
-       
-       
-       
+
        
        /**
         * 
@@ -647,9 +778,8 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
        
        private class SelectionLabelProvider extends ColumnLabelProvider {
                
-               Collection<?>  selected;
-               public SelectionLabelProvider(Collection<?>  selected) {
-                       this.selected = selected;
+               public SelectionLabelProvider() {
+                       
                }
                @Override
                public String getText(Object element) {
@@ -658,7 +788,9 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                
                @Override
                public Image getImage(Object element) {
-                       if (selected.contains(element))
+                       if (updateList == null)
+                               return null;
+                       if (updateList.isSelected((Pair<Statement, Statement>) element))
                                return checked;
                        else
                                return unchecked;
@@ -670,16 +802,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                @Override
                public String getText(Object element) {
                        final UpdateNode node = (UpdateNode)element;
-                       try  {
-                               return getSession().syncRequest(new Read<String>() {
-                                       @Override
-                                       public String perform(ReadGraph graph) throws DatabaseException {
-                                               return node.getLabel(graph);
-                                       }
-                               });
-                       } catch (Exception e) {
-                               return e.getMessage();
-                       }
+                       return node.getLabel();
                }
                
                @Override
@@ -734,14 +857,10 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
        
        private class SelectionEditingSupport extends EditingSupport {
                
-               @SuppressWarnings("rawtypes")
-               Collection selected;
-
                
                @SuppressWarnings("rawtypes")
-               public SelectionEditingSupport(ColumnViewer viewer, Collection selected) {
+               public SelectionEditingSupport(ColumnViewer viewer) {
                        super(viewer);
-                       this.selected = selected;
                        
                }
 
@@ -757,16 +876,20 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 {
                
                @Override
                protected Object getValue(Object element) {
-                       return selected.contains(element);
+                       if (updateList == null)
+                               return false;
+                       return updateList.isSelected((Pair<Statement, Statement>) element);
                }
                
                @SuppressWarnings("unchecked")
                @Override
                protected void setValue(Object element, Object value) {
+                       if (updateList == null)
+                               return;
                        if (Boolean.TRUE.equals(value))
-                               selected.add(element);
+                               updateList.addSelected((Pair<Statement, Statement>) element);
                        else
-                               selected.remove(element);
+                               updateList.removeSelected((Pair<Statement, Statement>) element);
                        
                        getViewer().refresh(element);
                }