]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java
Handle asserted property statements
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / ModelUpdate.java
index 2a263f117ff5f26eb398f468f7d41df270a622f2..5f400df4c653285946551ea0f2addf8c4c81f497 100644 (file)
@@ -1,7 +1,10 @@
 package org.simantics.interop.update.model;
 
+import java.util.ArrayDeque;
 import java.util.ArrayList;
-import java.util.HashSet;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Deque;
 import java.util.List;
 import java.util.Map.Entry;
 
@@ -15,24 +18,31 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.request.Read;
 import org.simantics.interop.test.GraphChanges;
+import org.simantics.interop.test.GraphChanges.Modification;
 import org.simantics.interop.test.GraphComparator;
+import org.simantics.utils.datastructures.BijectionMap;
 import org.simantics.utils.datastructures.Pair;
 
 public abstract class ModelUpdate {
        
-       private GraphChanges changes;
+       private Resource oldModel;      // old model that is going to be updated (User modified model)
+       private Resource newModel;      // new model that contains the updates   (New design model)
+       private Resource originalModel; // original model (optional) that is used for detecting and retaining user made changes (Old design model)
+       
+       private GraphChanges changes;   // changes between old /new
        private UpdateTree updateTree;
        private UpdateList updateList;
        
-       private GraphChanges changes2;
+       private GraphChanges changes2;  // changes between original / old
        private UpdateTree updateTree2;
        private UpdateList updateList2;
        
-       private GraphChanges changes3;
+       private GraphChanges changes3;  // changes between original / new
        private UpdateTree updateTree3;
        private UpdateList updateList3;
        
        private List<ChangeFilter> filters = new ArrayList<ChangeFilter>();
+       private List<ChangeFilter> userFilters = new ArrayList<ChangeFilter>();
        
        boolean init = false;
        
@@ -40,8 +50,19 @@ public abstract class ModelUpdate {
                setInput(oldModel, newModel, null, false);
        }
        
+       /**
+        * Initialises the ModelUpdate with given input
+        * @param oldModel the model that is going to be updated (User modified model)
+        * @param newModel the model containing updates (New design model)
+        * @param originalModel the model that is used for detecting and retaining user made changes (Old design model). Parameter can be null.
+        * @param newDistinct when originalModel is given, additions to the old and the new model (when compared to the original model) are forced to be distinct. 
+        * @throws DatabaseException
+        */
        public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct) throws DatabaseException{
-               addFilters(filters);
+               this.oldModel = oldModel;
+               this.newModel = newModel;
+               this.originalModel = originalModel;
+//             addFilters(filters);
                if (originalModel != null) {
                        // tree way comparison
                        // compare the original and the old model
@@ -51,7 +72,7 @@ public abstract class ModelUpdate {
                                showWarning(result2.second);
                        comparator2.test(getSession());
                        changes2 = comparator2.getChanges();
-                       changes2 = getSession().syncRequest(new FilterChangesRead(changes2));
+                       changes2 = getSession().syncRequest(createFilterRead(changes2, filters));
                        updateTree2 = getUpdateTree(changes2);
                        updateList2 = getUpdateList(changes2);
                        
@@ -62,7 +83,7 @@ public abstract class ModelUpdate {
                                showWarning(result3.second);
                        comparator3.test(getSession());
                        changes3 = comparator3.getChanges();
-                       changes3 = getSession().syncRequest(new FilterChangesRead(changes3));
+                       changes3 = getSession().syncRequest(createFilterRead(changes3, filters));
                }
                
                Pair<GraphComparator,String> result = getChanges(oldModel,newModel);
@@ -104,28 +125,155 @@ public abstract class ModelUpdate {
                }
                comparator.test(getSession());
                changes = comparator.getChanges();
-               changes = getSession().syncRequest(new FilterChangesRead(changes));
+               changes = getSession().syncRequest(createFilterRead(changes, filters));
                updateTree = getUpdateTree(changes);
                updateList = getUpdateList(changes);
+               if (userFilters.size() != 0) {
+                       refreshUserFilters();
+               }
                
                
                if (originalModel != null) {
-                       createDefaultSelections();
+                       defaultSelections();
                }
                
                init = true;
        }
        
+       public void addFilter(ChangeFilter filter) {
+               if (init)
+                       throw new IllegalStateException("ModelUpdate has been initialized, adjusting filters is no longer possible.");
+               filters.add(filter);
+               
+       }
        
+       public List<ChangeFilter> getFilters() {
+               return Collections.unmodifiableList(filters);
+       }
+       
+       /**
+        * Adds an user filter. Use refreshUserFilters() to apply the changes.
+        * @param filter
+        */
+       public void addUserFilter(ChangeFilter filter) {
+               userFilters.add(filter);
+       }
+       
+       /**
+        * Removes an user filter. Use refreshUserFilters() to apply the changes.
+        * @param filter
+        */
+       public void removeUserFilter(ChangeFilter filter) {
+               userFilters.remove(filter);
+       }
+       
+       /**
+        * Clears user filters.  Use refreshUserFilters() to apply the changes.
+        */
+       public void clearUserFilters() {
+               userFilters.clear();
+       }
+       
+       public List<ChangeFilter> getUserFilters() {
+               return userFilters;
+       }
+       
+       public void refreshUserFilters() throws DatabaseException{
+               // use user filters to set visible flags of changes.
+               // First, set all changes visible.
+               Deque<UpdateNode> stack = new ArrayDeque<>();
+               stack.push(updateTree.getRootNode());
+               while (!stack.isEmpty()) {
+                       UpdateNode n = stack.pop();
+                       n.setVisible(true);
+                       stack.addAll(n.getChildren());
+               }
+               for (PropertyChange pc : updateList.getChanges()) {
+                       pc.setVisible(true);
+               }
+               if (userFilters.size() > 0) {
+                       // Create filtered changes
+                       List<ChangeFilter> combined = new ArrayList<>(filters);
+                       combined.addAll(userFilters);
+                       GraphChanges filteredChanges = getSession().syncRequest(createFilterRead(changes, combined));
+                       UpdateTree updateTreeF = getUpdateTree(filteredChanges);
+                       UpdateList updateListF = getUpdateList(filteredChanges);
+                       // hide changes that are not contained within the filtered changes.
+                       applyVisibleFlags(updateTree.getRootNode(), updateTreeF.getRootNode());
+                       applyVisibleFlags(updateList.getChanges(), updateListF.getChanges());
+               }
+       }
+       
+       private void applyVisibleFlags(UpdateNode l, UpdateNode r) {
+               BijectionMap<UpdateNode, UpdateNode> comparable = new BijectionMap<>();
+               for (UpdateNode lc : l.getChildren()) {
+                       for (UpdateNode rc : r.getChildren()) {
+                               if (comparable.containsRight(rc))
+                                       continue;
+                               if (lc.getResource() != null) {
+                                       if (lc.getResource().equals(rc.getResource())) {
+                               
+                                               comparable.map(lc, rc);
+                                               break;
+                                       }
+                               } else if (rc.getResource() == null){
+                                       UpdateOp lop = lc.getOp();
+                                       UpdateOp rop = rc.getOp();
+                                       if (lop.getStatement() != null && lop.getStatement().equals(rop.getStatement())) {
+                                               comparable.map(lc, rc);
+                                               break;
+                                       }
+                               }
+                       }
+               }
+               for (UpdateNode lc : l.getChildren()) {
+                       if (!comparable.containsLeft(lc))
+                               lc.setVisible(false);
+               }
+               for (Entry<UpdateNode, UpdateNode> entry : comparable.getEntries()) {
+                       applyVisibleFlags(entry.getKey(), entry.getValue());
+               }       
+       }
+       
+       private void applyVisibleFlags(Collection<PropertyChange> l, Collection<PropertyChange> r) {
+               BijectionMap<PropertyChange, PropertyChange> comparable = new BijectionMap<>();
+               for (PropertyChange lc : l) {
+                       for (PropertyChange rc : r) {
+                               if (comparable.containsRight(rc))
+                                       continue;
+                               if (lc.getFirst() != null && lc.getFirst().equals(rc.getFirst())) {
+                                       comparable.map(lc, rc);
+                                       break;
+                               }
+                               if (lc.getSecond() != null && lc.getSecond().equals(rc.getSecond())) {
+                                       comparable.map(lc, rc);
+                                       break;
+                               }
+                       }
+               }
+               for (PropertyChange lc : l) {
+                       if (!comparable.containsLeft(lc))
+                               lc.setVisible(false);
+               }
+               
+       }
        
        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());
+               return new UpdateList(changes, changes.getModifications());
        }
        
