Search in sources :

Example 1 with Variable

use of parsii.eval.Variable in project pinot by linkedin.

the class MetricExpression method evaluateExpression.

public static double evaluateExpression(String expressionString, Map<String, Double> context) throws Exception {
    Scope scope = Scope.create();
    expressionString = expressionString.replace(COUNT_METRIC, COUNT_METRIC_ESCAPED);
    Map<String, Double> metricValueContext = context;
    if (context.containsKey(COUNT_METRIC)) {
        metricValueContext = new HashMap<>(context);
        metricValueContext.put(COUNT_METRIC_ESCAPED, context.get(COUNT_METRIC));
    }
    Expression expression = Parser.parse(expressionString, scope);
    for (String metricName : metricValueContext.keySet()) {
        Variable variable = scope.create(metricName);
        if (!metricValueContext.containsKey(metricName)) {
            throw new Exception("No value set for metric:" + metricName + "  in the context:" + metricValueContext);
        }
        variable.setValue(metricValueContext.get(metricName));
    }
    return expression.evaluate();
}
Also used : Variable(parsii.eval.Variable) Scope(parsii.eval.Scope) Expression(parsii.eval.Expression) ParseException(parsii.tokenizer.ParseException)

Aggregations

Expression (parsii.eval.Expression)1 Scope (parsii.eval.Scope)1 Variable (parsii.eval.Variable)1 ParseException (parsii.tokenizer.ParseException)1