当前位置: 首页 > 工具软件 > Easy Rules > 使用案例 >

JAVA规则引擎之EasyRules

麻书
2023-12-01

规则引擎之EasyRules

什么是EasyRules

首先EasyRule是一个规则引擎.这个名字由来是受到了Martin Fowler 的文章 Should I use a Rules Engine

You can build a simple rules engine yourself. All you need is to create a bunch of objects with conditions and actions, store them in a collection, and run through them to evaluate the conditions and execute the actions.

框架特点

轻量级类库和容易上手
基于POJO的开发与注解的编程模型
方便且适用于java的抽象的业务模型规则
支持从简单的规则创建组合规则

Useful abstractions to define business rules and apply them easily with Java
The ability to create composite rules from primitive ones

实践-FizzBuzz

循环打印 1 到 100,但如果是 5 的倍數就印 fizz,如果是 7 的倍數就印 buzz,如果同時是 5 和 7 的倍數就印 fizzzuzz,否则打印数字本身
不使用规则引擎

@Test 
public void fizzbuzz() { 
    for(int i = 1; i <= 100; i++) { 
        if (((i % 5) == 0) && ((i % 7) == 0)) 
            System.out.print("fizzbuzz"); 
        else if ((i % 5) == 0) 
            System.out.print("fizz"); 
        else if ((i % 7) == 0) 
            System.out.print("buzz"); 
        else 
            System.out.print(i); 
            System.out.println(); 
    }
     System.out.println(); 
}

使用Easy Rule
添加Maven依赖:

<dependency>
    <groupId>org.easyrules</groupId>
    <artifactId>easyrules-core</artifactId>
    <version>2.4.0</version>
</dependency>

定义FizzRule
检查输入是否是fizz

import org.easyrules.annotation.Action;
import org.easyrules.annotation.Condition;
import org.easyrules.annotation.Priority;
import org.easyrules.annotation.Rule;

@Rule  //规则类
public class FizzRule {

    private int input;

    @Condition   //判断是否命中规则
    public boolean isFizz(){
        return input % 5 ==0 ;
    }

    @Action   //命中规则后执行动作
    public void printFizz(){
        System.out.println("fizz");
    }

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

    @Priority  //优先级 0/1/2/3.....  越小优先级越高
    public int getPriority(){
        return 1;
    }
}
  • @Rule可以标注name和description属性,每个rule的name要唯一,如果没有指定,则RuleProxy则默认取类名
  • @Condition是条件判断,要求返回boolean值,表示是否满足条件
  • @Action标注条件成立之后触发的方法
  • @Priority标注该rule的优先级,默认是Integer.MAX_VALUE - 1,值越小越优先

BuzzRule

检查输入是否是buzz

import org.easyrules.annotation.Action;
import org.easyrules.annotation.Condition;
import org.easyrules.annotation.Priority;
import org.easyrules.annotation.Rule;

@Rule  //规则类
public class BuzzRule {

    private int input; 

    @Condition  //判断是否命中规则
    public boolean isBuzz() {
        return input % 7 == 0; 
        } 

    @Action  //命中规则后执行动作
    public void printBuzz() {
        System.out.print("buzz"); 
        } 

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

    @Priority  //优先级 0/1/2/3.....  越小优先级越高
    public int getPriority() {
        return 2; 
        }
}

FizzBuzzRule

检查输入是否是fizzbuzz,这是一个组合规则,它由FizzRule和BuzzRule组成

import org.easyrules.core.CompositeRule;

public class FizzBuzzRule extends CompositeRule {

    public FizzBuzzRule(Object... rules) {
        for (Object rule : rules) {
            addRule(rule);
        }
    }

    @Override
    public int getPriority() {
        return 0;
    }
}

NonFizzBuzzRule

import org.easyrules.annotation.Action;
import org.easyrules.annotation.Condition;
import org.easyrules.annotation.Priority;
import org.easyrules.annotation.Rule;

@Rule
public class NonFizzBuzzRule {

    private int input;

    @Condition
    public boolean isNotFizzNorBuzz() {
        // can return true here
        return input % 5 != 0 || input % 7 != 0;
    }

    @Action
    public void printInput() {
        System.out.print(input);
    }

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

    @Priority
    public int getPriority() {
        return 3;
    }
}

执行

import org.easyrules.api.RulesEngine;
import org.easyrules.core.RulesEngineBuilder;

public class FizzBuzzWihEasyRules {
    public static void main(String[] args) {
        RulesEngine fizzBuzzEngine = RulesEngineBuilder.aNewRulesEngine().withSkipOnFirstAppliedRule(true)
                .withSilentMode(true).build();
        // create rules
        FizzRule fizzRule = new FizzRule();
        BuzzRule buzzRule = new BuzzRule();
        //FizzBuzzRule fizzBuzzRule = new FizzBuzzRule(fizzRule, buzzRule);
        NonFizzBuzzRule nonFizzBuzzRule = new NonFizzBuzzRule();
        // register rules 注册规则
        fizzBuzzEngine.registerRule(fizzRule);
        fizzBuzzEngine.registerRule(buzzRule);
        fizzBuzzEngine.registerRule(nonFizzBuzzRule);
        // fire rules
        for (int i = 1; i <= 100; i++) {
            //设置入参
            fizzRule.setInput(i);
            buzzRule.setInput(i);
            nonFizzBuzzRule.setInput(i);
            fizzBuzzEngine.fireRules();
            System.out.println();
        }
    }
}

作者:孙成酱子说
链接:https://www.jianshu.com/p/a8f1c3b789f2
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

 类似资料: