]> gerrit.simantics Code Review - simantics/district.git/commitdiff
First version of throttled profile results - configure via proeprty view 58/2658/1
authorjsimomaa <jani.simomaa@gmail.com>
Tue, 19 Feb 2019 11:25:51 +0000 (13:25 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Tue, 19 Feb 2019 11:25:51 +0000 (13:25 +0200)
gitlab #30

Change-Id: Ie99ee4b3b83911646072d44757570b07f20c450d

org.simantics.district.network.ontology/graph/DistrictNetworkDiagramSettings.pgraph
org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java
org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java
org.simantics.district.network/src/org/simantics/district/network/profile/EdgeThicknessStyle.java
org.simantics.district.network/src/org/simantics/district/network/profile/ThrottledStyleBase.java [new file with mode: 0644]
org.simantics.district.network/src/org/simantics/district/network/profile/VertexSizeStyle.java
org.simantics.district.network/src/org/simantics/district/network/profile/VertexSymbolStyle.java

index 334adbedd9f0461debeae348c0b3d517703afe69..8abc05deed5a9c0229a8ad6b744a877ca9d116b0 100644 (file)
@@ -83,6 +83,8 @@ DN.Diagram
         L0.HasLabel "Diagram Background Color"
     >-- DN.Diagram.drawMapEnabled ==> "Boolean" <R L0.HasProperty : SEL.GenericParameterType
         L0.HasLabel "Draw Map"
+    >-- DN.Diagram.profileUpdateInterval ==> "Long" <R L0.HasProperty : SEL.GenericParameterType
+        L0.HasLabel "Profile update interval"
     @L0.assert DN.Diagram.elementColoringGradientHue
         0.0 : L0.Float
             L0.HasDataType $(Float(unit="deg",range=[0..360]))
@@ -97,6 +99,8 @@ DN.Diagram
     @L0.assert DN.Diagram.nodeScaleBias 0.0
     @L0.assert DN.Diagram.nodeScaleProperty DN.Vertex.ScaleProperty.NominalSupplyPressure
     @L0.assert DN.Diagram.drawMapEnabled true
+    @L0.assert DN.Diagram.profileUpdateInterval
+        2000 : L0.Long
 
 // ----------------------------------------------------------------------------
 // Built-in enumerated ScaleProperty & ThicknessProperty instances
index 97bf9d0a18aacfaf9a88da89407888a5a20b0809..e853952822a4d76333bb15b942db55f816976e2f 100644 (file)
@@ -43,6 +43,8 @@ public class DistrictNetworkResource {
     public final Resource Diagram_nodeScaleGain_Inverse;
     public final Resource Diagram_nodeScaleProperty;
     public final Resource Diagram_nodeScaleProperty_Inverse;
+    public final Resource Diagram_profileUpdateInterval;
+    public final Resource Diagram_profileUpdateInterval_Inverse;
     public final Resource Diagram_splitToMultipleEnabled;
     public final Resource Diagram_splitToMultipleEnabled_Inverse;
     public final Resource Diagram_trackChangesEnabled;
@@ -317,6 +319,8 @@ public class DistrictNetworkResource {
         public static final String Diagram_nodeScaleGain_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/nodeScaleGain/Inverse";
         public static final String Diagram_nodeScaleProperty = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/nodeScaleProperty";
         public static final String Diagram_nodeScaleProperty_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/nodeScaleProperty/Inverse";
+        public static final String Diagram_profileUpdateInterval = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/profileUpdateInterval";
+        public static final String Diagram_profileUpdateInterval_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/profileUpdateInterval/Inverse";
         public static final String Diagram_splitToMultipleEnabled = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/splitToMultipleEnabled";
         public static final String Diagram_splitToMultipleEnabled_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/splitToMultipleEnabled/Inverse";
         public static final String Diagram_trackChangesEnabled = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/trackChangesEnabled";
@@ -601,6 +605,8 @@ public class DistrictNetworkResource {
         Diagram_nodeScaleGain_Inverse = getResourceOrNull(graph, URIs.Diagram_nodeScaleGain_Inverse);
         Diagram_nodeScaleProperty = getResourceOrNull(graph, URIs.Diagram_nodeScaleProperty);
         Diagram_nodeScaleProperty_Inverse = getResourceOrNull(graph, URIs.Diagram_nodeScaleProperty_Inverse);
+        Diagram_profileUpdateInterval = getResourceOrNull(graph, URIs.Diagram_profileUpdateInterval);
+        Diagram_profileUpdateInterval_Inverse = getResourceOrNull(graph, URIs.Diagram_profileUpdateInterval_Inverse);
         Diagram_splitToMultipleEnabled = getResourceOrNull(graph, URIs.Diagram_splitToMultipleEnabled);
         Diagram_splitToMultipleEnabled_Inverse = getResourceOrNull(graph, URIs.Diagram_splitToMultipleEnabled_Inverse);
         Diagram_trackChangesEnabled = getResourceOrNull(graph, URIs.Diagram_trackChangesEnabled);
index 682c91fb1d00782b53ddae99bba9e875b1d14bcf..b74a66198366ccff9b2b24f79c8909c549eb017c 100644 (file)
@@ -17,12 +17,12 @@ import org.simantics.scenegraph.profile.common.ProfileVariables;
  * @author Tuukka Lehtonen
  * @since 1.35.0
  */
-public class DNElementColorStyle extends StyleBase<Color> {
+public class DNElementColorStyle extends ThrottledStyleBase<Color> {
 
        private static final boolean DEBUG = false;
 
        @Override
-       public Color calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
+       public Color calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
                DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheAsyncListener.instance());
                if (ds.elementColoringFunction.isPresent()) {
                        if (DEBUG)
@@ -40,7 +40,7 @@ public class DNElementColorStyle extends StyleBase<Color> {
        }
 
        @Override
-       public void applyStyleForNode(EvaluationContext observer, INode node, Color color) {
+       public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Color color) {
                SingleElementNode n = (SingleElementNode) node;
                for (INode nn : n.getNodes())
                        ProfileVariables.claimNodeProperty(nn, "dynamicColor", color, observer);
index 45b55549333848a94c3d808fbfe4e824e94af877..06dcdf3f05e8fc3d0ff6327e75ff7486ee3b893a 100644 (file)
@@ -5,20 +5,19 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.diagram.profile.StyleBase;
 import org.simantics.scenegraph.INode;
 import org.simantics.scenegraph.g2d.G2DSceneGraph;
 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
 import org.simantics.scenegraph.profile.EvaluationContext;
 import org.simantics.scenegraph.profile.common.ProfileVariables;
 
-public class EdgeThicknessStyle extends StyleBase<Double> {
+public class EdgeThicknessStyle extends ThrottledStyleBase<Double> {
 
        private static final Double PENDING = Double.NaN;
        private static final Double ONE = 1.0;
 
        @Override
-       public Double calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
+       public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
                DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheAsyncListener.instance());
                Double thickness = ONE;
                if (ds.edgeThicknessProperty.isPresent()) {
@@ -34,7 +33,7 @@ public class EdgeThicknessStyle extends StyleBase<Double> {
        }
 
        @Override
-       public void applyStyleForNode(EvaluationContext observer, INode node, Double value) {
+       public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
 //             System.out.println("apply: " + node + " : " + value);
                ConnectionNode n = (ConnectionNode) node;
                if (value == PENDING) {
diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/ThrottledStyleBase.java b/org.simantics.district.network/src/org/simantics/district/network/profile/ThrottledStyleBase.java
new file mode 100644 (file)
index 0000000..f8b817a
--- /dev/null
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2019 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.district.network.profile;
+
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UnaryRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.request.Read;
+import org.simantics.diagram.profile.StyleBase;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.scenegraph.INode;
+import org.simantics.scenegraph.profile.EvaluationContext;
+import org.simantics.scenegraph.profile.Group;
+import org.simantics.scenegraph.profile.Observer;
+import org.simantics.scenegraph.profile.common.ObserverGroupListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class ThrottledStyleBase<Result> extends StyleBase<Optional<Result>> {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ThrottledStyleBase.class);
+    private static final long DEFAULT_INTERVAL = 2000;
+
+    private long lastCalculateTimestamp = 0;
+    private AtomicLong interval = new AtomicLong(DEFAULT_INTERVAL);
+    private Listener<Optional<Result>> resultListener;
+
+    @Override
+    protected Read<Optional<Result>> getStyleCalculationRequest(Resource runtimeDiagram, Resource entry, Resource item) {
+        Simantics.getSession().asyncRequest(new ProfileUpdateIntervalRead(runtimeDiagram), new Listener<Long>() {
+
+            @Override
+            public void execute(Long result) {
+                interval.set(result);
+            }
+
+            @Override
+            public void exception(Throwable t) {
+                LOGGER.error("Could not listen interval from runtime diagram {}", runtimeDiagram, t);
+            }
+
+            @Override
+            public boolean isDisposed() {
+                return resultListener.isDisposed();
+            }
+        });
+        return super.getStyleCalculationRequest(runtimeDiagram, entry, item);
+    }
+
+    @Override
+    protected Listener<Optional<Result>> getStyleResultListener(ObserverGroupListener groupListener, Resource item,
+            Group group, Observer observer, Resource runtimeDiagram) {
+        resultListener = super.getStyleResultListener(groupListener, item, group, observer, runtimeDiagram);
+        return resultListener;
+    }
+
+    @Override
+    public final Optional<Result> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry,
+            Resource groupItem) throws DatabaseException {
+        long currentTimestamp = System.currentTimeMillis();
+        if (lastCalculateTimestamp > (currentTimestamp - interval.get())) {
+            LOGGER.debug("Throttling result calculation for {} {} {} {}", runtimeDiagram, entry, groupItem, interval.get());
+            return Optional.empty();
+        }
+        lastCalculateTimestamp = currentTimestamp;
+        // let's calculate
+        return Optional.ofNullable(calculateThrottledStyle(graph, runtimeDiagram, entry, groupItem));
+    }
+
+    public abstract Result calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException;
+
+    @Override
+    public final void applyStyleForNode(EvaluationContext evaluationContext, INode node, Optional<Result> result) {
+        if (!result.equals(Optional.empty())) {
+            applyThrottledStyleForNode(evaluationContext, node, result.get());
+        } else {
+            LOGGER.debug("Do not apply as results are unchanged for {} {} {}", evaluationContext, node, result);
+        }
+    }
+
+    public abstract void applyThrottledStyleForNode(EvaluationContext evaluationContext, INode node, Result result);
+    
+    private static class ProfileUpdateIntervalRead extends UnaryRead<Resource, Long> {
+
+        public ProfileUpdateIntervalRead(Resource parameter) {
+            super(parameter);
+        }
+
+        @Override
+        public Long perform(ReadGraph graph) throws DatabaseException {
+            Resource configuration = graph.getPossibleObject(parameter, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
+            DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+            Long interval = DEFAULT_INTERVAL;
+            if (configuration != null) {
+                interval = graph.getPossibleRelatedValue(configuration, DN.Diagram_profileUpdateInterval, Bindings.LONG);
+            }
+            return interval != null ? interval : DEFAULT_INTERVAL;
+        }
+    }
+}
index b33cd1c0851f1b1d786e001cf0567a8a2d96def5..d536bc0438b6b78b42a84c390a14cc24df13c002 100644 (file)
@@ -5,20 +5,19 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.diagram.profile.StyleBase;
 import org.simantics.scenegraph.INode;
 import org.simantics.scenegraph.g2d.G2DSceneGraph;
 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
 import org.simantics.scenegraph.profile.EvaluationContext;
 import org.simantics.scenegraph.profile.common.ProfileVariables;
 
-public class VertexSizeStyle extends StyleBase<Double> {
+public class VertexSizeStyle extends ThrottledStyleBase<Double> {
 
        private static final Double PENDING = Double.NaN;
        private static final Double ONE = 1.0;
 
        @Override
-       public Double calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
+       public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
                DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheAsyncListener.instance());
                Double scaling = ONE;
                if (ds.vertexScaleProperty.isPresent()) {
@@ -35,7 +34,7 @@ public class VertexSizeStyle extends StyleBase<Double> {
        }
 
        @Override
-       public void applyStyleForNode(EvaluationContext observer, INode node, Double value) {
+       public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
                //System.out.println("apply: " + node + " : " + value);
                SingleElementNode n = (SingleElementNode) node;
                if (value == PENDING) {
index 65304502adc19c0f4de55ac7b540927a40db1282..98078f82f0ada9ebc70149801f2e6051109e9d4d 100644 (file)
@@ -3,7 +3,6 @@ package org.simantics.district.network.profile;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.diagram.profile.StyleBase;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.scenegraph.INode;
 import org.simantics.scenegraph.g2d.G2DSceneGraph;
@@ -12,11 +11,11 @@ import org.simantics.scenegraph.profile.EvaluationContext;
 import org.simantics.scenegraph.profile.common.ProfileVariables;
 import org.simantics.scl.runtime.function.Function;
 
-public class VertexSymbolStyle extends StyleBase<String> {
+public class VertexSymbolStyle extends ThrottledStyleBase<String> {
 
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
-       public String calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
+       public String calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
                DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
                Resource symbolFunctionResource = graph.getPossibleObject(entry, DN.HasSymbolFunction);
                if (symbolFunctionResource == null)
@@ -30,7 +29,7 @@ public class VertexSymbolStyle extends StyleBase<String> {
        }
 
        @Override
-       public void applyStyleForNode(EvaluationContext observer, INode node, String value) {
+       public void applyThrottledStyleForNode(EvaluationContext observer, INode node, String value) {
                SingleElementNode n = (SingleElementNode) node;
                for (INode nn : n.getNodes())
                        ProfileVariables.claimNodeProperty(nn, "SVG", value, observer);