]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java
Process changes in smaller chunks
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / ModelUpdate.java
index f3fbdda5b13877a3705b55901e1057aab3bb726a..4e1cc4fed37efb8decac9616d92662fd2aad1cd7 100644 (file)
@@ -7,13 +7,17 @@ import java.util.Deque;
 import java.util.List;
 import java.util.Map.Entry;
 
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.Statement;
+import org.simantics.db.VirtualGraph;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.request.Read;
@@ -46,6 +50,7 @@ public abstract class ModelUpdate {
        private List<ChangeFilter2> userFilters = new ArrayList<ChangeFilter2>();
        
        boolean init = false;
+       private int chunkSize = -1;
        
        public void setInput(Resource oldModel, Resource newModel) throws DatabaseException {
                setInput(oldModel, newModel, null, false);
@@ -60,9 +65,16 @@ public abstract class ModelUpdate {
         * @throws DatabaseException
         */
        public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct) throws DatabaseException{
+               this.setInput(oldModel, newModel, originalModel, newDistinct, null);
+       }
+       public void setInput(Resource oldModel, Resource newModel, Resource originalModel, boolean newDistinct, IProgressMonitor monitor) throws DatabaseException{
+               
                this.oldModel = oldModel;
                this.newModel = newModel;
                this.originalModel = originalModel;
+               
+               if (monitor == null)
+                       monitor = new NullProgressMonitor();
 //             addFilters(filters);
                if (originalModel != null) {
                        // tree way comparison
@@ -71,10 +83,15 @@ public abstract class ModelUpdate {
                        GraphComparator comparator2  = result2.first;
                        if (result2.second != null)
                                showWarning(result2.second);
-                       comparator2.test(getSession());
+                       comparator2.test(getSession(), monitor);
                        changes2 = comparator2.getChanges();
+                       comparator2.dispose();
+                       if (monitor.isCanceled()) {
+                               dispose();
+                               return;
+                       }
                        changes2 = getSession().syncRequest(createFilterRead(changes2, filters));
-                       Pair<UpdateTree, UpdateList> chg2 = createChangeObjects(changes2);
+                       Pair<UpdateTree, UpdateList> chg2 = createChangeObjects(changes2, null);
                        updateTree2 = chg2.first;
                        updateList2 = chg2.second;
                        
@@ -83,10 +100,15 @@ public abstract class ModelUpdate {
                        GraphComparator comparator3  = result3.first;
                        if (result3.second != null)
                                showWarning(result3.second);
-                       comparator3.test(getSession());
+                       comparator3.test(getSession(), monitor);
                        changes3 = comparator3.getChanges();
+                       comparator2.dispose();
                        changes3 = getSession().syncRequest(createFilterRead(changes3, filters));
                }
+               if (monitor.isCanceled()) {
+                       dispose();
+                       return;
+               }
                
                Pair<GraphComparator,String> result = getChanges(oldModel,newModel);
                GraphComparator comparator  = result.first;
@@ -125,10 +147,21 @@ public abstract class ModelUpdate {
                                }
                        }
                }
-               comparator.test(getSession());
+               if (monitor.isCanceled()) {
+                       dispose();
+                       return;
+               }
+               comparator.test(getSession(), monitor);
+               monitor.setTaskName("Processing changes...");
+               monitor.subTask("");
                changes = comparator.getChanges();
+               comparator.dispose();
                changes = getSession().syncRequest(createFilterRead(changes, filters));
-               Pair<UpdateTree, UpdateList> chg = createChangeObjects(changes);
+               Pair<UpdateTree, UpdateList> chg = createChangeObjects(changes, monitor);
+               if (chg == null) {
+                       dispose();
+                       return;
+               }
         updateTree = chg.first;
         updateList = chg.second;
                if (userFilters.size() != 0) {
@@ -139,7 +172,30 @@ public abstract class ModelUpdate {
                if (originalModel != null) {
                        defaultSelections();
                }
+               init = true;
+       }
+       
+       public void setInput(Resource oldModel, Resource newModel, GraphChanges changes, IProgressMonitor monitor) throws DatabaseException{
+               if (!oldModel.equals(changes.getResource1()) ||
+                       !newModel.equals(changes.getResource2())) {
+                       throw new DatabaseException("GraphChanges does not match input models");
+               }
+               this.changes = getSession().syncRequest(createFilterRead(changes, filters));
+               Pair<UpdateTree, UpdateList> chg = createChangeObjects(changes, monitor);
+               if (chg == null) {
+                       dispose();
+                       return;
+               }
+        updateTree = chg.first;
+        updateList = chg.second;
+               if (userFilters.size() != 0) {
+                       refreshUserFilters();
+               }
                
+               
+               if (originalModel != null) {
+                       defaultSelections();
+               }
                init = true;
        }
        
@@ -181,6 +237,14 @@ public abstract class ModelUpdate {
                return userFilters;
        }
        
+       public int getChunkSize() {
+               return chunkSize;
+       }
+       
+       public void setChunkSize(int chunkSize) {
+               this.chunkSize = chunkSize;
+       }
+       
        public void refreshUserFilters() throws DatabaseException{
                // use user filters to set visible flags of changes.
                // First, set all changes visible.
@@ -253,15 +317,30 @@ public abstract class ModelUpdate {
        
        protected abstract Pair<GraphComparator,String> getChanges(Resource r1, Resource r2)  throws DatabaseException;
        
-       protected Pair<UpdateTree, UpdateList> createChangeObjects(GraphChanges changes) throws DatabaseException{
-           UpdateTree updateTree = getUpdateTree(changes);
-        UpdateList updateList = getUpdateList(changes);
+       protected Pair<UpdateTree, UpdateList> createChangeObjects(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException{
+               if (monitor != null) {
+                       if (monitor.isCanceled())
+                               return null;
+                       monitor.subTask("Processing structural changes");
+               }
+               UpdateTree updateTree = getUpdateTree(changes, monitor);
+               if (monitor != null) {
+                       if (monitor.isCanceled())
+                               return null;
+                       monitor.subTask("Processing property changes");
+               }
+        UpdateList updateList = getUpdateList(changes, monitor);
+        if (monitor != null) {
+               if (monitor.isCanceled())
+                               return null;
+               monitor.subTask("Postprocessing changes");
+        }
         postProcess(updateTree, updateList);
         return new Pair<UpdateTree, UpdateList>(updateTree, updateList);
        }
        
-       protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException;
-       protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException {
+       protected abstract UpdateTree getUpdateTree(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException;
+       protected UpdateList getUpdateList(GraphChanges changes, IProgressMonitor monitor) throws DatabaseException {
                return new UpdateList(changes, changes.getModifications());
        }
        
@@ -312,7 +391,7 @@ public abstract class ModelUpdate {
        
        public UpdateTree getUpdateTree3() throws DatabaseException{
                if (updateTree3 == null && changes3 != null) {
-                   Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3);
+                   Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3, null);
             updateTree3 = chg3.first;
             updateList3 = chg3.second;
                }
@@ -320,9 +399,9 @@ public abstract class ModelUpdate {
        }
        public UpdateList getUpdateList3() throws DatabaseException {
                if (updateList3 == null && changes3 != null) {
-                   Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3);
-            updateTree3 = chg3.first;
-            updateList3 = chg3.second;
+                       Pair<UpdateTree, UpdateList> chg3 = createChangeObjects(changes3, null);
+                       updateTree3 = chg3.first;
+                       updateList3 = chg3.second;
                }
                return updateList3;
        }
@@ -334,30 +413,91 @@ public abstract class ModelUpdate {
            return updateNode3;
        }
 
-       
        public void applyAll(WriteGraph graph) throws DatabaseException {
-               Layer0Utils.addCommentMetadata(graph, "Apply all model updates");
-               graph.markUndoPoint();
-               for (PropertyChange mod : updateList.getChanges()) {
-                       mod.apply(graph);
-               }
+               List<PropertyChange> list = updateList.getChanges();
+               apply(graph, list, "Apply all model updates");
                
                updateTree.getUpdateOps().applyAll(graph);
        }
        
        public void applySelected(WriteGraph graph) throws DatabaseException {
-               Layer0Utils.addCommentMetadata(graph, "Apply selected model updates");
-               graph.markUndoPoint();
+               List<PropertyChange> list = new ArrayList<PropertyChange>();
+               for (PropertyChange mod : updateList.getChanges()) {
+                       if (mod.selected())
+                               list.add(mod);
+               }
+               apply(graph, list, "Apply selected model updates");
                
                updateTree.getUpdateOps().applySelected(graph);
+       }
+       
+       public void applyAll(Session session, VirtualGraph vg) throws DatabaseException {
+               List<PropertyChange> list = updateList.getChanges();
+               apply(session, list, "Apply all model updates", vg);
                
+               updateTree.getUpdateOps().applyAll(session, vg);
+       }
+       
+       public void applySelected(Session session, VirtualGraph vg) throws DatabaseException {
+               List<PropertyChange> list = new ArrayList<PropertyChange>();
                for (PropertyChange mod : updateList.getChanges()) {
                        if (mod.selected())
-                               mod.apply(graph);
+                               list.add(mod);
                }
+               apply(session, list, "Apply selected model updates", vg);
+               
+               updateTree.getUpdateOps().applySelected(session, vg);
+               
        }
        
+       protected void apply(WriteGraph graph, List<PropertyChange> list, String message) throws DatabaseException {
+               Layer0Utils.addCommentMetadata(graph, message);
+               graph.markUndoPoint();
+               for (PropertyChange mod : list) {
+                       mod.apply(graph);
+               }
+       }
        
+       protected void apply(Session session, List<PropertyChange> list, String message, VirtualGraph vg) throws DatabaseException {
+               if (getChunkSize() > 0) {
+                       for (int s = 0; s < list.size(); ) {
+                               int e = s + getChunkSize();
+                               if (e > list.size())
+                                       e = list.size();
+                               List<PropertyChange> subList = list.subList(s, e);
+                               if (s == 0) {
+                                       session.syncRequest(new WriteRequest(vg) {
+                                               public void perform(WriteGraph graph) throws DatabaseException {
+                                                       Layer0Utils.addCommentMetadata(graph, message);
+                                                       graph.markUndoPoint();
+                                               }
+                                       });
+                               }
+                               session.syncRequest(new WriteRequest(vg) {
+                                       
+                                       @Override
+                                       public void perform(WriteGraph graph) throws DatabaseException {
+                                               for (PropertyChange mod : subList) {
+                                                       mod.apply(graph);
+                                               }
+                                       }
+                               });
+                               s = e;
+                       }
+               } else {
+                       session.syncRequest(new WriteRequest(vg) {
+                               
+                               @Override
+                               public void perform(WriteGraph graph) throws DatabaseException {
+                                       Layer0Utils.addCommentMetadata(graph, message);
+                                       graph.markUndoPoint();
+                                       for (PropertyChange mod : list) {
+                                               mod.apply(graph);
+                                       }
+                               }
+                       });
+               }
+       }
        
        
        protected Session getSession() {
@@ -600,5 +740,6 @@ public abstract class ModelUpdate {
                updateTree2 = null;
                updateTree3 = null;
                updateNode3 = null;
+               init = false;
        }
 }