1 package org.simantics.district.network.profile;
3 import org.simantics.databoard.Bindings;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.request.ResourceRead;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.variable.Variable;
9 import org.simantics.db.layer0.variable.Variables;
10 import org.simantics.diagram.stubs.DiagramResource;
11 import org.simantics.district.network.ontology.DistrictNetworkResource;
12 import org.simantics.scl.runtime.function.Function1;
15 * @author Tuukka Lehtonen
17 public class DiagramSettingsRequest extends ResourceRead<DiagramSettings> {
19 public DiagramSettingsRequest(Resource runtimeDiagram) {
20 super(runtimeDiagram);
23 @SuppressWarnings("unchecked")
25 public DiagramSettings perform(ReadGraph graph) throws DatabaseException {
26 DiagramResource DIA = DiagramResource.getInstance(graph);
27 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
29 Function1<Resource, Double> elementColoringFunction = null;
30 float elementColoringGradientHue = 0;
31 float elementColoringGradientSaturation = 1;
32 Function1<Resource, Double> edgeThicknessProperty = null;
33 Function1<Resource, Double> nodeScaleProperty = null;
34 double edgeThicknessGain = 1;
35 double edgeThicknessBias = 0;
36 double nodeScaleGain = 1;
37 double nodeScaleBias = 0;
39 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
40 if (diagram != null) {
41 Variable dv = Variables.getPossibleVariable(graph, diagram);
43 Object obj = dv.getPossiblePropertyValue(graph, DN.Diagram_elementColoringFunction);
44 if (obj instanceof Function1<?,?>) {
45 elementColoringFunction = (Function1<Resource, Double>) obj;
48 elementColoringGradientHue =
49 limit(0, 360, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientHue, 0.0f))
51 elementColoringGradientSaturation =
52 limit(0, 100, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientSaturation, 0.0f))
55 Resource etp = graph.getPossibleObject(diagram, DN.Diagram_edgeThicknessProperty);
56 //System.out.println("etp: " + NameUtils.getURIOrSafeNameInternal(graph, etp));
58 Variable etpv = Variables.getPossibleVariable(graph, etp);
60 //System.out.println("etpv: " + etpv.getURI(graph));
61 edgeThicknessProperty = etpv.getPropertyValue(graph, DN.Edge_ThicknessProperty_value);
65 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_gain, 1)
66 * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessGain, 1);
68 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_bias, 0)
69 + safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessBias, 0);
71 Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScaleProperty);
73 Variable nspv = Variables.getPossibleVariable(graph, nsp);
75 //System.out.println("nspv: " + nspv.getURI(graph));
76 nodeScaleProperty = nspv.getPropertyValue(graph, DN.Vertex_ScaleProperty_value);
80 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_gain, 1)
81 * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleGain, 1);
83 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_bias, 0)
84 + safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleBias, 0);
88 DiagramSettings ds = new DiagramSettings(
89 nodeScaleProperty, nodeScaleGain, nodeScaleBias,
90 edgeThicknessProperty, edgeThicknessGain, edgeThicknessBias,
91 elementColoringFunction,
92 elementColoringGradientHue,
93 elementColoringGradientSaturation);
94 //System.out.println("new diagram settings: " + ds);
98 private static float safeFloatProperty(ReadGraph graph, Resource r, Resource property, float defaultValue) throws DatabaseException {
99 Float d = graph.getPossibleRelatedValue(r, property, Bindings.FLOAT);
100 return d != null ? d : defaultValue;
103 private static double safeDoubleProperty(ReadGraph graph, Resource r, Resource property, double defaultValue) throws DatabaseException {
104 Double d = graph.getPossibleRelatedValue(r, property, Bindings.DOUBLE);
105 return d != null ? d : defaultValue;
108 private static float limit(float min, float max, float value) {
109 return Math.max(min, Math.min(value, max));
112 @SuppressWarnings("unused")
113 private static double limit(double min, double max, double value) {
114 return Math.max(min, Math.min(value, max));