]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DiagramSettingsRequest.java
DynamicVisualisations enhancements & deprecate old profiles & settings
[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> arrowLengthProperty = null;
30                 double arrowLengthGain = 1;
31                 double arrowLengthBias = 0;
32
33                 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
34                 if (diagram != null) {
35                         Variable dv = Variables.getPossibleVariable(graph, diagram);
36
37                         Resource alp = graph.getPossibleObject(diagram, DN.Diagram_arrowLengthProperty);
38                         //System.out.println("alp: " + NameUtils.getURIOrSafeNameInternal(graph, alp));
39                         if (alp != null) {
40                                 Variable alpv = Variables.getPossibleVariable(graph, alp);
41                                 if (alpv != null) {
42                                         //System.out.println("alpv: " + alpv.getURI(graph));
43                                         arrowLengthProperty = alpv.getPropertyValue(graph, DN.Edge_ArrowLengthProperty_value);
44                                 }
45
46                                 arrowLengthGain =
47                                                 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_gain, 1)
48                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthGain, 1);
49                                 arrowLengthBias =
50                                                 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_bias, 0)
51                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthBias, 0);
52                         }
53
54                 }
55
56                 DiagramSettings ds = new DiagramSettings( arrowLengthProperty, arrowLengthGain, arrowLengthBias);
57                 //System.out.println("new diagram settings: " + ds);
58                 return ds;
59         }
60
61         private static float safeFloatProperty(ReadGraph graph, Resource r, Resource property, float defaultValue) throws DatabaseException {
62                 Float d = graph.getPossibleRelatedValue(r, property, Bindings.FLOAT);
63                 return d != null ? d : defaultValue;
64         }
65
66         private static double safeDoubleProperty(ReadGraph graph, Resource r, Resource property, double defaultValue) throws DatabaseException {
67                 Double d = graph.getPossibleRelatedValue(r, property, Bindings.DOUBLE);
68                 return d != null ? d : defaultValue;
69         }
70
71         private static float limit(float min, float max, float value) {
72                 return Math.max(min, Math.min(value,  max));
73         }
74
75         @SuppressWarnings("unused")
76         private static double limit(double min, double max, double value) {
77                 return Math.max(min, Math.min(value,  max));
78         }
79
80 }