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.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;
31 * @author Jani Simomaa
33 public class DNElementSizeStyle extends ThrottledStyleBase<Double> {
35 private static final Logger LOGGER = LoggerFactory.getLogger(DNElementSizeStyle.class);
36 private static final boolean DEBUG = false;
38 private static final Double PENDING = Double.NaN;
39 private static final Double ONE = 1.0;
42 public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
44 DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
45 TransientCacheAsyncListener.instance());
47 // Prevent PendingVariableExceptions from coming through
48 boolean wasSynchronous = graph.setSynchronous(true);
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);
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()) {
61 Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent);
62 if (mappedComponent != null) {
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) {
69 Variable module = possibleActiveVariable.getPossibleChild(graph, dsc.getModuleName());
71 Variable attribute = module.getPossibleProperty(graph, dsc.getAttributeName());
72 if (attribute != null) {
73 Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
74 if (possibleValue != null) {
79 if (dsc.isUseDefault()) {
80 DynamicSizingObject dynamicSizingObject = dv.getDefaultSizeContributions().get(mappingName);
82 // This is required if ontology module needs to be compiled
83 Object currentGraph = SCLContext.getCurrent().get("graph");
85 SCLContext.getCurrent().put("graph", graph);
87 DynamicSizeContribution ddcc = dynamicSizingObject.getSizeContributions().get(dsc.getLabel());
88 minValue = ddcc.getDefaultMin();
89 maxValue = ddcc.getDefaultMax();
91 SCLContext.getCurrent().put("graph", currentGraph);
94 minValue = dsc.getDefaultMin();
95 maxValue = dsc.getDefaultMax();
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);
103 LOGGER.warn("No value for {}", attribute.getURI(graph));
106 LOGGER.warn("Wrong attribute name {} for {} !!", dsc.getAttributeName(), module.getURI(graph));
109 LOGGER.warn("Wrong modulename {} for {} !!", dsc.getModuleName(), possibleActiveVariable.getURI(graph));
112 LOGGER.debug("No active experiment for {}", variable.getURI(graph));
115 LOGGER.debug("No mapped component for {} to calculate dynamic size style", groupItem);
121 graph.setSynchronous(wasSynchronous);
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);
133 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
137 for (INode nn : n.getNodes()) {
138 ProfileVariables.claimNodeProperty(nn, "size", value, observer);
139 ProfileVariables.claimNodeProperty(nn, "stroke", value, observer);
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);