]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/ThrottledStyleBase.java
First version of throttled profile results - configure via proeprty view
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / ThrottledStyleBase.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2019 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.district.network.profile;
13
14 import java.util.Optional;
15 import java.util.concurrent.atomic.AtomicLong;
16
17 import org.simantics.Simantics;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.UnaryRead;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.procedure.Listener;
24 import org.simantics.db.request.Read;
25 import org.simantics.diagram.profile.StyleBase;
26 import org.simantics.diagram.stubs.DiagramResource;
27 import org.simantics.district.network.ontology.DistrictNetworkResource;
28 import org.simantics.scenegraph.INode;
29 import org.simantics.scenegraph.profile.EvaluationContext;
30 import org.simantics.scenegraph.profile.Group;
31 import org.simantics.scenegraph.profile.Observer;
32 import org.simantics.scenegraph.profile.common.ObserverGroupListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public abstract class ThrottledStyleBase<Result> extends StyleBase<Optional<Result>> {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(ThrottledStyleBase.class);
39     private static final long DEFAULT_INTERVAL = 2000;
40
41     private long lastCalculateTimestamp = 0;
42     private AtomicLong interval = new AtomicLong(DEFAULT_INTERVAL);
43     private Listener<Optional<Result>> resultListener;
44
45     @Override
46     protected Read<Optional<Result>> getStyleCalculationRequest(Resource runtimeDiagram, Resource entry, Resource item) {
47         Simantics.getSession().asyncRequest(new ProfileUpdateIntervalRead(runtimeDiagram), new Listener<Long>() {
48
49             @Override
50             public void execute(Long result) {
51                 interval.set(result);
52             }
53
54             @Override
55             public void exception(Throwable t) {
56                 LOGGER.error("Could not listen interval from runtime diagram {}", runtimeDiagram, t);
57             }
58
59             @Override
60             public boolean isDisposed() {
61                 return resultListener.isDisposed();
62             }
63         });
64         return super.getStyleCalculationRequest(runtimeDiagram, entry, item);
65     }
66
67     @Override
68     protected Listener<Optional<Result>> getStyleResultListener(ObserverGroupListener groupListener, Resource item,
69             Group group, Observer observer, Resource runtimeDiagram) {
70         resultListener = super.getStyleResultListener(groupListener, item, group, observer, runtimeDiagram);
71         return resultListener;
72     }
73
74     @Override
75     public final Optional<Result> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry,
76             Resource groupItem) throws DatabaseException {
77         long currentTimestamp = System.currentTimeMillis();
78         if (lastCalculateTimestamp > (currentTimestamp - interval.get())) {
79             LOGGER.debug("Throttling result calculation for {} {} {} {}", runtimeDiagram, entry, groupItem, interval.get());
80             return Optional.empty();
81         }
82         lastCalculateTimestamp = currentTimestamp;
83         // let's calculate
84         return Optional.ofNullable(calculateThrottledStyle(graph, runtimeDiagram, entry, groupItem));
85     }
86
87     public abstract Result calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException;
88
89     @Override
90     public final void applyStyleForNode(EvaluationContext evaluationContext, INode node, Optional<Result> result) {
91         if (!result.equals(Optional.empty())) {
92             applyThrottledStyleForNode(evaluationContext, node, result.get());
93         } else {
94             LOGGER.debug("Do not apply as results are unchanged for {} {} {}", evaluationContext, node, result);
95         }
96     }
97
98     public abstract void applyThrottledStyleForNode(EvaluationContext evaluationContext, INode node, Result result);
99     
100     private static class ProfileUpdateIntervalRead extends UnaryRead<Resource, Long> {
101
102         public ProfileUpdateIntervalRead(Resource parameter) {
103             super(parameter);
104         }
105
106         @Override
107         public Long perform(ReadGraph graph) throws DatabaseException {
108             Resource configuration = graph.getPossibleObject(parameter, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
109             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
110             Long interval = DEFAULT_INTERVAL;
111             if (configuration != null) {
112                 interval = graph.getPossibleRelatedValue(configuration, DN.Diagram_profileUpdateInterval, Bindings.LONG);
113             }
114             return interval != null ? interval : DEFAULT_INTERVAL;
115         }
116     }
117 }