parsii.eval.Parser Java Examples

The following examples show how to use parsii.eval.Parser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetricExpression.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #2
Source File: ParserTest.java    From parsii with MIT License 6 votes vote down vote up
@Test
public void simple() throws ParseException {
    assertEquals(-109d, Parser.parse("1 - (10 - -100)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.01d, Parser.parse("1 / 10 * 10 / 100").evaluate(), BinaryOperation.EPSILON);
    assertEquals(-89d, Parser.parse("1 + 10 - 100").evaluate(), BinaryOperation.EPSILON);
    assertEquals(91d, Parser.parse("1 - 10 - -100").evaluate(), BinaryOperation.EPSILON);
    assertEquals(91d, Parser.parse("1 - 10  + 100").evaluate(), BinaryOperation.EPSILON);
    assertEquals(-109d, Parser.parse("1 - (10 + 100)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(-89d, Parser.parse("1 + (10 - 100)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(100d, Parser.parse("1 / 1 * 100").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.01d, Parser.parse("1 / (1 * 100)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.01d, Parser.parse("1 * 1 / 100").evaluate(), BinaryOperation.EPSILON);
    assertEquals(7d, Parser.parse("3+4").evaluate(), BinaryOperation.EPSILON);
    assertEquals(7d, Parser.parse("3      +    4").evaluate(), BinaryOperation.EPSILON);
    assertEquals(-1d, Parser.parse("3+ -4").evaluate(), BinaryOperation.EPSILON);
    assertEquals(-1d, Parser.parse("3+(-4)").evaluate(), BinaryOperation.EPSILON);
}
 
Example #3
Source File: ParserTest.java    From parsii with MIT License 6 votes vote down vote up
@Test
public void scopes() throws ParseException {
    Scope root = new Scope();
    Variable a = root.getVariable("a").withValue(1);
    Scope subScope1 = new Scope().withParent(root);
    Scope subScope2 = new Scope().withParent(root);
    Variable b1 = subScope1.getVariable("b").withValue(2);
    Variable b2 = subScope2.getVariable("b").withValue(3);
    Variable c = root.getVariable("c").withValue(4);
    Variable c1 = subScope1.getVariable("c").withValue(5);
    assertEquals(c, c1);
    Variable d = root.getVariable("d").withValue(9);
    Variable d1 = subScope1.create("d").withValue(7);
    assertNotEquals(d, d1);
    Expression expr1 = Parser.parse("a + b + c + d", subScope1);
    Expression expr2 = Parser.parse("a + b + c + d", subScope2);
    assertEquals(15d, expr1.evaluate(), BinaryOperation.EPSILON);
    assertEquals(18d, expr2.evaluate(), BinaryOperation.EPSILON);
    a.setValue(10);
    b1.setValue(20);
    b2.setValue(30);
    c.setValue(40);
    c1.setValue(50);
    assertEquals(87d, expr1.evaluate(), BinaryOperation.EPSILON);
    assertEquals(99d, expr2.evaluate(), BinaryOperation.EPSILON);
}
 
Example #4
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void startingWithDecimalPoint() throws ParseException {
    assertEquals(.2, Parser.parse(".2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(.2, Parser.parse("+.2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(.4, Parser.parse(".2+.2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(.4, Parser.parse(".6+-.2").evaluate(), BinaryOperation.EPSILON);
}
 
Example #5
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void errorOnUnknownVariable() throws ParseException {
    Scope s = new Scope();
    try {
        s.create("a");
        s.create("b");
        Parser.parse("a*b+c", s);
    } catch (ParseException e) {
        assertEquals(1, e.getErrors().size());
    }

    s.create("c");
    Parser.parse("a*b+c", s);
}
 
Example #6
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void getVariables() throws ParseException {
    Scope s = new Scope();
    Parser.parse("a*b+c+e+wer", s);
    assertTrue(s.getNames().contains("a"));
    assertTrue(s.getNames().contains("b"));
    assertTrue(s.getNames().contains("c"));
    assertTrue(s.getNames().contains("e"));
    assertTrue(s.getNames().contains("wer"));
    assertFalse(s.getNames().contains("x"));

    // pi and euler are always defined...
    assertEquals(7, s.getVariables().size());
}
 
Example #7
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void quantifiers() throws ParseException {
    assertEquals(1000d, Parser.parse("1K").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1000d, Parser.parse("1M * 1m").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1d, Parser.parse("1n * 1G").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1d, Parser.parse("(1M / 1k) * 1m").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1d, Parser.parse("1u * 10 k * 1000  m * 0.1 k").evaluate(), BinaryOperation.EPSILON);
}
 
Example #8
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void relationalOperators() throws ParseException {
    // Test for Issue with >= and <= operators (#4)
    assertEquals(1d, Parser.parse("5 <= 5").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1d, Parser.parse("5 >= 5").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0d, Parser.parse("5 < 5").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0d, Parser.parse("5 > 5").evaluate(), BinaryOperation.EPSILON);
}
 
Example #9
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void functions() throws ParseException {
    assertEquals(0d, Parser.parse("1 + sin(-pi) + cos(pi)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(4.72038341576d, Parser.parse("tan(sqrt(euler ^ (pi * 3)))").evaluate(), BinaryOperation.EPSILON);
    assertEquals(3d, Parser.parse("| 3 - 6 |").evaluate(), BinaryOperation.EPSILON);
    assertEquals(3d, Parser.parse("if(3 > 2 && 2 < 3, 2+1, 1+1)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(2d, Parser.parse("if(3 < 2 || 2 > 3, 2+1, 1+1)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(2d, Parser.parse("min(3,2)").evaluate(), BinaryOperation.EPSILON);

    // Test a var arg method...
    Parser.registerFunction("avg", new Function() {
        @Override
        public int getNumberOfArguments() {
            return -1;
        }

        @Override
        public double eval(List<Expression> args) {
            double avg = 0;
            if (args.isEmpty()) {
                return avg;
            }
            for (Expression e : args) {
                avg += e.evaluate();
            }
            return avg / args.size();
        }

        @Override
        public boolean isNaturalFunction() {
            return true;
        }
    });
    assertEquals(3.25d, Parser.parse("avg(3,2,1,7)").evaluate(), BinaryOperation.EPSILON);
}
 
Example #10
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void variables() throws ParseException {
    Scope scope = new Scope();

    Variable a = scope.create("a");
    Variable b = scope.create("b");
    Expression expr = Parser.parse("3*a + 4 * b", scope);
    assertEquals(0d, expr.evaluate(), BinaryOperation.EPSILON);
    a.setValue(2);
    assertEquals(6d, expr.evaluate(), BinaryOperation.EPSILON);
    b.setValue(3);
    assertEquals(18d, expr.evaluate(), BinaryOperation.EPSILON);
    assertEquals(18d, expr.evaluate(), BinaryOperation.EPSILON);
}
 
Example #11
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void signedValueAfterOperand() throws ParseException {
    assertEquals(-1.2, Parser.parse("1+-2.2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(3.2, Parser.parse("1++2.2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(6 * -1.1, Parser.parse("6*-1.1").evaluate(), BinaryOperation.EPSILON);
    assertEquals(6 * 1.1, Parser.parse("6*+1.1").evaluate(), BinaryOperation.EPSILON);
}
 
Example #12
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void signedParentheses() throws ParseException {
    assertEquals(0.2, Parser.parse("-(-0.2)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1.2, Parser.parse("1-(-0.2)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.8, Parser.parse("1+(-0.2)").evaluate(), BinaryOperation.EPSILON);
    assertEquals(2.2, Parser.parse("+(2.2)").evaluate(), BinaryOperation.EPSILON);
}
 
Example #13
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void signed() throws ParseException {
    assertEquals(-2.02, Parser.parse("-2.02").evaluate(), BinaryOperation.EPSILON);
    assertEquals(2.02, Parser.parse("+2.02").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1.01, Parser.parse("+2.02 + -1.01").evaluate(), BinaryOperation.EPSILON);
    assertEquals(-4.03, Parser.parse("-2.02 - +2.01").evaluate(), BinaryOperation.EPSILON);
    assertEquals(3.03, Parser.parse("+2.02 + +1.01").evaluate(), BinaryOperation.EPSILON);
}
 
Example #14
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void precedence() throws ParseException {
    // term vs. product
    assertEquals(19d, Parser.parse("3+4*4").evaluate(), BinaryOperation.EPSILON);
    // product vs. power
    assertEquals(20.25d, Parser.parse("3^4/4").evaluate(), BinaryOperation.EPSILON);
    // relation vs. product
    assertEquals(1d, Parser.parse("3 < 4*4").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0d, Parser.parse("3 > 4*4").evaluate(), BinaryOperation.EPSILON);
    // brackets
    assertEquals(28d, Parser.parse("(3 + 4) * 4").evaluate(), BinaryOperation.EPSILON);
    assertEquals(304d, Parser.parse("3e2 + 4").evaluate(), BinaryOperation.EPSILON);
    assertEquals(1200d, Parser.parse("3e2 * 4").evaluate(), BinaryOperation.EPSILON);
}
 
Example #15
Source File: ParserTest.java    From parsii with MIT License 5 votes vote down vote up
@Test
public void number() throws ParseException {
    assertEquals(4003.333333d, Parser.parse("3.333_333+4_000").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.03, Parser.parse("3e-2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(300d, Parser.parse("3e2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(300d, Parser.parse("3e+2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(320d, Parser.parse("3.2e2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.032, Parser.parse("3.2e-2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.03, Parser.parse("3E-2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(300d, Parser.parse("3E2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(300d, Parser.parse("3E+2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(320d, Parser.parse("3.2E2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(0.032, Parser.parse("3.2E-2").evaluate(), BinaryOperation.EPSILON);
}
 
Example #16
Source File: MetricExpression.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
public List<MetricFunction> computeMetricFunctions() {
  try {
    Scope scope = Scope.create();
    Set<String> metricTokens = new TreeSet<>(); // can be either metric names or ids ! :-/

    // expression parser errors out on variables starting with _
    // we're replacing the __COUNT default metric, with an escaped string
    // after evaluating, we replace the escaped string back with the original
    String modifiedExpressions = expression.replace(COUNT_METRIC, COUNT_METRIC_ESCAPED);

    Parser.parse(modifiedExpressions, scope);
    metricTokens = scope.getLocalNames();

    ArrayList<MetricFunction> metricFunctions = new ArrayList<>();
    for (String metricToken : metricTokens) {
      Long metricId = null;
      MetricConfigDTO metricConfig = null;
      String metricDataset = dataset;
      DatasetConfigDTO datasetConfig = ThirdEyeUtils.getDatasetConfigFromName(metricDataset);
      if (metricToken.equals(COUNT_METRIC_ESCAPED)) {
        metricToken = COUNT_METRIC;
      } else {
        metricId = Long.valueOf(metricToken.replace(MetricConfigBean.DERIVED_METRIC_ID_PREFIX, ""));
        metricConfig = ThirdEyeUtils.getMetricConfigFromId(metricId);
        if (metricConfig != null) {
          metricDataset = metricConfig.getDataset();
        }
      }
      metricFunctions.add(
          new MetricFunction(aggFunction, metricToken, metricId, metricDataset, metricConfig, datasetConfig));
    }
    return metricFunctions;
  } catch (ParseException e) {
    throw new RuntimeException("Exception parsing expressionString:" + expression, e);
  }
}
 
Example #17
Source File: TestExpressionPerformance.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public double parse(String exprStr, Map<String, Double> tuple) throws Exception {
    Scope scope = Scope.create();
    if (expression == null) {
        expression = Parser.parse(exprStr, scope);
    }
    for (String valName : tuple.keySet()) {
        Object value = tuple.get(valName);
        if (value instanceof Number) {
            scope.getVariable(valName).setValue(((Number)value).doubleValue());
        }
    }
    return expression.evaluate();
}
 
Example #18
Source File: TestExpressionPerformance.java    From Eagle with Apache License 2.0 5 votes vote down vote up
public double parse(String exprStr, Map<String, Double> tuple) throws Exception{		
	Scope scope = Scope.create();
	if (expression == null) {
		expression = Parser.parse(exprStr, scope);
	}
	for(String valName : tuple.keySet()) {
		Object value = tuple.get(valName);
		if(value instanceof Number) {
			scope.getVariable(valName).setValue(((Number)value).doubleValue());
		}
	}
	return expression.evaluate();
}
 
Example #19
Source File: ParserTest.java    From parsii with MIT License 4 votes vote down vote up
@Test
public void trailingDecimalPoint() throws ParseException {
    assertEquals(2., Parser.parse("2.").evaluate(), BinaryOperation.EPSILON);
}
 
Example #20
Source File: ParserTest.java    From parsii with MIT License 4 votes vote down vote up
@Test
public void blockComment() throws ParseException {
    assertEquals(29, Parser.parse("27+ /*xxx*/ 2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(29, Parser.parse("27+/*xxx*/ 2").evaluate(), BinaryOperation.EPSILON);
    assertEquals(29, Parser.parse("27/*xxx*/+2").evaluate(), BinaryOperation.EPSILON);
}
 
Example #21
Source File: ExpressionParser.java    From eagle with Apache License 2.0 4 votes vote down vote up
/**
 * @param exprStr expression string in format like: <code>(max(a, b)* min(a, b)) / abs(a-b+c-d)</code>
 * @throws ParseException
 * @throws ParsiiInvalidException
 */
public ExpressionParser(String exprStr) throws ParseException, ParsiiInvalidException {
    this.exprStr = exprStr;
    scope = Scope.create();
    expression = Parser.parse(this.exprStr, scope);
}
 
Example #22
Source File: ExpressionParser.java    From Eagle with Apache License 2.0 2 votes vote down vote up
/**
 * @param exprStr expression string in format like: <code>(max(a, b)* min(a, b)) / abs(a-b+c-d)</code>
 *
 * @throws ParseException
 * @throws ParsiiInvalidException
 */
public ExpressionParser(String exprStr) throws ParseException, ParsiiInvalidException{
	this.exprStr = exprStr;
	scope = Scope.create();
	expression = Parser.parse(this.exprStr,scope);
}