]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DNElementSizeStyle.java
DynamicVisualisations enhancements & deprecate old profiles & settings
[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.model.DynamicSizeContribution;
14 import org.simantics.district.network.visualisations.model.DynamicSizeMap;
15 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.modeling.ModelingResources;
18 import org.simantics.scenegraph.INode;
19 import org.simantics.scenegraph.g2d.G2DSceneGraph;
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;
25
26 /**
27  * @author Jani Simomaa
28  */
29 public class DNElementSizeStyle extends ThrottledStyleBase<Double> {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(DNElementSizeStyle.class);
32     private static final boolean DEBUG = false;
33     
34     private static final Double PENDING = Double.NaN;
35     private static final Double ONE = 1.0;
36
37         @Override
38         public Double 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         // Prevent PendingVariableExceptions from coming through
44         boolean wasSynchronous = graph.setSynchronous(true);
45         try {
46             if (dv != null) {
47                 Layer0 L0 = Layer0.getInstance(graph);
48                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
49                 ModelingResources MOD = ModelingResources.getInstance(graph);
50                 Resource mapping = graph.getSingleObject(groupItem, DN.HasMapping);
51             
52                 Map<String, DynamicSizeContribution> sizeContributions = dv.getSizeContributions();
53                 String mappingName = graph.getRelatedValue(mapping, L0.HasName);
54                 DynamicSizeContribution dsc = sizeContributions.get(mappingName);
55                 if (dsc != null && dsc.isUsed()) {
56     
57                     Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent);
58                     if (mappedComponent != null) {
59     
60                         Resource component = graph.getSingleObject(mappedComponent, MOD.ElementToComponent);
61                         Variable variable = Variables.getVariable(graph, component);
62                         Variable possibleActiveVariable = Variables.possibleActiveVariable(graph, variable);
63                         if (possibleActiveVariable != null) {
64     
65                             Variable module = possibleActiveVariable.getPossibleChild(graph, dsc.getModuleName());
66                             if (module != null) {
67                                 Variable attribute = module.getPossibleProperty(graph, dsc.getAttributeName());
68                                 if (attribute != null) {
69                                     Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
70                                     if (possibleValue != null) {
71                                         // here we do the adjusting according to spec in #15038
72                                         double adjustedValue = possibleValue.doubleValue() * dsc.getVariableGain() + dsc.getVariableBias();
73                                         DynamicSizeMap defaultSizeMap = dsc.getDefaultSizeMap();
74                                         double size = defaultSizeMap.getSize(adjustedValue, dsc.getDefaultMin(), dsc.getDefaultMax());
75                                         return size;
76                                     } else {
77                                         LOGGER.warn("No value for {}", attribute.getURI(graph));
78                                     }
79                                 } else {
80                                     LOGGER.warn("Wrong attribute name {} for {} !!", dsc.getAttributeName(), module.getURI(graph));
81                                 }
82                             } else {
83                                 LOGGER.warn("Wrong modulename {} for {} !!", dsc.getModuleName(), possibleActiveVariable.getURI(graph));
84                             }
85                         } else {
86                             LOGGER.debug("No active experiment for {}", variable.getURI(graph));
87                         }
88                     } else {
89                         LOGGER.debug("No mapped component for {} to calculate dynamic size style", groupItem);
90                     }
91                 }
92             }
93                 }
94                 finally {
95                         graph.setSynchronous(wasSynchronous);
96                 }
97                 return 1.0;
98         }
99
100         @Override
101         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
102         //System.out.println("apply: " + node + " : " + value);
103         SingleElementNode n = (SingleElementNode) node;
104         if (value == PENDING) {
105             ((G2DSceneGraph)node.getRootNode()).setPending(node);
106         } else {
107             ((G2DSceneGraph)node.getRootNode()).clearPending(node);
108         }
109         if (value == null)
110             value = ONE;
111         for (INode nn : n.getNodes()) {
112             ProfileVariables.claimNodeProperty(nn, "size", value, observer);
113             ProfileVariables.claimNodeProperty(nn, "stroke", value, observer);
114         }
115         }
116
117         @Override
118         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
119         ((G2DSceneGraph)node.getRootNode()).clearPending(node);
120         SingleElementNode n = (SingleElementNode) node;
121         for (INode nn : n.getNodes()) {
122             ProfileVariables.claimNodeProperty(nn, "size", ONE, evaluationContext);
123             ProfileVariables.claimNodeProperty(nn, "stroke", ONE, evaluationContext);
124         }
125         }
126
127 }