1 package org.simantics.district.network.profile;
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;
27 * @author Jani Simomaa
29 public class DNElementSizeStyle extends ThrottledStyleBase<Double> {
31 private static final Logger LOGGER = LoggerFactory.getLogger(DNElementSizeStyle.class);
32 private static final boolean DEBUG = false;
34 private static final Double PENDING = Double.NaN;
35 private static final Double ONE = 1.0;
38 public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
40 DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
41 TransientCacheAsyncListener.instance());
43 // Prevent PendingVariableExceptions from coming through
44 boolean wasSynchronous = graph.setSynchronous(true);
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);
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()) {
57 Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent);
58 if (mappedComponent != null) {
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) {
65 Variable module = possibleActiveVariable.getPossibleChild(graph, dsc.getModuleName());
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());
77 LOGGER.warn("No value for {}", attribute.getURI(graph));
80 LOGGER.warn("Wrong attribute name {} for {} !!", dsc.getAttributeName(), module.getURI(graph));
83 LOGGER.warn("Wrong modulename {} for {} !!", dsc.getModuleName(), possibleActiveVariable.getURI(graph));
86 LOGGER.debug("No active experiment for {}", variable.getURI(graph));
89 LOGGER.debug("No mapped component for {} to calculate dynamic size style", groupItem);
95 graph.setSynchronous(wasSynchronous);
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);
107 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
111 for (INode nn : n.getNodes()) {
112 ProfileVariables.claimNodeProperty(nn, "size", value, observer);
113 ProfileVariables.claimNodeProperty(nn, "stroke", value, observer);
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);