]> gerrit.simantics Code Review - simantics/district.git/commitdiff
More sensible caching for connection lines. 65/2765/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 11 Mar 2019 07:58:01 +0000 (09:58 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Mon, 11 Mar 2019 07:58:49 +0000 (09:58 +0200)
gitlab #37

Change-Id: I8c88d8e6c77963722ebecac86834bdc4c27462e0

org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java
org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java

index 75a46fd4ef0f94dd77eac0ecc31cb59d32cb5b89..5b3d2b43b4c1e4da8582f811323e31d102268fc2 100644 (file)
@@ -19,6 +19,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.diagram.profile.StyleBase;
 import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingResources;
@@ -27,6 +28,10 @@ import org.simantics.scenegraph.g2d.G2DNode;
 import org.simantics.scenegraph.profile.EvaluationContext;
 import org.simantics.scenegraph.profile.common.ProfileVariables;
 import org.simantics.scenegraph.utils.GeometryUtils;
+import org.simantics.scl.compiler.top.ValueNotFound;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
 import org.simantics.structural.stubs.StructuralResource2;
 
 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
@@ -94,7 +99,48 @@ public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
        @Override
        public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
                        throws DatabaseException {
-               return graph.syncRequest(new ElementConnectionRequest(groupItem), TransientCacheListener.instance());
+               DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+               ModelingResources MOD = ModelingResources.getInstance(graph);
+               StructuralResource2 STR = StructuralResource2.getInstance(graph);
+               
+               Resource vertex = groupItem;
+               if (!graph.isInstanceOf(vertex, DN.Vertex))
+                       return Collections.emptyList();
+               
+               double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
+               
+               Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex);
+               if (component == null)
+                       return Collections.emptyList();
+               
+               Resource componentType = graph.getPossibleType(component, STR.Component);
+               if (componentType == null)
+                       return Collections.emptyList();
+               
+               Function1<Resource, List<Resource>> fun = getConnectedComponentsFunctionCached(graph, componentType);
+               if (fun == null)
+                       return Collections.emptyList();
+
+               List<Resource> components = Simantics.applySCLRead(graph, fun, component);
+               
+               if (components == null || components.isEmpty())
+                       return Collections.emptyList();
+               
+               List<Point2D> result = new ArrayList<>(components.size() + 1);
+               result.add(new Point2D.Double(coords[0], coords[1]));
+               for (Resource comp : components) {
+                       Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
+                       Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
+                       if (mappingElement != null) {
+                               double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
+                               result.add(new Point2D.Double(coords2[0], coords2[1]));
+                       }
+                       else {
+                               result.add(null);
+                       }
+               }
+               
+               return result;
        }
        
        @Override
@@ -118,63 +164,35 @@ public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
                ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
        }
        
-       private static final class ElementConnectionRequest extends ResourceRead<List<Point2D>> {
-               private ElementConnectionRequest(Resource resource) {
+       private static Function1<Resource, List<Resource>> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType)
+                       throws DatabaseException {
+               return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance());
+       }
+       
+       private static final class ConnectedComponentsFunctionRequest
+                       extends ResourceRead<Function1<Resource, List<Resource>>> {
+               public ConnectedComponentsFunctionRequest(Resource resource) {
                        super(resource);
                }
-       
+
+               @SuppressWarnings("unchecked")
                @Override
-               public List<Point2D> perform(ReadGraph graph) throws DatabaseException {
-                       DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
-                       ModelingResources MOD = ModelingResources.getInstance(graph);
-                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
-                       
-                       Resource vertex = resource;
-                       if (!graph.isInstanceOf(vertex, DN.Vertex))
-                               return Collections.emptyList();
-                       
-                       double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
-                       
-                       Resource element = graph.getPossibleObject(vertex, DN.MappedComponent);
-                       if (element == null)
-                               return Collections.emptyList();
-                       Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
-                       if (component == null)
-                               return Collections.emptyList();
-                       Resource componentType = graph.getPossibleType(component, STR.Component);
-                       if (componentType == null)
-                               return Collections.emptyList();
-                       
-                       Resource actionsModule = Layer0Utils.getPossibleChild(graph, componentType, "Actions");
+               public Function1<Resource, List<Resource>> perform(ReadGraph graph) throws DatabaseException {
+                       Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, "Actions");
                        if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
-                               return Collections.emptyList();
+                               return null;
                        
-                       List<Resource> components;
+                       String uri = graph.getURI(actionsModule);
+                       SCLContext sclContext = SCLContext.getCurrent();
+                       Object oldGraph = sclContext.get("graph");
                        try {
-                               components = Simantics.applySCL(graph.getURI(actionsModule), "getConnectedComponents", graph, component);
+                               sclContext.put("graph", graph);
+                               return (Function1<Resource, List<Resource>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents");
+                       } catch (ValueNotFound e1) {
+                               return null;
+                       } finally {
+                               sclContext.put("graph", oldGraph);
                        }
-                       catch (DatabaseException e) {
-                               return Collections.emptyList();
-                       }
-                       
-                       if (components == null || components.isEmpty())
-                               return Collections.emptyList();
-                       
-                       List<Point2D> result = new ArrayList<>(components.size() + 1);
-                       result.add(new Point2D.Double(coords[0], coords[1]));
-                       for (Resource comp : components) {
-                               Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
-                               Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
-                               if (mappingElement != null) {
-                                       double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
-                                       result.add(new Point2D.Double(coords2[0], coords2[1]));
-                               }
-                               else {
-                                       result.add(null);
-                               }
-                       }
-                       
-                       return result;
                }
        }
 }
index fd72a67378b62609f3061284aca2528723a78512..24a77575d6d6ed6ac9ba88485bb12c8763efa3f3 100644 (file)
@@ -9,6 +9,8 @@ import org.simantics.datatypes.literal.RGB.Integer;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.common.utils.OrderedSetUtils;
 import org.simantics.db.exception.BindingException;
 import org.simantics.db.exception.DatabaseException;
@@ -181,6 +183,10 @@ public class DistrictNetworkUtil {
         ModelingResources MOD = ModelingResources.getInstance(graph);
         return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
     }
+    
+    public static Resource getMappedComponentCached(ReadGraph graph, Resource vertex) throws DatabaseException {
+        return graph.syncRequest(new MappedComponentRequest(vertex), TransientCacheListener.instance());
+    }
 
     public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
         if (element == null)
@@ -242,4 +248,16 @@ public class DistrictNetworkUtil {
                 DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
                 Bindings.getBindingUnchecked(RGB.Integer.class));
     }
+
+    public static final class MappedComponentRequest extends ResourceRead<Resource> {
+        public MappedComponentRequest(Resource element) {
+            super(element);
+        }
+
+        @Override
+        public Resource perform(ReadGraph graph) throws DatabaseException {
+            return getMappedComponent(graph, resource);
+        }
+    }
+
 }