'Evaluating boolean expressions in Java dynamically

I made use of JEP earlier and then realised that it wont fit my case.

On a call to the database/backend, I get a json response, which has a structure as below:

"key": {
"!A & B"
}

And there will be values for A and B like true or false.

I used bpodgursky.jbool_expressions, but do not understand how to substitute values:

 Expression<String> parsedExpression = RuleSet.simplify(ExprParser.parse("!A&B"));
    RuleSet.assign("A", Boolean.TRUE); //THIS IS WRONG

Could anybody help me with the correct library to work on and some sample example to do so?



Solution 1:[1]

You'll need to create it like this.

Expression<String> expression = RuleSet.simplify(ExprParser.parse("!A&B"));
Map<String, Object> keyValue = Map.of("A", false, "B", true);
Expression<String> resolved = RuleSet.assign(expression, keyValue);
System.out.println(resolved); // returns true

Refer this documentation for more clarity on how to use it: https://github.com/bpodgursky/jbool_expressions

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aman