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> arrowLengthProperty = null;
30 double arrowLengthGain = 1;
31 double arrowLengthBias = 0;
33 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
34 if (diagram != null) {
35 Variable dv = Variables.getPossibleVariable(graph, diagram);
37 Resource alp = graph.getPossibleObject(diagram, DN.Diagram_arrowLengthProperty);
38 //System.out.println("alp: " + NameUtils.getURIOrSafeNameInternal(graph, alp));
40 Variable alpv = Variables.getPossibleVariable(graph, alp);
42 //System.out.println("alpv: " + alpv.getURI(graph));
43 arrowLengthProperty = alpv.getPropertyValue(graph, DN.Edge_ArrowLengthProperty_value);
47 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_gain, 1)
48 * safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthGain, 1);
50 safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_bias, 0)
51 + safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthBias, 0);
56 DiagramSettings ds = new DiagramSettings( arrowLengthProperty, arrowLengthGain, arrowLengthBias);
57 //System.out.println("new diagram settings: " + ds);
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;
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;
71 private static float limit(float min, float max, float value) {
72 return Math.max(min, Math.min(value, max));
75 @SuppressWarnings("unused")
76 private static double limit(double min, double max, double value) {
77 return Math.max(min, Math.min(value, max));