parsii.eval.Variable Java Examples

The following examples show how to use parsii.eval.Variable. 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: ExpressionParser.java    From eagle with Apache License 2.0 6 votes vote down vote up
public ExpressionParser setVariables(Map<String, Double> tuple) throws ParsiiUnknowVariableException {
    // for(String valName : tuple.keySet()) {
    // Double value = tuple.get(valName);
    for (Map.Entry<String, Double> entry : tuple.entrySet()) {
        String valName = entry.getKey();
        Double value = entry.getValue();
        Variable variable = scope.getVariable(valName);
        if (variable != null && value != null) {
            variable.setValue(value);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.warn("Variable for " + valName + " is null in scope of expression: " + this.exprStr);
            }
        }
    }
    return this;
}
 
Example #2
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 #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: ExpressionParser.java    From Eagle with Apache License 2.0 5 votes vote down vote up
public ExpressionParser setVariables(Map<String, Double> tuple) throws ParsiiUnknowVariableException{
//		for(String valName : tuple.keySet()) {
//			Double value = tuple.get(valName);
		for(Map.Entry<String,Double> entry : tuple.entrySet()) {
            String valName = entry.getKey();
            Double value = entry.getValue();
			Variable variable = scope.getVariable(valName);
			if(variable!=null && value !=null) {
				variable.setValue(value);
			}else{
				if(LOG.isDebugEnabled()) LOG.warn("Variable for "+valName+" is null in scope of expression: "+this.exprStr);
			}
		}
		return this;
	}
 
Example #5
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);
}