]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/ThrottledStyleBase.java
Disable throttling for now, as it doens't work.
[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 // Needs fixing - this will result registration of listeners for nothing in the cache
78 //        long currentTimestamp = System.currentTimeMillis();
79 //        if (lastCalculateTimestamp > (currentTimestamp - interval.get())) {
80 //            LOGGER.debug("Throttling result calculation for {} {} {} {}", runtimeDiagram, entry, groupItem, interval.get());
81 //            return Optional.empty();
82 //        }
83 //        lastCalculateTimestamp = currentTimestamp;
84         // let's calculate
85         return Optional.ofNullable(calculateThrottledStyle(graph, runtimeDiagram, entry, groupItem));
86     }
87
88     public abstract Result calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException;
89
90     @Override
91     public final void applyStyleForNode(EvaluationContext evaluationContext, INode node, Optional<Result> result) {
92         if (!result.equals(Optional.empty())) {
93             applyThrottledStyleForNode(evaluationContext, node, result.get());
94         } else {
95             LOGGER.debug("Do not apply as results are unchanged for {} {} {}", evaluationContext, node, result);
96         }
97     }
98
99     public abstract void applyThrottledStyleForNode(EvaluationContext evaluationContext, INode node, Result result);
100     
101     private static class ProfileUpdateIntervalRead extends UnaryRead<Resource, Long> {
102
103         public ProfileUpdateIntervalRead(Resource parameter) {
104             super(parameter);
105         }
106
107         @Override
108         public Long perform(ReadGraph graph) throws DatabaseException {
109             Resource configuration = graph.getPossibleObject(parameter, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
110             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
111             Long interval = DEFAULT_INTERVAL;
112             if (configuration != null) {
113                 interval = graph.getPossibleRelatedValue(configuration, DN.Diagram_profileUpdateInterval, Bindings.LONG);
114             }
115             return interval != null ? interval : DEFAULT_INTERVAL;
116         }
117     }
118 }