]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java
Set license/copyright information for district features
[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.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.DynamicVisualisationsContributions.DynamicColoringObject;
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.simantics.scl.runtime.SCLContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * @author Tuukka Lehtonen
30  * @since 1.35.0
31  */
32 public class DNElementColorStyle extends ThrottledStyleBase<Color> {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(DNElementColorStyle.class);
35
36     private static final boolean DEBUG = false;
37
38         @Override
39         public Color calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
40             
41         DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
42                 TransientCacheAsyncListener.instance());
43
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                                         
73                                         double minValue;
74                                         double maxValue;
75                                         if (dcc.isUseDefault()) {
76                                             DynamicColoringObject dynamicColoringObject = dv.getDefaultColorContributions().get(mappingName);
77                                             
78                                             // This is required if ontology module needs to be compiled
79                                             Object currentGraph = SCLContext.getCurrent().get("graph");
80                                             try {
81                                                 SCLContext.getCurrent().put("graph", graph);
82                                                 
83                                                 DynamicColorContribution ddcc = dynamicColoringObject.getColorContributions().get(dcc.getLabel());
84                                                 minValue = ddcc.getDefaultMin();
85                                                 maxValue = ddcc.getDefaultMax();
86                                             } finally {
87                                                 SCLContext.getCurrent().put("graph", currentGraph);
88                                             }
89                                         } else {
90                                             minValue = dcc.getDefaultMin();
91                                             maxValue = dcc.getDefaultMax();
92                                         }
93                                         // here we do the adjusting according to spec in #15038
94                                         double adjustedValue = possibleValue.doubleValue() * dcc.getVariableGain() + dcc.getVariableBias();
95                                         DynamicColorMap defaultColorMap = dcc.getDefaultColorMap();
96                                         Color color = defaultColorMap.getColor(adjustedValue, dv.getColorBarOptions().isUseGradients(), minValue, maxValue);
97                                         return color;
98                                     } else {
99                                         LOGGER.warn("No value for {}", attribute.getURI(graph));
100                                     }
101                                 } else {
102                                     LOGGER.warn("Wrong attribute name {} for {} !!", dcc.getAttributeName(), module.getURI(graph));
103                                 }
104                             } else {
105                                 LOGGER.warn("Wrong modulename {} for {} !!", dcc.getModuleName(), possibleActiveVariable.getURI(graph));
106                             }
107                         } else {
108                             LOGGER.debug("No active experiment for {}", variable.getURI(graph));
109                         }
110                     } else {
111                         LOGGER.debug("No mapped component for {} to calculate dynamic color style", groupItem);
112                     }
113                 }
114             }
115                 }
116                 finally {
117                         graph.setSynchronous(wasSynchronous);
118                 }
119                 return null;
120         }
121
122         @Override
123         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Color color) {
124                 SingleElementNode n = (SingleElementNode) node;
125                 for (INode nn : n.getNodes())
126                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", color, observer);
127         }
128
129         @Override
130         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
131                 SingleElementNode n = (SingleElementNode) node;
132                 for (INode nn : n.getNodes())
133                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", null, evaluationContext);
134         }
135
136 }