]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DNElementSizeStyle.java
Set license/copyright information for district features
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / DNElementSizeStyle.java
1 package org.simantics.district.network.profile;
2
3 import java.util.Map;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.db.layer0.variable.Variables;
12 import org.simantics.district.network.ontology.DistrictNetworkResource;
13 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions.DynamicColoringObject;
14 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions.DynamicSizingObject;
15 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
16 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
17 import org.simantics.district.network.visualisations.model.DynamicSizeMap;
18 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.ModelingResources;
21 import org.simantics.scenegraph.INode;
22 import org.simantics.scenegraph.g2d.G2DSceneGraph;
23 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
24 import org.simantics.scenegraph.profile.EvaluationContext;
25 import org.simantics.scenegraph.profile.common.ProfileVariables;
26 import org.simantics.scl.runtime.SCLContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * @author Jani Simomaa
32  */
33 public class DNElementSizeStyle extends ThrottledStyleBase<Double> {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(DNElementSizeStyle.class);
36     private static final boolean DEBUG = false;
37     
38     private static final Double PENDING = Double.NaN;
39     private static final Double ONE = 1.0;
40
41         @Override
42         public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
43             
44         DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
45                 TransientCacheAsyncListener.instance());
46
47         // Prevent PendingVariableExceptions from coming through
48         boolean wasSynchronous = graph.setSynchronous(true);
49         try {
50             if (dv != null) {
51                 Layer0 L0 = Layer0.getInstance(graph);
52                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
53                 ModelingResources MOD = ModelingResources.getInstance(graph);
54                 Resource mapping = graph.getSingleObject(groupItem, DN.HasMapping);
55             
56                 Map<String, DynamicSizeContribution> sizeContributions = dv.getSizeContributions();
57                 String mappingName = graph.getRelatedValue(mapping, L0.HasName);
58                 DynamicSizeContribution dsc = sizeContributions.get(mappingName);
59                 if (dsc != null && dsc.isUsed()) {
60     
61                     Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent);
62                     if (mappedComponent != null) {
63     
64                         Resource component = graph.getSingleObject(mappedComponent, MOD.ElementToComponent);
65                         Variable variable = Variables.getVariable(graph, component);
66                         Variable possibleActiveVariable = Variables.possibleActiveVariable(graph, variable);
67                         if (possibleActiveVariable != null) {
68     
69                             Variable module = possibleActiveVariable.getPossibleChild(graph, dsc.getModuleName());
70                             if (module != null) {
71                                 Variable attribute = module.getPossibleProperty(graph, dsc.getAttributeName());
72                                 if (attribute != null) {
73                                     Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
74                                     if (possibleValue != null) {
75                                         
76                                         
77                                         double minValue;
78                                         double maxValue;
79                                         if (dsc.isUseDefault()) {
80                                             DynamicSizingObject dynamicSizingObject = dv.getDefaultSizeContributions().get(mappingName);
81                                             
82                                             // This is required if ontology module needs to be compiled
83                                             Object currentGraph = SCLContext.getCurrent().get("graph");
84                                             try {
85                                                 SCLContext.getCurrent().put("graph", graph);
86                                                 
87                                                 DynamicSizeContribution ddcc = dynamicSizingObject.getSizeContributions().get(dsc.getLabel());
88                                                 minValue = ddcc.getDefaultMin();
89                                                 maxValue = ddcc.getDefaultMax();
90                                             } finally {
91                                                 SCLContext.getCurrent().put("graph", currentGraph);
92                                             }
93                                         } else {
94                                             minValue = dsc.getDefaultMin();
95                                             maxValue = dsc.getDefaultMax();
96                                         }
97                                         // here we do the adjusting according to spec in #15038
98                                         double adjustedValue = possibleValue.doubleValue() * dsc.getVariableGain() + dsc.getVariableBias();
99                                         DynamicSizeMap defaultSizeMap = dsc.getDefaultSizeMap();
100                                         double size = defaultSizeMap.getSize(adjustedValue, dv.getSizeBarOptions().isUseGradients(), minValue, maxValue);
101                                         return size;
102                                     } else {
103                                         LOGGER.warn("No value for {}", attribute.getURI(graph));
104                                     }
105                                 } else {
106                                     LOGGER.warn("Wrong attribute name {} for {} !!", dsc.getAttributeName(), module.getURI(graph));
107                                 }
108                             } else {
109                                 LOGGER.warn("Wrong modulename {} for {} !!", dsc.getModuleName(), possibleActiveVariable.getURI(graph));
110                             }
111                         } else {
112                             LOGGER.debug("No active experiment for {}", variable.getURI(graph));
113                         }
114                     } else {
115                         LOGGER.debug("No mapped component for {} to calculate dynamic size style", groupItem);
116                     }
117                 }
118             }
119                 }
120                 finally {
121                         graph.setSynchronous(wasSynchronous);
122                 }
123                 return 1.0;
124         }
125
126         @Override
127         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
128         //System.out.println("apply: " + node + " : " + value);
129         SingleElementNode n = (SingleElementNode) node;
130         if (value == PENDING) {
131             ((G2DSceneGraph)node.getRootNode()).setPending(node);
132         } else {
133             ((G2DSceneGraph)node.getRootNode()).clearPending(node);
134         }
135         if (value == null)
136             value = ONE;
137         for (INode nn : n.getNodes()) {
138             ProfileVariables.claimNodeProperty(nn, "size", value, observer);
139             ProfileVariables.claimNodeProperty(nn, "stroke", value, observer);
140         }
141         }
142
143         @Override
144         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
145         ((G2DSceneGraph)node.getRootNode()).clearPending(node);
146         SingleElementNode n = (SingleElementNode) node;
147         for (INode nn : n.getNodes()) {
148             ProfileVariables.claimNodeProperty(nn, "size", ONE, evaluationContext);
149             ProfileVariables.claimNodeProperty(nn, "stroke", ONE, evaluationContext);
150         }
151         }
152
153 }