Tuesday, 3 May 2016

How to use MVEL expression language

Add the below dependency in pom.xml

   <properties>
       <mvel.version>2.2.0.Final</mvel.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mvel</groupId>
         <artifactId>mvel2</artifactId>
<version>${mvel.version}</version>
</dependency>
</dependencies>

Below code helps to evaluate an expression through MVEL

A & B are said to be the fields that are part of the expression and these are substituted with values from the Mapfor evaluation.

   String prefValue = "(A <= B)";
Serializable expression = MVEL.compileExpression(prefValue);

Map<String, BigDecimal> expressionVariables = new HashMap<String, BigDecimal>(2);
expressionVariables.put("A", new BigDecimal(10));
expressionVariables.put("B", new BigDecimal(20));

System.out.println( (Boolean)MVEL.executeExpression(expression, expressionVariables));

No comments:

Post a Comment