]> gerrit.simantics Code Review - simantics/district.git/blob
6fdb9721f059b20d32498e75f0f423659ebfa5be
[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         @Override
24         public DiagramSettings perform(ReadGraph graph) throws DatabaseException {
25                 DiagramResource DIA = DiagramResource.getInstance(graph);
26                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
27
28                 Function1<Resource, Double> edgeThicknessProperty = null;
29                 Function1<Resource, Double> nodeScaleProperty = null;
30                 double edgeThicknessGain = 1;
31                 double edgeThicknessBias = 0;
32                 double nodeScaleGain = 1;
33                 double nodeScaleBias = 0;
34
35                 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
36                 if (diagram != null) {
37                         Resource etp = graph.getPossibleObject(diagram, DN.Diagram_edgeThicknessProperty);
38                         //System.out.println("etp: " + NameUtils.getURIOrSafeNameInternal(graph, etp));
39                         if (etp != null) {
40                                 Variable etpv = Variables.getPossibleVariable(graph, etp);
41                                 if (etpv != null) {
42                                         //System.out.println("etpv: " + etpv.getURI(graph));
43                                         edgeThicknessProperty = etpv.getPropertyValue(graph, DN.Edge_ThicknessProperty_value);
44                                 }
45
46                                 edgeThicknessGain =
47                                                 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_gain, 1)
48                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessGain, 1);
49                                 edgeThicknessBias =
50                                                 safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_bias, 0)
51                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessBias, 0);
52                         }
53                         Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScaleProperty);
54                         if (nsp != null) {
55                                 Variable nspv = Variables.getPossibleVariable(graph, nsp);
56                                 if (nspv != null) {
57                                         //System.out.println("nspv: " + nspv.getURI(graph));
58                                         nodeScaleProperty = nspv.getPropertyValue(graph, DN.Vertex_ScaleProperty_value);
59                                 }
60
61                                 nodeScaleGain =
62                                                 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_gain, 1)
63                                                 * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleGain, 1);
64                                 nodeScaleBias =
65                                                 safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_bias, 0)
66                                                 + safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleBias, 0);
67                         }
68                 }
69
70                 DiagramSettings ds = new DiagramSettings(
71                                 nodeScaleProperty, nodeScaleGain, nodeScaleBias,
72                                 edgeThicknessProperty, edgeThicknessGain, edgeThicknessBias);
73                 //System.out.println("new diagram settings: " + ds);
74                 return ds;
75         }
76
77         private static double safeDoubleProperty(ReadGraph graph, Resource r, Resource property, double defaultValue) throws DatabaseException {
78                 Double d = graph.getPossibleRelatedValue(r, property, Bindings.DOUBLE);
79                 return d != null ? d : defaultValue;
80         }
81
82 }