]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DiagramSettingsRequest.java
Merge remote-tracking branch 'origin/master' into release/1.35.1
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / DiagramSettingsRequest.java
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> nodeScaleProperty = null;
34                 double edgeThicknessGain = 1;
35                 double edgeThicknessBias = 0;
36                 double nodeScaleGain = 1;
37                 double nodeScaleBias = 0;
38
39                 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
40                 if (diagram != null) {
41                         Variable dv = Variables.getPossibleVariable(graph, diagram);
42                         if (dv != null) {
43                                 Object obj = dv.getPossiblePropertyValue(graph, DN.Diagram_elementColoringFunction);
44                                 if (obj instanceof Function1<?,?>) {
45                                         elementColoringFunction = (Function1<Resource, Double>) obj;
46                                 }
47                         }
48                         elementColoringGradientHue =
49                                         limit(0, 360, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientHue, 0.0f))
50                                         / 360.0f;
51                         elementColoringGradientSaturation =
52                                         limit(0, 100, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientSaturation, 0.0f))
53                                         / 100.0f;
54
55                         Resource etp = graph.getPossibleObject(diagram, DN.Diagram_edgeThicknessProperty);
56                         //System.out.println("etp: " + NameUtils.getURIOrSafeNameInternal(graph, etp));
57                         if (etp != null) {
58                                 Variable etpv = Variables.getPossibleVariable(graph, etp);
59                                 if (etpv != null) {
60                                         //System.out.println("etpv: " + etpv.getURI(graph));
61                                         edgeThicknessProperty = etpv.getPropertyValue(graph, DN.Edge_ThicknessProperty_value);
62                                 }
63
64                                 edgeThicknessGain =
65                                                 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_gain, 1)
66                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessGain, 1);
67                                 edgeThicknessBias =
68                                                 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_bias, 0)
69                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessBias, 0);
70                         }
71                         Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScaleProperty);
72                         if (nsp != null) {
73                                 Variable nspv = Variables.getPossibleVariable(graph, nsp);
74                                 if (nspv != null) {
75                                         //System.out.println("nspv: " + nspv.getURI(graph));
76                                         nodeScaleProperty = nspv.getPropertyValue(graph, DN.Vertex_ScaleProperty_value);
77                                 }
78
79                                 nodeScaleGain =
80                                                 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_gain, 1)
81                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleGain, 1);
82                                 nodeScaleBias =
83                                                 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_bias, 0)
84                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleBias, 0);
85                         }
86                 }
87
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);
95                 return ds;
96         }
97
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;
101         }
102
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;
106         }
107
108         private static float limit(float min, float max, float value) {
109                 return Math.max(min, Math.min(value,  max));
110         }
111
112         @SuppressWarnings("unused")
113         private static double limit(double min, double max, double value) {
114                 return Math.max(min, Math.min(value,  max));
115         }
116
117 }