0

I'm working on a tool in the context of a java project to evaluate a custom domain specific, rule-like expression like

min-5 avg datalist > Number

with the individual tokens meaning the following:

  • min-5 : optional minimum (or maximum, in that case max-5) occurences of the following term
  • avg : an optional aggregation function which operates on the following token datalist (can also be sum or anything similar)
  • datalist : A list of data (type: integer/ double) which will be available before the evaluation of the entire expression starts, can be reduced to a single value by the preceding aggregation function
  • operator: conditional operator < or > or =
  • Number: value for the conditional operator

Note(s):

  • The optional amount of occurrences and the aggregation can not happen both, that would make no sense.
  • There can be multiple of the above expressions, chained with and/or
  • These expressions are external input, not pre-defined

The evaluation of this expression should output a boolean

As I am rather new to expression evaluation / parsing I am searching for an elegant way to solve this, possibly with a java framework/tool.

What I've tried so far:

  • Parsing by hand which turned out not so nicely
  • Trying to use Janino Expression Evaluator, but I don't know how to handle this programmatically

I am searching for a solution to solve this in an elegant way, I am thankful for any suggestions

Molly
  • 1,887
  • 3
  • 17
  • 34
  • See my SO answer on how to build parsers (especially for expressions) easily. These parsers have the nice property that you can use them to evaluate the expessions, too. https://stackoverflow.com/a/2336769/120163 – Ira Baxter Jan 22 '19 at 12:34

1 Answers1

0

what you try to do is a DSL (domain specific language) and the elegant way to solve your issue is to create a grammar for yuor specific language that help you on parsing function. Take a look at JavaCC or Antlr.

gccodec
  • 343
  • 1
  • 8