]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/ThrottledStyleBase.java
8e5421a84b3693b8362778266206a61916f3302d
[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.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.UnaryRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.Listener;
23 import org.simantics.db.request.Read;
24 import org.simantics.diagram.profile.StyleBase;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.scenegraph.INode;
28 import org.simantics.scenegraph.profile.EvaluationContext;
29 import org.simantics.scenegraph.profile.Group;
30 import org.simantics.scenegraph.profile.Observer;
31 import org.simantics.scenegraph.profile.common.ObserverGroupListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class ThrottledStyleBase<Result> extends StyleBase<Optional<Result>> {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(ThrottledStyleBase.class);
38     private static final long DEFAULT_INTERVAL = 2000;
39
40     //private long lastCalculateTimestamp = 0;
41     private AtomicLong interval = new AtomicLong(DEFAULT_INTERVAL);
42     private Listener<Optional<Result>> resultListener;
43
44     @Override
45     protected Read<Optional<Result>> getStyleCalculationRequest(Resource runtimeDiagram, Resource entry, Resource item) {
46         //Simantics.getSession().asyncRequest(new ProfileUpdateIntervalRead(runtimeDiagram), new ProfileUpdateIntervalListener(runtimeDiagram, entry, item)); 
47         return super.getStyleCalculationRequest(runtimeDiagram, entry, item);
48     }
49
50     @Override
51     protected Listener<Optional<Result>> getStyleResultListener(ObserverGroupListener groupListener, Resource item,
52             Group group, Observer observer, Resource runtimeDiagram) {
53         resultListener = super.getStyleResultListener(groupListener, item, group, observer, runtimeDiagram);
54         return resultListener;
55     }
56
57     @Override
58     public final Optional<Result> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry,
59             Resource groupItem) throws DatabaseException {
60 // Needs fixing - this will result registration of listeners for nothing in the cache
61 //        long currentTimestamp = System.currentTimeMillis();
62 //        if (lastCalculateTimestamp > (currentTimestamp - interval.get())) {
63 //            LOGGER.debug("Throttling result calculation for {} {} {} {}", runtimeDiagram, entry, groupItem, interval.get());
64 //            return Optional.empty();
65 //        }
66 //        lastCalculateTimestamp = currentTimestamp;
67         // let's calculate
68         return Optional.ofNullable(calculateThrottledStyle(graph, runtimeDiagram, entry, groupItem));
69     }
70
71     public abstract Result calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException;
72
73     @Override
74     public final void applyStyleForNode(EvaluationContext evaluationContext, INode node, Optional<Result> result) {
75         if (!Optional.empty().equals(result) && result != null) {
76             applyThrottledStyleForNode(evaluationContext, node, result.get());
77         } else {
78             LOGGER.debug("Do not apply as results are unchanged for {} {} {}", evaluationContext, node, result);
79             // TODO: fix this duplicate method invocation with null
80             applyThrottledStyleForNode(evaluationContext, node, null);
81         }
82     }
83
84     public abstract void applyThrottledStyleForNode(EvaluationContext evaluationContext, INode node, Result result);
85     
86     private static class ProfileUpdateIntervalRead extends UnaryRead<Resource, Long> {
87
88         public ProfileUpdateIntervalRead(Resource parameter) {
89             super(parameter);
90         }
91
92         @Override
93         public Long perform(ReadGraph graph) throws DatabaseException {
94             Resource configuration = graph.getPossibleObject(parameter, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
95             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
96             Long interval = DEFAULT_INTERVAL;
97             if (configuration != null) {
98                 // TODO: take back to use when throttled profile is actually working
99                 // interval = graph.getPossibleRelatedValue(configuration, DN.Diagram_profileUpdateInterval, Bindings.LONG);
100             }
101             return interval != null ? interval : DEFAULT_INTERVAL;
102         }
103     }
104     
105     private class ProfileUpdateIntervalListener implements Listener<Long> {
106
107         private Resource runtimeDiagram;
108         private Resource entry;
109         private Resource item;
110
111         public ProfileUpdateIntervalListener(Resource runtimeDiagram, Resource entry, Resource item) {
112             this.runtimeDiagram = runtimeDiagram;
113             this.entry = entry;
114             this.item = item;
115         }
116
117         @Override
118         public void execute(Long result) {
119             interval.set(result);
120         }
121
122         @Override
123         public void exception(Throwable t) {
124             LOGGER.error("Could not listen interval from runtime diagram {}", runtimeDiagram, t);
125         }
126
127         @Override
128         public boolean isDisposed() {
129             return resultListener.isDisposed();
130         }
131
132         @Override
133         public int hashCode() {
134             final int prime = 31;
135             int result = 1;
136             result = prime * result + getEnclosingInstance().hashCode();
137             result = prime * result + ((entry == null) ? 0 : entry.hashCode());
138             result = prime * result + ((item == null) ? 0 : item.hashCode());
139             result = prime * result + ((runtimeDiagram == null) ? 0 : runtimeDiagram.hashCode());
140             return result;
141         }
142
143         @Override
144         public boolean equals(Object obj) {
145             if (this == obj)
146                 return true;
147             if (obj == null)
148                 return false;
149             if (getClass() != obj.getClass())
150                 return false;
151             ProfileUpdateIntervalListener other = (ProfileUpdateIntervalListener) obj;
152             if (!getEnclosingInstance().equals(other.getEnclosingInstance()))
153                 return false;
154             if (entry == null) {
155                 if (other.entry != null)
156                     return false;
157             } else if (!entry.equals(other.entry))
158                 return false;
159             if (item == null) {
160                 if (other.item != null)
161                     return false;
162             } else if (!item.equals(other.item))
163                 return false;
164             if (runtimeDiagram == null) {
165                 if (other.runtimeDiagram != null)
166                     return false;
167             } else if (!runtimeDiagram.equals(other.runtimeDiagram))
168                 return false;
169             return true;
170         }
171
172         private ThrottledStyleBase<Result> getEnclosingInstance() {
173             return ThrottledStyleBase.this;
174         }
175     }
176
177 }