import org.simantics.db.Resource;
import org.simantics.db.common.request.ResourceRead;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
import org.simantics.diagram.stubs.DiagramResource;
import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.scl.runtime.function.Function1;
/**
* @author Tuukka Lehtonen
*/
public class DiagramSettingsRequest extends ResourceRead<DiagramSettings> {
- protected DiagramSettingsRequest(Resource runtimeDiagram) {
+ public DiagramSettingsRequest(Resource runtimeDiagram) {
super(runtimeDiagram);
}
- @Override
+ @SuppressWarnings("unchecked")
+ @Override
public DiagramSettings perform(ReadGraph graph) throws DatabaseException {
DiagramResource DIA = DiagramResource.getInstance(graph);
DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
- Resource edgeThicknessProperty = null;
- Resource nodeScalingProperty = null;
- double edgeThicknessScale = 1;
- double nodeScalingScale = 1;
+ Function1<Resource, Double> elementColoringFunction = null;
+ float elementColoringGradientHue = 0;
+ float elementColoringGradientSaturation = 1;
+ Function1<Resource, Double> edgeThicknessProperty = null;
+ Function1<Resource, Double> nodeScaleProperty = null;
+ double edgeThicknessGain = 1;
+ double edgeThicknessBias = 0;
+ double nodeScaleGain = 1;
+ double nodeScaleBias = 0;
Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
if (diagram != null) {
+ Variable dv = Variables.getPossibleVariable(graph, diagram);
+ if (dv != null) {
+ Object obj = dv.getPossiblePropertyValue(graph, DN.Diagram_elementColoringFunction);
+ if (obj instanceof Function1<?,?>) {
+ elementColoringFunction = (Function1<Resource, Double>) obj;
+ }
+ }
+ elementColoringGradientHue =
+ limit(0, 360, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientHue, 0.0f))
+ / 360.0f;
+ elementColoringGradientSaturation =
+ limit(0, 100, safeFloatProperty(graph, diagram, DN.Diagram_elementColoringGradientSaturation, 0.0f))
+ / 100.0f;
+
Resource etp = graph.getPossibleObject(diagram, DN.Diagram_edgeThicknessProperty);
- edgeThicknessProperty = graph.getPossibleObject(etp, DN.Edge_ThicknessProperty_value);
-// Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScalingProperty);
-// nodeScalingProperty = graph.getPossibleObject(nsp, DN.Vertex_ScaleProperty_value);
+ //System.out.println("etp: " + NameUtils.getURIOrSafeNameInternal(graph, etp));
+ if (etp != null) {
+ Variable etpv = Variables.getPossibleVariable(graph, etp);
+ if (etpv != null) {
+ //System.out.println("etpv: " + etpv.getURI(graph));
+ edgeThicknessProperty = etpv.getPropertyValue(graph, DN.Edge_ThicknessProperty_value);
+ }
- edgeThicknessScale =
- safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_scale, 1)
- * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessScale, 1);
+ edgeThicknessGain =
+ safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_gain, 1)
+ * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessGain, 1);
+ edgeThicknessBias =
+ safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_bias, 0)
+ + safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessBias, 0);
+ }
+ Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScaleProperty);
+ if (nsp != null) {
+ Variable nspv = Variables.getPossibleVariable(graph, nsp);
+ if (nspv != null) {
+ //System.out.println("nspv: " + nspv.getURI(graph));
+ nodeScaleProperty = nspv.getPropertyValue(graph, DN.Vertex_ScaleProperty_value);
+ }
-// nodeScalingScale =
-// safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_scale, 1)
-// * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScale, 1);
+ nodeScaleGain =
+ safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_gain, 1)
+ * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleGain, 1);
+ nodeScaleBias =
+ safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_bias, 0)
+ + safeDoubleProperty(graph, diagram, DN.Diagram_nodeScaleBias, 0);
+ }
}
- return new DiagramSettings(nodeScalingProperty, nodeScalingScale, edgeThicknessProperty, edgeThicknessScale);
+ DiagramSettings ds = new DiagramSettings(
+ nodeScaleProperty, nodeScaleGain, nodeScaleBias,
+ edgeThicknessProperty, edgeThicknessGain, edgeThicknessBias,
+ elementColoringFunction,
+ elementColoringGradientHue,
+ elementColoringGradientSaturation);
+ //System.out.println("new diagram settings: " + ds);
+ return ds;
+ }
+
+ private static float safeFloatProperty(ReadGraph graph, Resource r, Resource property, float defaultValue) throws DatabaseException {
+ Float d = graph.getPossibleRelatedValue(r, property, Bindings.FLOAT);
+ return d != null ? d : defaultValue;
}
private static double safeDoubleProperty(ReadGraph graph, Resource r, Resource property, double defaultValue) throws DatabaseException {
return d != null ? d : defaultValue;
}
+ private static float limit(float min, float max, float value) {
+ return Math.max(min, Math.min(value, max));
+ }
+
+ @SuppressWarnings("unused")
+ private static double limit(double min, double max, double value) {
+ return Math.max(min, Math.min(value, max));
+ }
+
}