+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object nodeScalePropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ Resource diagram = resolveElement(graph, context);
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ return baseMappingModifier(graph, diagram, DN.Diagram_nodeScaleProperty, DN.Vertex_ScaleProperty, context);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasDiameterValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, 0);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasNominalMassFlowValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasNominalMassFlow, 0);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasNominalSupplyPressure(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasSupplyPressure, 0);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasElevation(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, 0);
+ }
+
+ private static final Function1<Resource, Double> ONE = new FunctionImpl1<Resource, Double>() {
+ private final Double ONE = 1.0;
+ @Override
+ public Double apply(Resource edge) {
+ return ONE;
+ }
+ @Override
+ public String toString() {
+ return "1";
+ }
+ };
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return ONE;
+ }
+
+ private static Function1<Resource, Double> directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException {
+ Double def = defaultValue;
+ return new FunctionImpl1<Resource, Double>() {
+ @Override
+ public Double apply(Resource edge) {
+ ReadGraph graph = (ReadGraph) SCLContext.getCurrent().get("graph");
+ try {
+ Double d = graph.getPossibleRelatedValue(edge, property, Bindings.DOUBLE);
+ return d != null ? d : def;
+ } catch (DatabaseException e) {
+ LOGGER.error("Failed to evaluate property value", e);
+ return def;
+ }
+ }
+ };
+ }
+
+ private static class RangeValidator implements Function1<String, String> {
+ private double min;
+ private double max;
+ public RangeValidator(double min, double max) {
+ this.min = min;
+ this.max = max;
+ }
+ @Override
+ public String apply(String s) {
+ try {
+ double d = Double.parseDouble(s);
+ if (d < min)
+ return "Value must be greater than or equal to " + min;
+ if (d > max)
+ return "Value must be less than or equal to " + max;
+ return null;
+ } catch (NumberFormatException e) {
+ return "Specified value is not a number";
+ }
+ }
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object hueValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+ return new RangeValidator(0, 360);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object saturationValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+ return new RangeValidator(0, 100);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object brightnessValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+ String importEntry = null;
+ Resource root = Variables.getPossibleIndexRoot(graph, context);
+ if (root != null) {
+ Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain");
+ if (sclmain != null) {
+ importEntry = graph.getPossibleURI(sclmain);
+ }
+ }
+ SCLContext ctx = SCLContext.getCurrent();
+ Object oldGraph = ctx.put("graph", graph);
+ try {
+ return new BrightnessExpressionValidator(
+ importEntry != null
+ ? Arrays.asList(importEntry)
+ : Collections.emptyList());
+ } finally {
+ ctx.put("graph", oldGraph);
+ }
+ }
+
+ private static class BrightnessExpressionValidator implements Function1<String, String> {
+ private CommandSession session;
+
+ public BrightnessExpressionValidator(List<String> importEntries) {
+ this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT);
+ this.session.setImportEntries(imports(importEntries));
+ }
+
+ private ArrayList<CommandSessionImportEntry> imports(List<String> entries) {
+ ArrayList<CommandSessionImportEntry> result = new ArrayList<>();
+ entries.stream().map(CommandSessionImportEntry::new).forEach(result::add);
+ if (entries.isEmpty())
+ result.add(new CommandSessionImportEntry("Simantics/District/SCLMain"));
+ return result;
+ }
+
+ @Override
+ public String apply(String s) {
+ s = s.trim();
+ if (!s.startsWith("="))
+ return "Expression expected, must start with '='";
+ CompilationError[] errors = session.validate(s.substring(1));
+ if(errors.length == 0)
+ return null;
+ return errors[0].description;
+ }
+ }
+