X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.vtk%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fvtk%2Fcommon%2FAbstractVTKNodeMap.java;h=33a3154e798f96dca9a7dc993cd869cde3dac10e;hb=d44977b8d6c8784d6e16374c25079b41e6026e48;hp=a42c367252fc0744a754cc22fbd9f9045f283275;hpb=da945733caf267fed4cf28206cc99f4f57fbd1e9;p=simantics%2F3d.git diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java index a42c3672..33a3154e 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java @@ -30,10 +30,11 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Session; import org.simantics.db.UndoContext; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.db.procedure.SyncProcedure; import org.simantics.db.service.UndoRedoSupport; import org.simantics.g3d.ontology.G3D; import org.simantics.g3d.scenegraph.RenderListener; @@ -54,8 +55,6 @@ import vtk.vtkProp; public abstract class AbstractVTKNodeMap implements VTKNodeMap, IMappingListener, RenderListener, NodeListener, UndoRedoSupport.ChangeListener{ - private static final boolean DEBUG = true; - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractVTKNodeMap.class); protected Session session; @@ -88,7 +87,7 @@ public abstract class AbstractVTKNodeMap implements VT undoOpCount = undoContext.getAll().size(); redoOpCount = undoContext.getRedoList().size(); } catch(DatabaseException e) { - e.printStackTrace(); + LOGGER.error("Error reading from undo context", e); } } @@ -179,7 +178,8 @@ public abstract class AbstractVTKNodeMap implements VT UndoContext undoContext = undoRedoSupport.getUndoContext(session); int ucount = undoContext.getAll().size(); int rcount = undoContext.getRedoList().size(); - if (DEBUG) System.out.println("Previous U:" + undoOpCount +" R:" + redoOpCount +" Current U:"+ucount+" R:"+rcount); + + if (LOGGER.isTraceEnabled()) LOGGER.trace("Previous U:" + undoOpCount +" R:" + redoOpCount +" Current U:"+ucount+" R:"+rcount); if (ucount < undoOpCount) { runUndo = true; } else { @@ -192,7 +192,7 @@ public abstract class AbstractVTKNodeMap implements VT undoOpCount = ucount; redoOpCount = rcount; - if (DEBUG) System.out.println("Undo " + runUndo + " Redo " + runRedo); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Undo " + runUndo + " Redo " + runRedo); } catch (DatabaseException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -231,7 +231,7 @@ public abstract class AbstractVTKNodeMap implements VT @SuppressWarnings("unchecked") private void receiveAdd(E node, String id, boolean db) { - if (DEBUG) System.out.println("receiveAdd " + debugString(node) + " " + id + " " + db); + if (LOGGER.isTraceEnabled()) LOGGER.trace("receiveAdd " + debugString(node) + " " + id + " " + db); synchronized (syncMutex) { for (Pair n : added) { if (n.first.equals(node)) @@ -249,7 +249,7 @@ public abstract class AbstractVTKNodeMap implements VT @SuppressWarnings("unchecked") private void receiveRemove(E node, String id, boolean db) { - if (DEBUG) System.out.println("receiveRemove " + debugString(node) + " " + id + " " + db); + if (LOGGER.isTraceEnabled()) LOGGER.trace("receiveRemove " + debugString(node) + " " + id + " " + db); synchronized (syncMutex) { for (Pair n : removed) { if (n.first.equals(node)) @@ -266,7 +266,7 @@ public abstract class AbstractVTKNodeMap implements VT } private void receiveUpdate(E node, String id, boolean db) { - if (DEBUG) System.out.println("receiveUpdate " + debugString(node) + " " + id + " " + db); + if (LOGGER.isTraceEnabled()) LOGGER.trace("receiveUpdate " + debugString(node) + " " + id + " " + db); synchronized (syncMutex) { // for (Pair n : updated) { // if (n.first.equals(node)) @@ -301,7 +301,7 @@ public abstract class AbstractVTKNodeMap implements VT session.syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { - if (DEBUG) System.out.println("Commit " + commitMessage); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Commit " + commitMessage); if (commitMessage != null) { Layer0Utils.addCommentMetadata(graph, commitMessage); graph.markUndoPoint(); @@ -314,19 +314,22 @@ public abstract class AbstractVTKNodeMap implements VT ExceptionUtils.logAndShowError("Cannot commit editor changes", e); } }); + postCommit(); } catch (InvocationTargetException | InterruptedException e) { LOGGER.error("Unexpected exception", e); } } - + + protected void postCommit() {} + protected void commit(WriteGraph graph) throws DatabaseException { synchronized(syncMutex) { - if (DEBUG) System.out.println("Commit"); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Commit"); graphUpdates = true; mapping.updateDomain(graph); graphUpdates = false; clearDeletes(); - if (DEBUG) System.out.println("Commit done"); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Commit done"); } } @@ -336,19 +339,28 @@ public abstract class AbstractVTKNodeMap implements VT public void domainModified() { if (graphUpdates) return; - if (DEBUG)System.out.println("domainModified"); - session.asyncRequest(new ReadRequest() { - + if (LOGGER.isTraceEnabled()) LOGGER.trace("domainModified"); + session.asyncRequest(new UniqueRead() { @Override - public void run(ReadGraph graph) throws DatabaseException { + public Object perform(ReadGraph graph) throws DatabaseException { + return new Object(); + } + }, new SyncProcedure() { + @Override + public void execute(ReadGraph graph, Object result) throws DatabaseException { + // Perform all updates to the model in a single query thread update(graph); + }; + + @Override + public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException { + LOGGER.error("Failed to update pipeline changes" + throwable); } }); - } protected void reset(ReadGraph graph) throws MappingException { - if (DEBUG) System.out.println("Reset"); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Reset"); graphUpdates = true; mapping.getRangeModified().clear(); @@ -362,12 +374,14 @@ public abstract class AbstractVTKNodeMap implements VT private boolean useFullSyncWithUndo = false; protected void update(ReadGraph graph) throws DatabaseException { - synchronized (syncMutex) { - if (DEBUG) System.out.println("Graph update start"); - - if (runUndo && useFullSyncWithUndo) { + if (LOGGER.isTraceEnabled()) LOGGER.trace("Graph update start"); + + if (runUndo && useFullSyncWithUndo) { + synchronized (syncMutex) { reset(graph); - } else { + } + } else { + synchronized (syncMutex) { graphUpdates = true; for (DBObject domainObject : mapping.getDomainModified()) { @SuppressWarnings("unchecked") @@ -375,23 +389,29 @@ public abstract class AbstractVTKNodeMap implements VT if (rangeObject != null) graphModified.add(rangeObject); } - mapping.updateRange(graph); + + } + + mapping.updateRange(graph); + + synchronized (syncMutex) { graphModified.clear(); syncDeletes(); - clearDeletes(); - graphUpdates = false; - } - - if (mapping.isRangeModified() && !runUndo && !runRedo) - commit((String)null); + } - if (DEBUG) System.out.println("Graph update done"); + clearDeletes(); + graphUpdates = false; } + + if (mapping.isRangeModified() && !runUndo && !runRedo) + commit((String)null); + + if (LOGGER.isTraceEnabled()) LOGGER.trace("Graph update done"); } @Override public void rangeModified() { - //System.out.println("rangeModified"); + //LOGGER.trace("rangeModified"); } @@ -428,7 +448,6 @@ public abstract class AbstractVTKNodeMap implements VT * This code here synchronizes removed and added objects to collect deletable objects. (a deletable object is one which is removed but not added). * */ - @SuppressWarnings("unused") protected void syncDeletes() { deleteUC.clear(); for (Pair n : removed) { @@ -437,10 +456,10 @@ public abstract class AbstractVTKNodeMap implements VT for (Pair n : added) { deleteUC.remove(n.first); } - if (DEBUG && deleteUC.size() > 0) { - System.out.println("Delete sync"); + if (LOGGER.isTraceEnabled() && deleteUC.size() > 0) { + LOGGER.trace("Delete sync"); for (E n : delete) { - System.out.println(debugString(n)); + LOGGER.trace(debugString(n)); } } delete.addAll(deleteUC); @@ -450,11 +469,10 @@ public abstract class AbstractVTKNodeMap implements VT /** * Clears deletable objects from mapping cache. */ - @SuppressWarnings("unused") protected void clearDeletes() { - if (DEBUG && delete.size() > 0) System.out.println("Delete"); + if (LOGGER.isTraceEnabled() && delete.size() > 0) LOGGER.trace("Delete"); for (E n : delete) { - if (DEBUG) System.out.println(debugString(n)); + if (LOGGER.isTraceEnabled()) LOGGER.trace(debugString(n)); mapping.getRange().remove(n); stopListening(n); } @@ -493,10 +511,10 @@ public abstract class AbstractVTKNodeMap implements VT E n = stack.pop(); boolean conflict = filterChange(removed, n); if (conflict) { - if (DEBUG) System.out.println("Prevent removing " + n); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Prevent removing " + n); //filterChange(added, n) if (filterChange(added, n)) - if (DEBUG) System.out.println("Prevent adding " + n); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Prevent adding " + n); } if (n instanceof ParentNode) { ParentNode pn = (ParentNode)n; @@ -627,7 +645,7 @@ public abstract class AbstractVTKNodeMap implements VT @Override public void nodeAdded(ParentNode node, INode child, String rel) { - if (DEBUG) System.out.println("Node added " + child + " parent " + node); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Node added " + child + " parent " + node); //receiveAdd((E)child, rel ,graphUpdates); receiveAdd((E)child, rel ,graphModified.contains(node)); } @@ -636,7 +654,7 @@ public abstract class AbstractVTKNodeMap implements VT @Override public void nodeRemoved(ParentNode node, INode child, String rel) { - if (DEBUG) System.out.println("Node removed " + child + " parent " + node); + if (LOGGER.isTraceEnabled()) LOGGER.trace("Node removed " + child + " parent " + node); //receiveRemove((E)child, rel, graphUpdates); receiveRemove((E)child, rel, graphModified.contains(node));