From: Reino Ruusu Date: Mon, 11 Mar 2019 07:58:01 +0000 (+0200) Subject: More sensible caching for connection lines. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F65%2F2765%2F1;p=simantics%2Fdistrict.git More sensible caching for connection lines. gitlab #37 Change-Id: I8c88d8e6c77963722ebecac86834bdc4c27462e0 --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java index 75a46fd4..5b3d2b43 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java @@ -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> { @@ -94,7 +99,48 @@ public class ConnectionLineStyle extends StyleBase> { @Override public List 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> fun = getConnectedComponentsFunctionCached(graph, componentType); + if (fun == null) + return Collections.emptyList(); + + List components = Simantics.applySCLRead(graph, fun, component); + + if (components == null || components.isEmpty()) + return Collections.emptyList(); + + List 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> { ProfileVariables.denyChild(parent, "*", "districtNetworkConnection"); } - private static final class ElementConnectionRequest extends ResourceRead> { - private ElementConnectionRequest(Resource resource) { + private static Function1> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType) + throws DatabaseException { + return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance()); + } + + private static final class ConnectedComponentsFunctionRequest + extends ResourceRead>> { + public ConnectedComponentsFunctionRequest(Resource resource) { super(resource); } - + + @SuppressWarnings("unchecked") @Override - public List 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> 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 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>) 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 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; } } } diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java index fd72a673..24a77575 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -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 { + public MappedComponentRequest(Resource element) { + super(element); + } + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + return getMappedComponent(graph, resource); + } + } + }