Example CodeOpenSourceTips

Boolean Expression Evaluation using JEP in Java

1 Mins read

There are not many open source library available for Boolean expression evaluation. If we have a requirement of evaluating an expression of this kind

A && ( B || C || D)

A && B && (C || D)
…….

We may need to right our own parser to read the expression string and evaluate based on precedence. This code may be difficult to write as well.
JEP ( http://www.singularsys.com/jep/) is a Java library for parsing and evaluating mathematical expressions. With this package you can allow your users to enter an arbitrary formula as a string, and instantly evaluate it. Jep supports user defined variables, constants, and functions. A number of common mathematical functions and constants are included.

Its version 2.4.1 is available as a GPL license which can be downloaded from following location http://www.singularsys.com/jep/download.html

The JEP uses Double as result object so it means a value of 1.0 is equal to Boolean.TRUE and 0.0 is equal ant to Boolean.FALSE.

Below is a utility program which demonstrates how we can use JEP for Boolean Expression evaluation. This class can be used as it is in you application to make calls for Boolean expression evaluation.

This program requires the 2 distribution jars from JEP downloads.

jep-.jar
ext-.jar

These files should be available in the downloaded zip file in dist directory.

The addVariable() method of JEP is case sensitive for the variable name, so make sure you are using the exactly same case for expression and addVariable() method.

/**
*
*/
package expression;

import org.nfunk.jep.JEP;
import org.nfunk.jep.Node;

/**
* @author Sachin Joshi
*
*/
public class ExpressionEvaluator {
private String expression;
private JEP jep;

public ExpressionEvaluator(String expr) {
this.expression = expr;
jep = new JEP();
}

public JEP getJep() {
return jep;
}

public boolean evaluate() {
try {
Node n1 = jep.parse(expression);
Double result = (Double) jep.evaluate(n1);
System.out.println(expression + " = " + result);
return (!result.equals(0.0d));
} catch (Exception e) {
System.out.println("An error occured: " + e.getMessage());
}
return false;
}

public static void main(String[] args) {
ExpressionEvaluator e = new ExpressionEvaluator("x && (y || z || w)");
e.getJep().addVariable("x", Boolean.TRUE);
e.getJep().addVariable("x", Boolean.TRUE);
e.getJep().addVariable("y", Boolean.FALSE);
e.getJep().addVariable("z", Boolean.FALSE);
e.getJep().addVariable("w", Boolean.TRUE);
System.out.println("Result is : " + e.evaluate());
}
}
Related posts
KubernetesTips

3 Tips to Troubleshoot Readiness Probe Failed Errors Effectively : Kubernetes

4 Mins read
A Kubernetes readiness probe can improve your service’s quality and reduce operational issues, making your service more resilient and robust. However, the…
GadgetsSoftwareTechnologyTips

Is It good to Wear Eyeglasses for Software Engineers?

2 Mins read
As technology continues to be a fundamental tool in our day-to-day lives, we spend more time staring at digital screens. This may…
Tips

The 3 Things It Takes To Become A Digital Nomad

3 Mins read
There is so little reason to sit in a cubicle in an office that takes hours out of your day commuting to…
Power your team with InHype

Add some text to explain benefits of subscripton on your services.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *