-1

We are trying to build a generic Alert Registration Service for consumers. Here is the problem we are trying to solve :

  1. An alert can have 'N' templates registered.Each template would be picked up based on a rule match
  2. The rule is going to be something like this :

    {"rule1":{"fieldName":"field1","operator":">","value":"100"}

    FieldName could be something like - accountBal, status etc. Operator could be arithmetic operators like ("+","-","/","*") or logical operations like (&&, == , ||) We are planning to define a /registration service and an /execute service.

Let us take a rule say : rule1 = field1 > 100 --> choose Template 1 rule2 = field1 >=100 && field2 <150 - Choose Template 2

Basically I am envisioning it like this :

abstract class Rule
{
    private String field;
    private Operator symbol;
    private String fieldValue

}

A Rule --> can resolve to N Templates and the system should send me the list of templates back.

I did start looking at the Interpreter pattern and the Rule pattern, but I have not found I am looking at.

Basically I need a way to configure rules (will be sent as json) from service API and stored in DB. At runTime , based on the applicationId, bunch of rules will be fired and will return List<Template>

Any help would be appreciated

I was planning to use a custom Rule Engine with StandardEvaluationContext, but an example would help me get going.

  • Do you have a specific question? Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – shmosel Jun 04 '18 at 22:22
  • Yes. I am trying to see how I can use Spring EL StandardEvaluationContext in conjuction with a RuleRegistry to register rules and execute at runtime. – avenugopalan Jun 04 '18 at 22:30
  • A Java Scripting API, say with JavaScript would do fine, where "rule" then is a script (or function) evaluating to a boolean result. Advantage: numeric types. Alternatively since Java 9: REPL functionality, also programatically. – Joop Eggen Jun 04 '18 at 22:38

1 Answers1

0

I has used Apache Velocity to create a rule engine. Velocity provides ‘eval’ function which is able to evaluate boolean expressions.

Halil
  • 1,795
  • 1
  • 24
  • 39