]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java
fe0a1fe7f2ebce8bbbfdec501fce0a0032b6e544
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / DNElementColorStyle.java
1 package org.simantics.district.network.profile;
2
3 import java.awt.Color;
4 import java.util.Map;
5
6 import org.simantics.Simantics;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.Variables;
14 import org.simantics.district.network.ontology.DistrictNetworkResource;
15 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
16 import org.simantics.district.network.visualisations.model.DynamicColorMap;
17 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
18 import org.simantics.layer0.Layer0;
19 import org.simantics.modeling.ModelingResources;
20 import org.simantics.scenegraph.INode;
21 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
22 import org.simantics.scenegraph.profile.EvaluationContext;
23 import org.simantics.scenegraph.profile.common.ProfileVariables;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * @author Tuukka Lehtonen
29  * @since 1.35.0
30  */
31 public class DNElementColorStyle extends ThrottledStyleBase<Color> {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(DNElementColorStyle.class);
34
35     private static final boolean DEBUG = false;
36
37         @Override
38         public Color calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
39             
40         DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
41                 TransientCacheAsyncListener.instance());
42
43         DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheAsyncListener.instance());
44         // Prevent PendingVariableExceptions from coming through
45         boolean wasSynchronous = graph.setSynchronous(true);
46         try {
47             if (dv != null) {
48                 Layer0 L0 = Layer0.getInstance(graph);
49                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
50                 ModelingResources MOD = ModelingResources.getInstance(graph);
51                 Resource mapping = graph.getSingleObject(groupItem, DN.HasMapping);
52             
53                 Map<String, DynamicColorContribution> colorContributions = dv.getColorContributions();
54                 String mappingName = graph.getRelatedValue(mapping, L0.HasName);
55                 DynamicColorContribution dcc = colorContributions.get(mappingName);
56                 if (dcc != null && dcc.isUsed()) {
57     
58                     Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent);
59                     if (mappedComponent != null) {
60     
61                         Resource component = graph.getSingleObject(mappedComponent, MOD.ElementToComponent);
62                         Variable variable = Variables.getVariable(graph, component);
63                         Variable possibleActiveVariable = Variables.possibleActiveVariable(graph, variable);
64                         if (possibleActiveVariable != null) {
65     
66                             Variable module = possibleActiveVariable.getPossibleChild(graph, dcc.getModuleName());
67                             if (module != null) {
68                                 Variable attribute = module.getPossibleProperty(graph, dcc.getAttributeName());
69                                 if (attribute != null) {
70                                     Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
71                                     if (possibleValue != null) {
72                                         // here we do the adjusting according to spec in #15038
73                                         double adjustedValue = possibleValue.doubleValue() * dcc.getVariableGain() + dcc.getVariableBias();
74                                         DynamicColorMap defaultColorMap = dcc.getDefaultColorMap();
75                                         Color color = defaultColorMap.getColor(adjustedValue, dcc.getDefaultMin(), dcc.getDefaultMax());
76                                         return color;
77                                     } else {
78                                         LOGGER.warn("No value for {}", attribute.getURI(graph));
79                                     }
80                                 } else {
81                                     LOGGER.warn("Wrong attribute name {} for {} !!", dcc.getAttributeName(), module.getURI(graph));
82                                 }
83                             } else {
84                                 LOGGER.warn("Wrong modulename {} for {} !!", dcc.getModuleName(), possibleActiveVariable.getURI(graph));
85                             }
86                         } else {
87                             LOGGER.debug("No active experiment for {}", variable.getURI(graph));
88                         }
89                     } else {
90                         LOGGER.debug("No mapped component for {} to calculate dynamic color style", groupItem);
91                     }
92                 }
93             }
94                     
95             // the old implementation here
96                         if (ds.elementColoringFunction.isPresent()) {
97                                 if (DEBUG)
98                                         System.out.print("elementColoringFunction: " + ds.elementColoringFunction + "(" + groupItem + "): ");
99                                 Double t = Simantics.applySCLRead(graph, ds.elementColoringFunction.get(), groupItem);
100                                 if (DEBUG)
101                                         System.out.print(t);
102                                 if (t != null) {
103                                         Color result = ds.coloringGradient.get(t);
104                                         //System.out.println("color(" + t + "): " + result);
105                                         return result;
106                                 }
107                         }
108                 }
109                 finally {
110                         graph.setSynchronous(wasSynchronous);
111                 }
112                 return null;
113         }
114
115         @Override
116         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Color color) {
117                 SingleElementNode n = (SingleElementNode) node;
118                 for (INode nn : n.getNodes())
119                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", color, observer);
120         }
121
122         @Override
123         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
124                 SingleElementNode n = (SingleElementNode) node;
125                 for (INode nn : n.getNodes())
126                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", null, evaluationContext);
127         }
128
129 }