1 package org.simantics.district.network.profile;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.db.layer0.variable.Variables;
13 import org.simantics.district.network.ontology.DistrictNetworkResource;
14 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
15 import org.simantics.district.network.visualisations.model.DynamicColorMap;
16 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.modeling.ModelingResources;
19 import org.simantics.scenegraph.INode;
20 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
21 import org.simantics.scenegraph.profile.EvaluationContext;
22 import org.simantics.scenegraph.profile.common.ProfileVariables;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * @author Tuukka Lehtonen
30 public class DNElementColorStyle extends ThrottledStyleBase<Color> {
32 private static final Logger LOGGER = LoggerFactory.getLogger(DNElementColorStyle.class);
34 private static final boolean DEBUG = false;
37 public Color calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
39 DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
40 TransientCacheAsyncListener.instance());
42 // Prevent PendingVariableExceptions from coming through
43 boolean wasSynchronous = graph.setSynchronous(true);
46 Layer0 L0 = Layer0.getInstance(graph);
47 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
48 ModelingResources MOD = ModelingResources.getInstance(graph);
49 Resource mapping = graph.getSingleObject(groupItem, DN.HasMapping);
51 Map<String, DynamicColorContribution> colorContributions = dv.getColorContributions();
52 String mappingName = graph.getRelatedValue(mapping, L0.HasName);
53 DynamicColorContribution dcc = colorContributions.get(mappingName);
54 if (dcc != null && dcc.isUsed()) {
56 Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent);
57 if (mappedComponent != null) {
59 Resource component = graph.getSingleObject(mappedComponent, MOD.ElementToComponent);
60 Variable variable = Variables.getVariable(graph, component);
61 Variable possibleActiveVariable = Variables.possibleActiveVariable(graph, variable);
62 if (possibleActiveVariable != null) {
64 Variable module = possibleActiveVariable.getPossibleChild(graph, dcc.getModuleName());
66 Variable attribute = module.getPossibleProperty(graph, dcc.getAttributeName());
67 if (attribute != null) {
68 Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
69 if (possibleValue != null) {
70 // here we do the adjusting according to spec in #15038
71 double adjustedValue = possibleValue.doubleValue() * dcc.getVariableGain() + dcc.getVariableBias();
72 DynamicColorMap defaultColorMap = dcc.getDefaultColorMap();
73 Color color = defaultColorMap.getColor(adjustedValue, dcc.getDefaultMin(), dcc.getDefaultMax());
76 LOGGER.warn("No value for {}", attribute.getURI(graph));
79 LOGGER.warn("Wrong attribute name {} for {} !!", dcc.getAttributeName(), module.getURI(graph));
82 LOGGER.warn("Wrong modulename {} for {} !!", dcc.getModuleName(), possibleActiveVariable.getURI(graph));
85 LOGGER.debug("No active experiment for {}", variable.getURI(graph));
88 LOGGER.debug("No mapped component for {} to calculate dynamic color style", groupItem);
94 graph.setSynchronous(wasSynchronous);
100 public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Color color) {
101 SingleElementNode n = (SingleElementNode) node;
102 for (INode nn : n.getNodes())
103 ProfileVariables.claimNodeProperty(nn, "dynamicColor", color, observer);
107 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
108 SingleElementNode n = (SingleElementNode) node;
109 for (INode nn : n.getNodes())
110 ProfileVariables.claimNodeProperty(nn, "dynamicColor", null, evaluationContext);