looking to build a java dynamic rule engine using easy rules api

11.9k views Asked by At

I have many rules to be evaluated against the data in our database, i am planning on using easyrules api. I dont prefer programming or hardcoding the rules within my code. It should be easy to change the rule criteria once the code is built. Pls assist me on how can i make it dynamic and readable. Easyrules api insists on having a separate class for each rule as below: I want the below code to be dynamically built based on easily modifiable rule input: I was thinking of the below but not sure which one is best: 1. DB table - a rule table with rule conditions. (I can use an update query to change the rules) 2. JSON or XML.

I am flexible on other tools too not confined to easyrules. https://github.com/j-easy/easy-rules/wiki/hello-world

/**
 * Hello World rule class.
 *
 * @author Mahmoud Ben Hassine ([email protected])
 */
@Rule(name = "Hello World rule", description = "Say Hello to duke's friends only")
public class HelloWorldRule {

    /**
     * The user input which represents the data that the rule will operate on.
     */
    private String input;

    @Condition
    public boolean checkInput() {
        //The rule should be applied only if the user's response is yes (duke friend)
        return input.equalsIgnoreCase("yes");
    }

    @Action
    public void sayHelloToDukeFriend() throws Exception {
        //When rule conditions are satisfied, prints 'Hello duke's friend!' to the console
        System.out.println("Hello duke's friend!");
    }

    public void setInput(String input) {
        this.input = input;
    }
4

There are 4 answers

1
 Krishna  Rangaiah On

I would probably suggest you go with java + groovySrcipt engine. You can provide your rules in GroovyScripts and can dynamically execute them using GroovyEngine. it just a thought.

0
Nathalie On

I know my answer is late but it may interest somebody else.

I'm using easyrules engine a different way.

I use only one kind of rule and I'm able to do very complexe things with easyRule.

My rules are hard coded by I guess it may be possible to do something similar with rules stored in a database.

In my project I need to go from step to step depending on a complexe set of conditions. And the set of condition change for each step. My rule engine will build a new situation from the current situation depending on the conditions.

My unique rule, juste holds a test object and a next object.

And I have a currentSituation object which holds all parameters of the current situation. It has

  • a test() methods which just clones the object (and reset a ruleEvaluates flag to true).
  • a next() methods which just clones the object (and reset a few parametres)
  • a bunch of "is" methods which return the object itself but changes the ruleEvaluate to false if the condition is not matched.
  • a bunch of "do" methods which return the object it self after changing the relevant value.

And then I have a ruleEngine factory to provide a set of rules depending on the current step.

Each RuleEngine is the writen very easily using almost natural langage.

Ex

Situation currentSituation = getSituation();    
int priority=0;
Rule rule1 = new Rule(
    "MONDAY_RULE",
    priority++, 
    currentSituation.test().isMonday().isRaining(),
    currentSituation.next().docallMyBoss("won't work today").doGoBackToBed());

....


Rule rule7 = newRule(
    "FRIDAY_RULE",
    priority++,
    currentSituation.test().isFriday().isSunny(),
    currentSituation.next().doBuy("bathing Suit").doPlanWeekendBreak());

register(rule1);
register(rule2);

As the "is" methods and "do" methods return the currentSituation Object I can chain test condition and build options easily.

In the evaluate() method of the rule, I juste need to test the RuleEvalue flag of the testObject to see if it's true or not and if true, I provide the object that was built with the next() method

It works very well, and is easy to maintain and read. I just have to create new "is" and "do" methods to get more power.

I send a great thank you to Mahmoud Ben Hassine for providing such a simple and easy rule engine !

0
Lu Ming On

I thought you can refer this page, Easy Rules Example

It is code with @override

public class DefenseFourthDownRule extends BasicFootballRule {

    // return `true` if the rule should be applied, `false` else
    @Override public boolean evaluate() {
        if (gameState == null) return false;
        if (gameState.down != 4) return false;

        // we know it's 4th down ...
        return true;
    }

    @Override public void execute() {
        System.out.println("4th Down rule fired");
    }
}
0
ᐅdevrimbaris On

This is an old question but by the end of 2017, Easy Rules can have dynamic rules by external files.

From their website:

name: "alcohol rule" 
description: "children are not allowed to buy alcohol" 
priority: 2 condition: "person.isAdult() == false" 
actions: - "System.out.println(\"Shop: Sorry, you are not allowed to buy alcohol\");"