1 /*******************************************************************************
2 * Copyright (c) 2007, 2019 Association for Decentralized Information Management in
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.district.network.profile;
14 import java.util.Optional;
15 import java.util.concurrent.atomic.AtomicLong;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.UnaryRead;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.procedure.Listener;
22 import org.simantics.db.request.Read;
23 import org.simantics.diagram.profile.StyleBase;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.district.network.ontology.DistrictNetworkResource;
26 import org.simantics.scenegraph.INode;
27 import org.simantics.scenegraph.profile.EvaluationContext;
28 import org.simantics.scenegraph.profile.Group;
29 import org.simantics.scenegraph.profile.Observer;
30 import org.simantics.scenegraph.profile.common.ObserverGroupListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public abstract class ThrottledStyleBase<Result> extends StyleBase<Optional<Result>> {
36 private static final Logger LOGGER = LoggerFactory.getLogger(ThrottledStyleBase.class);
37 private static final long DEFAULT_INTERVAL = 2000;
39 //private long lastCalculateTimestamp = 0;
40 private AtomicLong interval = new AtomicLong(DEFAULT_INTERVAL);
41 private Listener<Optional<Result>> resultListener;
44 protected Read<Optional<Result>> getStyleCalculationRequest(Resource runtimeDiagram, Resource entry, Resource item) {
45 //Simantics.getSession().asyncRequest(new ProfileUpdateIntervalRead(runtimeDiagram), new ProfileUpdateIntervalListener(runtimeDiagram, entry, item));
46 return super.getStyleCalculationRequest(runtimeDiagram, entry, item);
50 protected Listener<Optional<Result>> getStyleResultListener(ObserverGroupListener groupListener, Resource item,
51 Group group, Observer observer, Resource runtimeDiagram) {
52 resultListener = super.getStyleResultListener(groupListener, item, group, observer, runtimeDiagram);
53 return resultListener;
57 public final Optional<Result> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry,
58 Resource groupItem) throws DatabaseException {
59 // Needs fixing - this will result registration of listeners for nothing in the cache
60 // long currentTimestamp = System.currentTimeMillis();
61 // if (lastCalculateTimestamp > (currentTimestamp - interval.get())) {
62 // LOGGER.debug("Throttling result calculation for {} {} {} {}", runtimeDiagram, entry, groupItem, interval.get());
63 // return Optional.empty();
65 // lastCalculateTimestamp = currentTimestamp;
67 return Optional.ofNullable(calculateThrottledStyle(graph, runtimeDiagram, entry, groupItem));
70 public abstract Result calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException;
73 public final void applyStyleForNode(EvaluationContext evaluationContext, INode node, Optional<Result> result) {
74 if (!Optional.empty().equals(result) && result != null) {
75 applyThrottledStyleForNode(evaluationContext, node, result.get());
77 LOGGER.debug("Do not apply as results are unchanged for {} {} {}", evaluationContext, node, result);
78 // TODO: fix this duplicate method invocation with null
79 applyThrottledStyleForNode(evaluationContext, node, null);
83 public abstract void applyThrottledStyleForNode(EvaluationContext evaluationContext, INode node, Result result);
85 private static class ProfileUpdateIntervalRead extends UnaryRead<Resource, Long> {
87 public ProfileUpdateIntervalRead(Resource parameter) {
92 public Long perform(ReadGraph graph) throws DatabaseException {
93 Resource configuration = graph.getPossibleObject(parameter, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
94 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
95 Long interval = DEFAULT_INTERVAL;
96 if (configuration != null) {
97 // TODO: take back to use when throttled profile is actually working
98 // interval = graph.getPossibleRelatedValue(configuration, DN.Diagram_profileUpdateInterval, Bindings.LONG);
100 return interval != null ? interval : DEFAULT_INTERVAL;
104 private class ProfileUpdateIntervalListener implements Listener<Long> {
106 private Resource runtimeDiagram;
107 private Resource entry;
108 private Resource item;
110 public ProfileUpdateIntervalListener(Resource runtimeDiagram, Resource entry, Resource item) {
111 this.runtimeDiagram = runtimeDiagram;
117 public void execute(Long result) {
118 interval.set(result);
122 public void exception(Throwable t) {
123 LOGGER.error("Could not listen interval from runtime diagram {}", runtimeDiagram, t);
127 public boolean isDisposed() {
128 return resultListener.isDisposed();
132 public int hashCode() {
133 final int prime = 31;
135 result = prime * result + getEnclosingInstance().hashCode();
136 result = prime * result + ((entry == null) ? 0 : entry.hashCode());
137 result = prime * result + ((item == null) ? 0 : item.hashCode());
138 result = prime * result + ((runtimeDiagram == null) ? 0 : runtimeDiagram.hashCode());
143 public boolean equals(Object obj) {
148 if (getClass() != obj.getClass())
150 ProfileUpdateIntervalListener other = (ProfileUpdateIntervalListener) obj;
151 if (!getEnclosingInstance().equals(other.getEnclosingInstance()))
154 if (other.entry != null)
156 } else if (!entry.equals(other.entry))
159 if (other.item != null)
161 } else if (!item.equals(other.item))
163 if (runtimeDiagram == null) {
164 if (other.runtimeDiagram != null)
166 } else if (!runtimeDiagram.equals(other.runtimeDiagram))
171 private ThrottledStyleBase<Result> getEnclosingInstance() {
172 return ThrottledStyleBase.this;