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.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;
36 public abstract class ThrottledStyleBase<Result> extends StyleBase<Optional<Result>> {
38 private static final Logger LOGGER = LoggerFactory.getLogger(ThrottledStyleBase.class);
39 private static final long DEFAULT_INTERVAL = 2000;
41 //private long lastCalculateTimestamp = 0;
42 private AtomicLong interval = new AtomicLong(DEFAULT_INTERVAL);
43 private Listener<Optional<Result>> resultListener;
46 protected Read<Optional<Result>> getStyleCalculationRequest(Resource runtimeDiagram, Resource entry, Resource item) {
47 Simantics.getSession().asyncRequest(new ProfileUpdateIntervalRead(runtimeDiagram), new Listener<Long>() {
50 public void execute(Long result) {
55 public void exception(Throwable t) {
56 LOGGER.error("Could not listen interval from runtime diagram {}", runtimeDiagram, t);
60 public boolean isDisposed() {
61 return resultListener.isDisposed();
64 return super.getStyleCalculationRequest(runtimeDiagram, entry, item);
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;
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();
83 // lastCalculateTimestamp = currentTimestamp;
85 return Optional.ofNullable(calculateThrottledStyle(graph, runtimeDiagram, entry, groupItem));
88 public abstract Result calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException;
91 public final void applyStyleForNode(EvaluationContext evaluationContext, INode node, Optional<Result> result) {
92 if (!result.equals(Optional.empty())) {
93 applyThrottledStyleForNode(evaluationContext, node, result.get());
95 LOGGER.debug("Do not apply as results are unchanged for {} {} {}", evaluationContext, node, result);
99 public abstract void applyThrottledStyleForNode(EvaluationContext evaluationContext, INode node, Result result);
101 private static class ProfileUpdateIntervalRead extends UnaryRead<Resource, Long> {
103 public ProfileUpdateIntervalRead(Resource parameter) {
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);
115 return interval != null ? interval : DEFAULT_INTERVAL;