]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/ArrowLengthStyle.java
Dynamic Visualisations view improvements & profile refactoring
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / ArrowLengthStyle.java
1 package org.simantics.district.network.profile;
2
3 import java.util.Map;
4 import java.util.Set;
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.common.procedure.adapter.TransientCacheListener;
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.DynamicVisualisationsContributions.DynamicArrowObject;
16 import org.simantics.district.network.visualisations.model.DynamicArrowContribution;
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.G2DSceneGraph;
22 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
23 import org.simantics.scenegraph.profile.EvaluationContext;
24 import org.simantics.scenegraph.profile.common.ProfileVariables;
25 import org.simantics.scl.runtime.SCLContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class ArrowLengthStyle extends ThrottledStyleBase<Double> {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(ArrowLengthStyle.class);
32         private static final Double PENDING = Double.NaN;
33
34         @Override
35         public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
36                 Resource diagram = graph.getSingleObject(groupItem, Layer0.getInstance(graph).PartOf);
37                 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
38                 if (!edgesToUse.contains(groupItem))
39                         return null;
40                 
41         DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
42                 TransientCacheAsyncListener.instance());
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, DynamicArrowContribution> arrowContributions = dv.getArrowContributions();
53                 String mappingName = graph.getRelatedValue(mapping, L0.HasName);
54                 DynamicArrowContribution dac = arrowContributions.get(mappingName);
55                 if (dac != null && dac.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, dac.getModuleName());
66                             if (module != null) {
67                                 Variable attribute = module.getPossibleProperty(graph, dac.getAttributeName());
68                                 if (attribute != null) {
69                                     Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
70                                     if (possibleValue != null) {
71                                         
72                                         double biasValue;
73                                         double gainValue;
74                                         if (dac.isUseDefault()) {
75                                             DynamicArrowObject dynamicArrowObject = dv.getDefaultArrowContributions().get(mappingName);
76                                             
77                                             // This is required if ontology module needs to be compiled
78                                             Object currentGraph = SCLContext.getCurrent().get("graph");
79                                             try {
80                                                 SCLContext.getCurrent().put("graph", graph);
81                                                 
82                                                 DynamicArrowContribution ddcc = dynamicArrowObject.getArrowContributions().get(dac.getLabel());
83                                                 biasValue = ddcc.getDefaultBias();
84                                                 gainValue = ddcc.getDefaultGain();
85                                             } finally {
86                                                 SCLContext.getCurrent().put("graph", currentGraph);
87                                             }
88                                         } else {
89                                             biasValue = dac.getDefaultBias();
90                                             gainValue = dac.getDefaultGain();
91                                         }
92                                         // here we do the adjusting according to spec in #15038
93                                         return possibleValue.doubleValue() * gainValue + biasValue;
94                                     } else {
95                                         LOGGER.warn("No value for {}", attribute.getURI(graph));
96                                     }
97                                 } else {
98                                     LOGGER.warn("Wrong attribute name {} for {} !!", dac.getAttributeName(), module.getURI(graph));
99                                 }
100                             } else {
101                                 LOGGER.warn("Wrong modulename {} for {} !!", dac.getModuleName(), possibleActiveVariable.getURI(graph));
102                             }
103                         } else {
104                             LOGGER.debug("No active experiment for {}", variable.getURI(graph));
105                         }
106                     } else {
107                         LOGGER.debug("No mapped component for {} to calculate dynamic color style", groupItem);
108                     }
109                 }
110             }
111         } finally {
112             graph.setSynchronous(wasSynchronous);
113         }
114         return null;
115     }
116
117         @Override
118         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
119 //              System.out.println("apply: " + node + " : " + value);
120                 ConnectionNode n = (ConnectionNode) node;
121                 if (value == PENDING) {
122                         ((G2DSceneGraph)node.getRootNode()).setPending(node);
123                 } else {
124                         ((G2DSceneGraph)node.getRootNode()).clearPending(node);
125                 }
126                 for (INode nn : n.getNodes())
127                         ProfileVariables.claimNodeProperty(nn, "arrowLength", value, observer);
128         }
129
130         @Override
131         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
132                 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
133                 ConnectionNode n = (ConnectionNode) node;
134                 for (INode nn : n.getNodes())
135                         ProfileVariables.claimNodeProperty(nn, "arrowLength", null, evaluationContext);
136         }
137 }