+ 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);
+ }
+ }
+ return new BrightnessExpressionValidator(
+ importEntry != null
+ ? Arrays.asList(importEntry)
+ : Collections.emptyList());
+ }
+
+ 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;
+ }
+ }
+