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> arrowLengthProperty = null;
34 Function1<Resource, Double> nodeScaleProperty = null;
35 double edgeThicknessGain = 1;
36 double edgeThicknessBias = 0;
37 double arrowLengthGain = 1;
38 double arrowLengthBias = 0;
39 double nodeScaleGain = 1;
40 double nodeScaleBias = 0;
42 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
43 if (diagram != null) {
44 Variable dv = Variables.getPossibleVariable(graph, diagram);
46 Object obj = dv.getPossiblePropertyValue(graph, DN.Diagram_elementColoringFunction);
47 if (obj instanceof Function1<?,?>) {
48 elementColoringFunction = (Function1<Resource, Double>) obj;
51 elementColoringGradientHue =
52 limit(0, 360, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientHue, 0.0f))
54 elementColoringGradientSaturation =
55 limit(0, 100, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientSaturation, 0.0f))
58 Resource etp = graph.getPossibleObject(diagram, DN.Diagram_edgeThicknessProperty);
59 //System.out.println("etp: " + NameUtils.getURIOrSafeNameInternal(graph, etp));
61 Variable etpv = Variables.getPossibleVariable(graph, etp);
63 //System.out.println("etpv: " + etpv.getURI(graph));
64 edgeThicknessProperty = etpv.getPropertyValue(graph, DN.Edge_ThicknessProperty_value);
68 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_gain, 1)
69 * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessGain, 1);
71 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_bias, 0)
72 + safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessBias, 0);
74 Resource alp = graph.getPossibleObject(diagram, DN.Diagram_arrowLengthProperty);
75 //System.out.println("alp: " + NameUtils.getURIOrSafeNameInternal(graph, alp));
77 Variable alpv = Variables.getPossibleVariable(graph, alp);
79 //System.out.println("alpv: " + alpv.getURI(graph));
80 arrowLengthProperty = alpv.getPropertyValue(graph, DN.Edge_ArrowLengthProperty_value);
84 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_gain, 1)
85 * safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthGain, 1);
87 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_bias, 0)
88 + safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthBias, 0);
90 Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScaleProperty);
92 Variable nspv = Variables.getPossibleVariable(graph, nsp);
94 //System.out.println("nspv: " + nspv.getURI(graph));
95 nodeScaleProperty = nspv.getPropertyValue(graph, DN.Vertex_ScaleProperty_value);
99 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_gain, 1)
100 * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleGain, 1);
102 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_bias, 0)
103 + safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleBias, 0);
107 DiagramSettings ds = new DiagramSettings(
108 nodeScaleProperty, nodeScaleGain, nodeScaleBias,
109 edgeThicknessProperty, edgeThicknessGain, edgeThicknessBias,
110 arrowLengthProperty, arrowLengthGain, arrowLengthBias,
111 elementColoringFunction,
112 elementColoringGradientHue,
113 elementColoringGradientSaturation);
114 //System.out.println("new diagram settings: " + ds);
118 private static float safeFloatProperty(ReadGraph graph, Resource r, Resource property, float defaultValue) throws DatabaseException {
119 Float d = graph.getPossibleRelatedValue(r, property, Bindings.FLOAT);
120 return d != null ? d : defaultValue;
123 private static double safeDoubleProperty(ReadGraph graph, Resource r, Resource property, double defaultValue) throws DatabaseException {
124 Double d = graph.getPossibleRelatedValue(r, property, Bindings.DOUBLE);
125 return d != null ? d : defaultValue;
128 private static float limit(float min, float max, float value) {
129 return Math.max(min, Math.min(value, max));
132 @SuppressWarnings("unused")
133 private static double limit(double min, double max, double value) {
134 return Math.max(min, Math.min(value, max));