package com.oguerreiro.resilient.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.mvel2.MVEL;

public class MvelTest {
  @Test
  void mvelEvalTest() throws Exception {
    String expression = "int i = 10; " + "int ii = 20; " + "i*ii+10";
    expression = "10.10**2";
    //expression = "i = Math.pow(10.10, 2); Math.round(i * 100) / 100.0";
    //expression = "Math.round(Math.pow(10.10, 2) * 100) / 100.0";

    Map<String, Object> variables = new HashMap<String, Object>();

    System.out.println(evaluateExpression(expression, variables));
  }

  public Object evaluateExpression(String expression, Map<String, Object> variables) {
    // Compiling the expression for performance (optional)
    Serializable compiledExpression = MVEL.compileExpression(expression);
    // Execute the compiled expression with variables
    return MVEL.executeExpression(compiledExpression, variables);
  }
}
