]> gerrit.simantics Code Review - simantics/district.git/blob
2254b305706c6fb0d997fcbe9c65fba6384b4bfe
[simantics/district.git] /
1 package org.simantics.district.network.profile;
2
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;
13
14 /**
15  * @author Tuukka Lehtonen
16  */
17 public class DiagramSettingsRequest extends ResourceRead<DiagramSettings> {
18
19         public DiagramSettingsRequest(Resource runtimeDiagram) {
20                 super(runtimeDiagram);
21         }
22
23         @SuppressWarnings("unchecked")
24     @Override
25         public DiagramSettings perform(ReadGraph graph) throws DatabaseException {
26                 DiagramResource DIA = DiagramResource.getInstance(graph);
27                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
28
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;
41
42                 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
43                 if (diagram != null) {
44                         Variable dv = Variables.getPossibleVariable(graph, diagram);
45                         if (dv != null) {
46                                 Object obj = dv.getPossiblePropertyValue(graph, DN.Diagram_elementColoringFunction);
47                                 if (obj instanceof Function1<?,?>) {
48                                         elementColoringFunction = (Function1<Resource, Double>) obj;
49                                 }
50                         }
51                         elementColoringGradientHue =
52                                         limit(0, 360, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientHue, 0.0f))
53                                         / 360.0f;
54                         elementColoringGradientSaturation =
55                                         limit(0, 100, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientSaturation, 0.0f))
56                                         / 100.0f;
57
58                         Resource etp = graph.getPossibleObject(diagram, DN.Diagram_edgeThicknessProperty);
59                         //System.out.println("etp: " + NameUtils.getURIOrSafeNameInternal(graph, etp));
60                         if (etp != null) {
61                                 Variable etpv = Variables.getPossibleVariable(graph, etp);
62                                 if (etpv != null) {
63                                         //System.out.println("etpv: " + etpv.getURI(graph));
64                                         edgeThicknessProperty = etpv.getPropertyValue(graph, DN.Edge_ThicknessProperty_value);
65                                 }
66
67                                 edgeThicknessGain =
68                                                 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_gain, 1)
69                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessGain, 1);
70                                 edgeThicknessBias =
71                                                 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_bias, 0)
72                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessBias, 0);
73                         }
74                         Resource alp = graph.getPossibleObject(diagram, DN.Diagram_arrowLengthProperty);
75                         //System.out.println("alp: " + NameUtils.getURIOrSafeNameInternal(graph, alp));
76                         if (alp != null) {
77                                 Variable alpv = Variables.getPossibleVariable(graph, alp);
78                                 if (alpv != null) {
79                                         //System.out.println("alpv: " + alpv.getURI(graph));
80                                         arrowLengthProperty = alpv.getPropertyValue(graph, DN.Edge_ArrowLengthProperty_value);
81                                 }
82
83                                 arrowLengthGain =
84                                                 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_gain, 1)
85                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthGain, 1);
86                                 arrowLengthBias =
87                                                 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_bias, 0)
88                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthBias, 0);
89                         }
90                         Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScaleProperty);
91                         if (nsp != null) {
92                                 Variable nspv = Variables.getPossibleVariable(graph, nsp);
93                                 if (nspv != null) {
94                                         //System.out.println("nspv: " + nspv.getURI(graph));
95                                         nodeScaleProperty = nspv.getPropertyValue(graph, DN.Vertex_ScaleProperty_value);
96                                 }
97
98                                 nodeScaleGain =
99                                                 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_gain, 1)
100                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleGain, 1);
101                                 nodeScaleBias =
102                                                 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_bias, 0)
103                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleBias, 0);
104                         }
105                 }
106
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);
115                 return ds;
116         }
117
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;
121         }
122
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;
126         }
127
128         private static float limit(float min, float max, float value) {
129                 return Math.max(min, Math.min(value,  max));
130         }
131
132         @SuppressWarnings("unused")
133         private static double limit(double min, double max, double value) {
134                 return Math.max(min, Math.min(value,  max));
135         }
136
137 }