X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fadapter%2FGraphToDiagramSynchronizer.java;h=226f47daa76b62ea90f572d561aa75e6bc830295;hb=28438fa467ae60dd63515be2df724c6ff9c299c9;hp=2b72b3e0f9a8ee1133ea853484c94a60d89f3dc4;hpb=e0334c5555088193846c692743067db8e43548d6;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java index 2b72b3e0f..226f47daa 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java @@ -11,9 +11,6 @@ *******************************************************************************/ package org.simantics.diagram.adapter; -import gnu.trove.map.hash.TObjectIntHashMap; -import gnu.trove.set.hash.THashSet; - import java.awt.geom.AffineTransform; import java.lang.reflect.InvocationTargetException; import java.util.ArrayDeque; @@ -49,6 +46,7 @@ import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter; import org.simantics.db.common.procedure.adapter.CacheListener; import org.simantics.db.common.procedure.adapter.ListenerSupport; import org.simantics.db.common.procedure.adapter.ProcedureAdapter; +import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; import org.simantics.db.common.request.AsyncReadRequest; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.session.SessionEventListenerAdapter; @@ -157,6 +155,11 @@ import org.simantics.utils.strings.EString; import org.simantics.utils.threads.ThreadUtils; import org.simantics.utils.threads.logger.ITask; import org.simantics.utils.threads.logger.ThreadLogger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import gnu.trove.map.hash.TObjectIntHashMap; +import gnu.trove.set.hash.THashSet; /** * This class loads a diagram contained in the graph database into the runtime @@ -249,6 +252,8 @@ import org.simantics.utils.threads.logger.ThreadLogger; */ public class GraphToDiagramSynchronizer extends AbstractDisposable implements IDiagramLoader, IModifiableSynchronizationContext { + private static final Logger LOGGER = LoggerFactory.getLogger(GraphToDiagramSynchronizer.class); + /** * Controls whether the class adds hint listeners to each diagram element * that try to perform basic sanity checks on changes happening in element @@ -611,6 +616,17 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID */ ConcurrentMap dataConnection = new ConcurrentHashMap(); + + void mapElementIfNew(final Object data, final IElement element) { + IElement mapped = getMappedElement(data); + if(mapped == null) { + mapElement(data, element); + currentUpdater.addedElements.add(element); + currentUpdater.addedElementMap.put(data, element); + } + } + + /** * @param data * @param element @@ -1110,7 +1126,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } } catch (InterruptedException e) { // Shouldn't happen. - e.printStackTrace(); + LOGGER.error("Dispose interrupted!", e); } finally { detachSessionListener(); @@ -1301,7 +1317,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID task4.finish(); // ITask task5 = ThreadLogger.getInstance().begin("DiagramContentRequest2"); ITask task42 = ThreadLogger.getInstance().begin("DiagramContentRequest2"); - DiagramContents contents = g.syncRequest(query); + DiagramContents contents = g.syncRequest(query, TransientCacheAsyncListener.instance()); //System.err.println("contents: " + contents); task42.finish(); // task5.finish(); @@ -1342,7 +1358,11 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID g.getSession().asyncRequest(new AsyncReadRequest() { @Override public void run(AsyncReadGraph graph) { - profileObserver.listen(graph, GraphToDiagramSynchronizer.this); + ProfileObserver po = profileObserver; + if (po != null) + po.listen(graph, GraphToDiagramSynchronizer.this); + else + LOGGER.info("profileObserver has been disposed already!"); } }); @@ -1609,11 +1629,11 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } public void clear() { - // Prevent DiagramContents leakage through DisposableListeners. - lastContent = null; - content = null; - changes = null; - + // Prevent DiagramContents leakage through DisposableListeners. + lastContent = null; + content = null; + changes = null; + this.addedElements.clear(); this.removedElements.clear(); this.addedConnectionSegments.clear(); @@ -1629,7 +1649,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID this.removedRouteGraphConnections.clear(); } - void processNodes(ReadGraph graph) throws DatabaseException { + void processNodes(AsyncReadGraph graph) throws DatabaseException { for (Map.Entry entry : changes.elements.entrySet()) { @@ -1654,10 +1674,13 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID // TODO: Connection loading has no listening, changes :Connection will not be noticed by this code! Listener loadListener = new DisposableListener(canvasListenerSupport) { - @Override - public String toString() { - return "Connection load listener for " + element; - } + + boolean firstTime = true; + + @Override + public String toString() { + return "Connection load listener for " + element; + } @Override public void execute(IElement loaded) { // Invoked when the element has been loaded. @@ -1669,6 +1692,19 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID return; } + if (firstTime) { + + mapElement(element, loaded); + synchronized (GraphToDiagramUpdater.this) { + addedElements.add(loaded); + addedElementMap.put(element, loaded); + addedConnectionMap.put(element, loaded); + } + + firstTime = false; + + } + Object data = loaded.getHint(ElementHints.KEY_OBJECT); // Logic for disposing listener @@ -1705,19 +1741,9 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } }; - graph.syncRequest(new ConnectionRequest(canvas, diagram, element, errorHandler, loadListener), new AsyncProcedure() { + graph.asyncRequest(new ConnectionRequest(canvas, diagram, element, errorHandler, loadListener), new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, final IElement e) { - if (e == null) - return; - - //System.out.println("ConnectionRequestProcedure " + e); - mapElement(element, e); - synchronized (GraphToDiagramUpdater.this) { - addedElements.add(e); - addedElementMap.put(element, e); - addedConnectionMap.put(element, e); - } // Read connection type graph.forSingleType(element, br.DIA.Connection, new Procedure() { @@ -1729,13 +1755,20 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID @Override public void execute(Resource connectionType) { synchronized (GraphToDiagramUpdater.this) { + IElement mapped = getMappedElement(element); + assert(mapped != null); + + if (DebugPolicy.DEBUG_CONNECTION_LISTENER) + System.out.println("CONNECTION ENTITY CREATED " + e + " " + element); + //System.out.println("new connection entity " + e); - ConnectionEntityImpl entity = new ConnectionEntityImpl(element, connectionType, e); - e.setHint(ElementHints.KEY_CONNECTION_ENTITY, entity); + ConnectionEntityImpl entity = new ConnectionEntityImpl(element, connectionType, mapped); + mapped.setHint(ElementHints.KEY_CONNECTION_ENTITY, entity); addedConnectionEntities.put(element, entity); } } }); + } @Override @@ -1746,10 +1779,13 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } else if (content.nodeSet.contains(element)) { Listener loadListener = new DisposableListener(canvasListenerSupport) { - @Override - public String toString() { - return "Node load listener for " + element; - } + + boolean firstTime = true; + + @Override + public String toString() { + return "Node load listener for " + element; + } @Override public void execute(IElement loaded) { // Invoked when the element has been loaded. @@ -1761,6 +1797,22 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID return; } + if (firstTime) { + + // This is invoked before the element is actually loaded. + //System.out.println("NodeRequestProcedure " + e); + if (DebugPolicy.DEBUG_NODE_LOAD) + System.out.println("MAPPING ADDED NODE: " + element + " -> " + loaded); + mapElement(element, loaded); + synchronized (GraphToDiagramUpdater.this) { + addedElements.add(loaded); + addedElementMap.put(element, loaded); + } + + firstTime = false; + + } + Object data = loaded.getHint(ElementHints.KEY_OBJECT); // Logic for disposing listener @@ -1798,21 +1850,9 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID }; //System.out.println("NODE REQUEST: " + element); - graph.syncRequest(new NodeRequest(canvas, diagram, element, loadListener), new AsyncProcedure() { + graph.asyncRequest(new NodeRequest(canvas, diagram, element, loadListener), new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, IElement e) { - if (e == null) - return; - - // This is invoked before the element is actually loaded. - //System.out.println("NodeRequestProcedure " + e); - if (DebugPolicy.DEBUG_NODE_LOAD) - System.out.println("MAPPING ADDED NODE: " + element + " -> " + e); - mapElement(element, e); - synchronized (GraphToDiagramUpdater.this) { - addedElements.add(e); - addedElementMap.put(element, e); - } } @Override @@ -1953,10 +1993,13 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID continue; Listener loadListener = new DisposableListener(canvasListenerSupport) { - @Override - public String toString() { - return "processRouteGraphConnections " + connection; - } + + boolean firstTime = true; + + @Override + public String toString() { + return "processRouteGraphConnections " + connection; + } @Override public void execute(IElement loaded) { // Invoked when the element has been loaded. @@ -1968,6 +2011,18 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID return; } + if(firstTime) { + if (DebugPolicy.DEBUG_NODE_LOAD) + System.out.println("MAPPING ADDED ROUTE GRAPH CONNECTION: " + connection + " -> " + loaded); + mapElement(connection, loaded); + synchronized (GraphToDiagramUpdater.this) { + addedElements.add(loaded); + addedElementMap.put(connection, loaded); + addedRouteGraphConnectionMap.put(connection, loaded); + } + firstTime = false; + } + Object data = loaded.getHint(ElementHints.KEY_OBJECT); // Logic for disposing listener @@ -2022,18 +2077,6 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID graph.syncRequest(new ConnectionRequest(canvas, diagram, connection, errorHandler, loadListener), new Procedure() { @Override public void execute(final IElement e) { - if (e == null) - return; - - //System.out.println("ConnectionRequestProcedure " + e); - if (DebugPolicy.DEBUG_NODE_LOAD) - System.out.println("MAPPING ADDED ROUTE GRAPH CONNECTION: " + connection + " -> " + e); - mapElement(connection, e); - synchronized (GraphToDiagramUpdater.this) { - addedElements.add(e); - addedElementMap.put(connection, e); - addedRouteGraphConnectionMap.put(connection, e); - } } @Override public void exception(Throwable throwable) { @@ -2062,7 +2105,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID return assertMappedConnection(connection); } - void processBranchPoints(ReadGraph graph) throws DatabaseException { + void processBranchPoints(AsyncReadGraph graph) throws DatabaseException { for (Map.Entry entry : changes.branchPoints.entrySet()) { final Resource element = entry.getKey(); @@ -2073,7 +2116,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID IElement mappedElement = getMappedElement(element); if (mappedElement == null) { if (DebugPolicy.DEBUG_NODE_LOAD) - graph.syncRequest(new ReadRequest() { + graph.asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { System.out.println(" EXTERNALLY ADDED BRANCH POINT: " @@ -2083,10 +2126,13 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID }); Listener loadListener = new DisposableListener(canvasListenerSupport) { - @Override - public String toString() { - return "processBranchPoints for " + element; - } + + boolean firstTime = true; + + @Override + public String toString() { + return "processBranchPoints for " + element; + } @Override public void execute(IElement loaded) { // Invoked when the element has been loaded. @@ -2098,6 +2144,21 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID return; } + if (firstTime) { + + mapElement(element, loaded); + synchronized (GraphToDiagramUpdater.this) { + addedBranchPoints.add(loaded); + addedElementMap.put(element, loaded); + ConnectionEntityImpl ce = getConnectionEntity(element); + loaded.setHint(ElementHints.KEY_CONNECTION_ENTITY, ce); + loaded.setHint(ElementHints.KEY_PARENT_ELEMENT, ce.getConnectionElement()); + } + + firstTime = false; + + } + Object data = loaded.getHint(ElementHints.KEY_OBJECT); if (addedElementMap.containsKey(data)) { // This element was just loaded, in @@ -2125,19 +2186,9 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } }; - graph.syncRequest(new NodeRequest(canvas, diagram, element, loadListener), new AsyncProcedure() { + graph.asyncRequest(new NodeRequest(canvas, diagram, element, loadListener), new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, IElement e) { - if (e != null) { - mapElement(element, e); - synchronized (GraphToDiagramUpdater.this) { - addedBranchPoints.add(e); - addedElementMap.put(element, e); - ConnectionEntityImpl ce = getConnectionEntity(element); - e.setHint(ElementHints.KEY_CONNECTION_ENTITY, ce); - e.setHint(ElementHints.KEY_PARENT_ELEMENT, ce.getConnectionElement()); - } - } } @Override @@ -2169,7 +2220,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } } - void processConnectionSegments(ReadGraph graph) throws DatabaseException { + void processConnectionSegments(AsyncReadGraph graph) throws DatabaseException { ConnectionSegmentAdapter adapter = connectionSegmentAdapter; for (Map.Entry entry : changes.connectionSegments.entrySet()) { @@ -2181,7 +2232,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID IElement mappedElement = getMappedElement(seg); if (mappedElement == null) { if (DebugPolicy.DEBUG_EDGE_LOAD) - graph.syncRequest(new ReadRequest() { + graph.asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { System.out.println(" EXTERNALLY ADDED CONNECTION SEGMENT: " + seg.toString() @@ -2189,13 +2240,13 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } }); - graph.syncRequest(new EdgeRequest(canvas, errorHandler, canvasListenerSupport, diagram, adapter, seg), new AsyncProcedure() { + graph.asyncRequest(new EdgeRequest(GraphToDiagramSynchronizer.this, canvas, errorHandler, canvasListenerSupport, diagram, adapter, seg), new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, IElement e) { if (DebugPolicy.DEBUG_EDGE_LOAD) - System.out.println("ADDED EDGE LOADED: " + e); + System.out.println("ADDED EDGE LOADED: " + e + " " + seg); + if (e != null) { - mapElement(seg, e); synchronized (GraphToDiagramUpdater.this) { addedConnectionSegments.add(e); addedElementMap.put(seg, e); @@ -2217,7 +2268,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID case REMOVED: { final IElement e = getMappedElement(seg); if (DebugPolicy.DEBUG_EDGE_LOAD) - graph.syncRequest(new ReadRequest() { + graph.asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { System.out.println(" EXTERNALLY REMOVED CONNECTION SEGMENT: " + seg.toString() + " - " @@ -2268,13 +2319,15 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID if (changes.isEmpty()) return; + ITask threadLog = ThreadLogger.task("processNodes"); + // NOTE: This order is important. Object task = Timing.BEGIN("processNodesConnections"); //System.out.println("---- PROCESS NODES & CONNECTIONS BEGIN"); if (!changes.elements.isEmpty()) { - graph.syncRequest(new ReadRequest() { + graph.syncRequest(new AsyncReadRequest() { @Override - public void run(ReadGraph graph) throws DatabaseException { + public void run(AsyncReadGraph graph) throws DatabaseException { processNodes(graph); } @Override @@ -2285,13 +2338,21 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } //System.out.println("---- PROCESS NODES & CONNECTIONS END"); + threadLog.finish(); + + threadLog = ThreadLogger.task("processConnections"); + processConnections(); + threadLog.finish(); + + threadLog = ThreadLogger.task("processBranchPoints"); + //System.out.println("---- PROCESS BRANCH POINTS BEGIN"); if (!changes.branchPoints.isEmpty()) { - graph.syncRequest(new ReadRequest() { + graph.syncRequest(new AsyncReadRequest() { @Override - public void run(ReadGraph graph) throws DatabaseException { + public void run(AsyncReadGraph graph) throws DatabaseException { processBranchPoints(graph); } @Override @@ -2302,14 +2363,19 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } //System.out.println("---- PROCESS BRANCH POINTS END"); + threadLog.finish(); + Timing.END(task); + + threadLog = ThreadLogger.task("processConnectionSegments"); + task = Timing.BEGIN("processConnectionSegments"); //System.out.println("---- PROCESS CONNECTION SEGMENTS BEGIN"); if (!changes.connectionSegments.isEmpty()) { - graph.syncRequest(new ReadRequest() { + graph.syncRequest(new AsyncReadRequest() { @Override - public void run(ReadGraph graph) throws DatabaseException { + public void run(AsyncReadGraph graph) throws DatabaseException { processConnectionSegments(graph); } @Override @@ -2320,8 +2386,12 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } //System.out.println("---- PROCESS CONNECTION SEGMENTS END"); + threadLog.finish(); + Timing.END(task); + threadLog = ThreadLogger.task("processRouteGraphConnections"); + task = Timing.BEGIN("processRouteGraphConnections"); if (!changes.routeGraphConnections.isEmpty()) { graph.syncRequest(new ReadRequest() { @@ -2337,6 +2407,8 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } Timing.END(task); + threadLog.finish(); + //System.out.println("---- AFTER LOADING"); //for (IElement e : addedElements) // System.out.println(" ADDED ELEMENT: " + e); @@ -2344,7 +2416,12 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID // System.out.println(" ADDED BRANCH POINTS: " + e); task = Timing.BEGIN("executeDeferredLoaders"); + threadLog = ThreadLogger.task("executeDeferredLoaders"); + executeDeferredLoaders(graph); + + threadLog.finish(); + Timing.END(task); } @@ -2385,17 +2462,15 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID return "defaultConnectionSegmentAdapter"; } }, new DisposableListener(listenerSupport) { - - @Override - public String toString() { - return "DefaultConnectionSegmentAdapter listener for " + edge; - } - + + @Override + public String toString() { + return "DefaultConnectionSegmentAdapter listener for " + edge; + } + @Override public void execute(IElement loaded) { // Invoked when the element has been loaded. - if (DebugPolicy.DEBUG_EDGE_LISTENER) - System.out.println("EDGE LoadListener for " + loaded); if (loaded == null) { disposeListener(); @@ -2403,6 +2478,10 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID } Object data = loaded.getHint(ElementHints.KEY_OBJECT); + + if (DebugPolicy.DEBUG_EDGE_LISTENER) + System.out.println("EDGE LoadListener for " + loaded + " " + data); + if (addedElementMap.containsKey(data)) { // This element was just loaded, in // which case its hints need to @@ -2667,9 +2746,9 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID GraphToDiagramUpdater updater = new GraphToDiagramUpdater(lastContent, content, changes); GraphToDiagramSynchronizer.this.currentUpdater = updater; try { - updater.process(graph); + updater.process(graph); } finally { - GraphToDiagramSynchronizer.this.currentUpdater = null; + GraphToDiagramSynchronizer.this.currentUpdater = null; } Timing.END(task); @@ -3430,7 +3509,7 @@ public class GraphToDiagramSynchronizer extends AbstractDisposable implements ID Resource resource = ElementUtils.adapt(ec, Resource.class); if (resource == null) { pass = false; - new Exception("Attempted to add an element to the diagram that is not adaptable to Resource: " + e + ", class: " + ec).printStackTrace(); + LOGGER.error("", new Exception("Attempted to add an element to the diagram that is not adaptable to Resource: " + e + ", class: " + ec)); } // Sanity check connection hints