Question

I need to evaluate a boolean expression. The purpose is to filter a set of tagged items. Tag can be any name (let's say, like Java identifier).

For example:

foo OR (bar AND !baz)

This would be true for items tagged:

  • foo fee
  • foo bar boo
  • bar

I don't know in advance which names will be needed. I've checked JEXL but it has JexlContext, which is basically a Map which needs to be filled prior to evaluation.

I need some library which calls a callback function to decide whether an identifier is true or false. Or any other mechanism to allow identifiers unknown prior to evaluation.

It has to be free as in beer (it's for a Maven plugin), so Jep is out of question.

What can I use?

Was it helpful?

Solution

You can provide an implementation of JexlContext that does not rely on a Map. Instead of filling it before evaluation, you can treat its checks for has, get as your callbacks, and ignore the calls to set.

OTHER TIPS

Try Predicate in Google's guava

The easiest way is to use JavaScript that is a part of JDK since java 6. Here is an example

    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    // populate variables foo, bar and baz to the engine, e.g.
    engine.put("foo", true);
    engine.put("bar", true);
    engine.put("baz", false);
    engine.eval("foo OR (bar AND !baz)"); 

For more information take a look on the tutorial from Oracle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top