-       protected void addFilters(List<ChangeFilter> filters) {
-               
+       public Resource getOldModel() {
+               return oldModel;
+       }
+       
+       public Resource getNewModel() {
+               return newModel;
+       }
+       
+       public Resource getOriginalModel() {
+               return originalModel;
        }
        
        public boolean isInit() {
@@ -171,10 +319,9 @@ public abstract class ModelUpdate {
        public void applyAll(WriteGraph graph) throws DatabaseException {
                Layer0Utils.addCommentMetadata(graph, "Apply all model updates");
                graph.markUndoPoint();
-               for (Pair<Statement, Statement> mod : updateList.getChanges()) {
-                       applyLiteralChange(graph, mod);
+               for (PropertyChange mod : updateList.getChanges()) {
+                       mod.apply(graph);
                }
-               updateList.clear();
                
                updateTree.getUpdateOps().applyAll(graph);
        }
@@ -182,82 +329,76 @@ public abstract class ModelUpdate {
        public void applySelected(WriteGraph graph) throws DatabaseException {
                Layer0Utils.addCommentMetadata(graph, "Apply selected model updates");
                graph.markUndoPoint();
-               HashSet<Pair<Statement, Statement>> changes = new HashSet<>(updateList.getSelected());
-               for (Pair<Statement, Statement> mod : changes) {
-                       updateList.removeChange(mod);
-                       applyLiteralChange(graph, mod);
+               for (PropertyChange mod : updateList.getChanges()) {
+                       if (mod.selected())
+                               mod.apply(graph);
                }
                
                updateTree.getUpdateOps().applySelected(graph);
        }
        
-       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.second.getPredicate();
-               if (graph.hasValue(mod.second.getObject())) {
-                       Object value = graph.getValue(mod.second.getObject());
-                       graph.claimLiteral(s, pred, value);
-               } else {
-                       graph.deny(s,pred);
-               }
-       }
+       
        
        
        protected Session getSession() {
                return Simantics.getSession();
        }
        
+       public Read<GraphChanges> createFilterRead(GraphChanges changes, List<ChangeFilter> filters) {
+               return new FilterChangesRead(changes, filters);
+       }
+       
        
        
-       private class FilterChangesRead implements Read<GraphChanges> {
+       public static class FilterChangesRead implements Read<GraphChanges> {
                private GraphChanges changes;
-               public FilterChangesRead(GraphChanges changes) {
+               private List<ChangeFilter> filters;
+               
+               public FilterChangesRead(GraphChanges changes, List<ChangeFilter> filters) {
                        this.changes = changes;
+                       this.filters = filters;
                }
                
                @Override
                public GraphChanges perform(ReadGraph graph) throws DatabaseException {
                        return filterChanges(graph, changes);
                }
-       }
-       
-       /**
-        * Filters changes:
-        * 1. Changes that are not essential for model update (changes that can be found when the models are axcatly the same)
-        * 2. Runs custom filters for value changes. 
-        * 
-        * @param g
-        * @param changes
-        * @return
-        * @throws DatabaseException
-        */
-       protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException 
-    {
                
-               List<Pair<Statement,Statement>> modifications = new ArrayList<Pair<Statement,Statement>>();
-               
-       for (Pair<Statement, Statement> mod : changes.getModifications()) {
-               
-               boolean accept = true;
-               for (ChangeFilter filter : filters) {
-                       if (!filter.accept(g, mod)) {
-                               accept = false;
-                               break;
-                       }       
-               }
-               if (accept)
-                       modifications.add(mod);
-       }
-       GraphChanges newChanges =  new GraphChanges(changes.getResource1(),changes.getResource2(),changes.getDeletions(), changes.getAdditions(), modifications, changes.getComparable());
-       return newChanges;
-    }
+               /**
+                * Filters changes:
+                * 1. Changes that are not essential for model update (changes that can be found when the models are exactly the same)
+                * 2. Runs custom filters for value changes. 
+                * 
+                * @param g
+                * @param changes
+                * @return
+                * @throws DatabaseException
+                */
+               protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException 
+           {
+                       
+                       List<Modification> modifications = new ArrayList<Modification>();
+                       
+               for (Modification mod : changes.getModifications()) {
+                       
+                       boolean accept = true;
+                       for (ChangeFilter filter : filters) {
+                               if (!filter.accept(g, mod)) {
+                                       accept = false;
+                                       break;
+                               }       
+                       }
+                       if (accept)
+                               modifications.add(mod);
+               }
+               GraphChanges newChanges =  new GraphChanges(changes.getResource1(),changes.getResource2(),changes.getDeletions(), changes.getAdditions(), modifications, changes.getComparable());
+               return newChanges;
+           }
+       }
+
        
        public interface ChangeFilter {
-               public boolean accept(ReadGraph g, Pair<Statement, Statement> change) throws DatabaseException;
+               public boolean accept(ReadGraph g, Modification change) throws DatabaseException;
        }
 
        
@@ -281,12 +422,12 @@ public abstract class ModelUpdate {
                }
                
                @Override
-               public boolean accept(ReadGraph g, Pair<Statement, Statement> change) throws DatabaseException {
+               public boolean accept(ReadGraph g, Modification change) throws DatabaseException {
                        //filter floating point values that have less than 1% difference.
-                       if (!g.hasValue(change.first.getObject()) || !g.hasValue(change.second.getObject()))
+                       if (!g.hasValue(change.getLeftStm().getObject()) || !g.hasValue(change.getRightStm().getObject()))
                                return true;
-               Object v1 = g.getValue(change.first.getObject());
-               Object v2 = g.getValue(change.second.getObject());
+               Object v1 = g.getValue(change.getLeftStm().getObject());
+               Object v2 = g.getValue(change.getRightStm().getObject());
                
                if (v1 instanceof Double && v2 instanceof Double) {
                        double d1 = (Double)v1;
@@ -304,15 +445,18 @@ public abstract class ModelUpdate {
                }
        }
        
-       protected void createDefaultSelections() {
+       public void defaultSelections() {
+               if (changes3 == null) {
+                       return;
+               }
                // 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);
+               for (PropertyChange pair : updateList.getChanges()) {
+                       pair.select(true);
                }
                
                // preserve user-made changes (by removing selections)
@@ -328,17 +472,17 @@ public abstract class ModelUpdate {
                        }
                }
                
-               for (Pair<Statement, Statement> pair : updateList.getChanges()) {
-                       if (pair.first != null) {
+               for (PropertyChange pair : updateList.getChanges()) {
+                       if (pair.getFirst() != null) {
                                boolean found = false;
-                               for (Pair<Statement, Statement> pair2 : updateList2.getChanges()) {
-                                       if (pair.first.equals(pair2.first)) {
+                               for (PropertyChange pair2 : updateList2.getChanges()) {
+                                       if (pair.getFirst() != null && pair.getFirst().equals(pair2.getSecond())) {
                                                found = true;
                                                break;
                                        }
                                }
                                if (found) {
-                                       updateList.removeSelected(pair);
+                                       pair.select(false);
                                }
                        }
                }