X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ffunction%2FFunctions.java;h=3aa6f2752cc04363acf417762d1e1ab680ea3ac7;hb=refs%2Fchanges%2F41%2F2341%2F1;hp=04cd94cf46658f2d3c0bbb8431275d06f8640ad0;hpb=3354c578fb8e65421d0fdb123310232ccdc597cf;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java index 04cd94cf..3aa6f275 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java @@ -1,5 +1,7 @@ package org.simantics.district.network.ui.function; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -42,6 +44,7 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.RuntimeDatabaseException; import org.simantics.db.exception.ServiceException; import org.simantics.db.layer0.QueryIndexUtils; +import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.db.layer0.variable.Variables.Role; @@ -52,10 +55,15 @@ import org.simantics.modeling.ModelingResources; import org.simantics.modeling.adapters.NewCompositeActionFactory; import org.simantics.modeling.typicals.TypicalUtil; import org.simantics.operation.Layer0X; +import org.simantics.scl.compiler.commands.CommandSession; +import org.simantics.scl.compiler.commands.CommandSessionImportEntry; +import org.simantics.scl.compiler.errors.CompilationError; +import org.simantics.scl.osgi.SCLOsgi; import org.simantics.scl.reflection.annotations.SCLValue; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function1; import org.simantics.scl.runtime.function.FunctionImpl1; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; import org.simantics.ui.workbench.action.DefaultActions; import org.simantics.utils.ui.SWTUtils; import org.slf4j.Logger; @@ -523,6 +531,10 @@ public class Functions { public Double apply(Resource edge) { return ONE; } + @Override + public String toString() { + return "1"; + } }; @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") @@ -547,4 +559,80 @@ public class Functions { }; } + private static class RangeValidator implements Function1 { + 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 { + private CommandSession session; + + public BrightnessExpressionValidator(List importEntries) { + this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT); + this.session.setImportEntries(imports(importEntries)); + } + + private ArrayList imports(List entries) { + ArrayList 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; + } + } + }