1 package org.simantics.district.network.profile;
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;
29 public class ArrowLengthStyle extends ThrottledStyleBase<Double> {
31 private static final Logger LOGGER = LoggerFactory.getLogger(ArrowLengthStyle.class);
32 private static final Double PENDING = Double.NaN;
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))
41 DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
42 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, DynamicArrowContribution> arrowContributions = dv.getArrowContributions();
53 String mappingName = graph.getRelatedValue(mapping, L0.HasName);
54 DynamicArrowContribution dac = arrowContributions.get(mappingName);
55 if (dac != null && dac.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, dac.getModuleName());
67 Variable attribute = module.getPossibleProperty(graph, dac.getAttributeName());
68 if (attribute != null) {
69 Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
70 if (possibleValue != null) {
74 if (dac.isUseDefault()) {
75 DynamicArrowObject dynamicArrowObject = dv.getDefaultArrowContributions().get(mappingName);
77 // This is required if ontology module needs to be compiled
78 Object currentGraph = SCLContext.getCurrent().get("graph");
80 SCLContext.getCurrent().put("graph", graph);
82 DynamicArrowContribution ddcc = dynamicArrowObject.getArrowContributions().get(dac.getLabel());
83 biasValue = ddcc.getDefaultBias();
84 gainValue = ddcc.getDefaultGain();
86 SCLContext.getCurrent().put("graph", currentGraph);
89 biasValue = dac.getDefaultBias();
90 gainValue = dac.getDefaultGain();
92 // here we do the adjusting according to spec in #15038
93 return possibleValue.doubleValue() * gainValue + biasValue;
95 LOGGER.warn("No value for {}", attribute.getURI(graph));
98 LOGGER.warn("Wrong attribute name {} for {} !!", dac.getAttributeName(), module.getURI(graph));
101 LOGGER.warn("Wrong modulename {} for {} !!", dac.getModuleName(), possibleActiveVariable.getURI(graph));
104 LOGGER.debug("No active experiment for {}", variable.getURI(graph));
107 LOGGER.debug("No mapped component for {} to calculate dynamic color style", groupItem);
112 graph.setSynchronous(wasSynchronous);
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);
124 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
126 for (INode nn : n.getNodes())
127 ProfileVariables.claimNodeProperty(nn, "arrowLength", value, observer);
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);