Class FWParameterResolver

java.lang.Object
net.simplace.core.jexl.FWParameterResolver

public final class FWParameterResolver extends Object
Resolves parameters: replaces the placeholders ${name} of texts and evaluates JEXL expressions. This is the only implementation for the pipeline map (GUI, pipelines, data readers) and for the simulation variable map (file names, SQL statements, URLs and identifiers of solutions and projects); the values come from a FWParameterContext.

Texts (replace(FWParameterContext, String, Object, boolean)): the placeholders are replaced repeatedly as long as replacements happen, so a value may contain placeholders again and placeholders may be nested (${a${i}}). A name is resolved as special parameter (see FWParameterContext) or as value of the context. A parameter that is present without value is replaced by an empty text. If a parameter is missing, a warning is logged and

  • with aIgnoreNull, the placeholder is replaced by an empty text,
  • otherwise, if the caller gave a default other than the text itself, the default is returned (e.g. "0" for a flag),
  • otherwise the placeholder stays in the text, all other placeholders are replaced.
A missing setting ${SETTINGS...} makes the whole result the default (may be null).

Expressions (evaluate(FWParameterContext, String), evaluateBoolean(Map, String, boolean), evaluateNumber(Map, String)) are JEXL rules with the same syntax as the rules of a solution: ${a.b} is the value of the parameter a.b (not its text), e.g. ${count} gt 2, ${type} eq 'DOUBLE' or ${type} eq 'INT', file:exists('sim/sim_resource.zip'), ${values}[2]. All namespaces of JexlBuilder can be used. They are used for the control attributes of the GUI configuration (visible, enabled, required, if), the minimum and maximum of number fields and the rules of the pipeline.

Author:
Andreas Enders
  • Field Details

    • PARAMPATTERN

      public static final Pattern PARAMPATTERN
      Matches texts that contain a placeholder ${...}.
    • PARAMSEARCHPATTERN

      public static final Pattern PARAMSEARCHPATTERN
      Finds the innermost placeholders ${name} (without nested braces).
  • Method Details

    • hasPlaceholders

      public static boolean hasPlaceholders(String aText)
      Parameters:
      aText - the text, may be null
      Returns:
      true if the text contains a placeholder start ${
    • replace

      public static String replace(Map<String,Object> aParameterMap, String aText, Object aDefault)
      Replaces the placeholders with the values of a parameter map, see replace(FWParameterContext, String, Object, boolean).
      Parameters:
      aParameterMap - the parameter map (TEMPDIR may be added)
      aText - the text
      aDefault - the result for missing parameters, see class description
      Returns:
      the resulting text
    • replace

      public static String replace(Map<String,Object> aParameterMap, String aText, Object aDefault, boolean aIgnoreNull)
      Replaces the placeholders with the values of a parameter map, see replace(FWParameterContext, String, Object, boolean).
      Parameters:
      aParameterMap - the parameter map (TEMPDIR may be added)
      aText - the text
      aDefault - the result for missing parameters, see class description
      aIgnoreNull - true to replace missing parameters by an empty string
      Returns:
      the resulting text
    • replace

      public static String replace(FWSimVarMap aVarMap, String aText, Object aDefault)
      Replaces the placeholders with the values of a simulation variable map, see replace(FWParameterContext, String, Object, boolean).
      Parameters:
      aVarMap - the variable map (TEMPDIR may be added)
      aText - the text
      aDefault - the result for missing parameters, see class description
      Returns:
      the resulting text
    • replace

      public static String replace(FWParameterContext aContext, String aText, Object aDefault, boolean aIgnoreNull)
      Replaces the placeholders ${name} of a text, see class description.
      Parameters:
      aContext - provides the parameter values
      aText - the text, may be null
      aDefault - the result for missing parameters (unless it is the text itself) and for a missing setting
      aIgnoreNull - true to replace missing parameters by an empty string
      Returns:
      the resulting text; null if the text is null
    • evaluate

      public static Object evaluate(Map<String,Object> aParameterMap, String aExpression)
      Evaluates a JEXL expression with the values of a parameter map, see evaluate(FWParameterContext, String).
      Parameters:
      aParameterMap - the parameter map
      aExpression - the expression
      Returns:
      the result
      Throws:
      RuntimeException - if the expression cannot be compiled or evaluated
    • evaluate

      public static Object evaluate(FWParameterContext aContext, String aExpression)
      Evaluates a JEXL expression or script. Parameters are written as in solution rules (${a.b}), the special parameters can be used. The compiled expressions are cached.
      Parameters:
      aContext - provides the parameter values
      aExpression - the expression or script
      Returns:
      the result
      Throws:
      RuntimeException - if the expression cannot be compiled or evaluated
    • evaluateBoolean

      public static boolean evaluateBoolean(Map<String,Object> aParameterMap, String aExpression, boolean aDefault)
      Evaluates a control attribute (e.g. visible, enabled, required, if) as JEXL expression with the values of a parameter map, see evaluateBoolean(FWParameterContext, String, boolean).
      Parameters:
      aParameterMap - the parameter map
      aExpression - the expression, may be null or empty
      aDefault - the result for an empty expression or if it cannot be evaluated
      Returns:
      the result as boolean
    • evaluateBoolean

      public static boolean evaluateBoolean(FWParameterContext aContext, String aExpression, boolean aDefault)
      Evaluates a JEXL expression as boolean. The result is converted with StringHelper.parseBoolean(Object, boolean) (true, "true", 1, "yes", ...).
      Parameters:
      aContext - provides the parameter values
      aExpression - the expression, may be null or empty
      aDefault - the result for an empty expression, an unknown result or if it cannot be evaluated (a warning is logged)
      Returns:
      the result as boolean
    • evaluateNumber

      public static double evaluateNumber(Map<String,Object> aParameterMap, String aExpression)
      Evaluates a JEXL expression as number, e.g. the minimum ${a.min} + 1 of a number field.
      Parameters:
      aParameterMap - the parameter map
      aExpression - the expression
      Returns:
      the result as number; a text result is parsed; 0 if the result is no number
      Throws:
      RuntimeException - if the expression cannot be compiled or evaluated
    • oneOfRule

      public static String oneOfRule(String aParameter, String... aValues)
      Creates an expression that checks whether a parameter has one of several values, e.g. ${Datatype} eq 'CSV' or ${Datatype} eq 'DATABASE' for the control attributes of generated forms.
      Parameters:
      aParameter - name of the parameter
      aValues - the values (must not contain ')
      Returns:
      the expression
    • getParametersFromString

      public static Set<String> getParametersFromString(String aText)
      Parameters:
      aText - text to be analyzed for parameters, may be null
      Returns:
      the names of all innermost placeholders ${name} in the text, in order of appearance