规则引擎之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

定义FizzRule

检查输入是否是fizz

@Rule
public class FizzRule {

    private int input;

    @Condition
    public boolean isFizz() {
        return input % 5 == 0;
    }

    @Action
    public void printFizz() {
        System.out.print("fizz");
    }

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

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

BuzzRule

检查输入是否是buzz

@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
    public int getPriority() {
        return 2;
    }
}

FizzBuzzRule

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

public class FizzBuzzRule extends CompositeRule {

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

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

NonFizzBuzzRule

@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;
    }
}

执行

public class FizzBuzzWithEasyRules {

    public static void main(String[] args) {

        // create rules engine
        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(fizzBuzzRule);
        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();
        }
    }
}

文档

EasyRules Github

Easy Rules

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

互联网金融系列-支付清算体系例子-下(理解日切)

笔者上一篇《互联网金融系列-支付清算体系介绍-上》已经比较全面的介绍了以银联为例子的支付清算体系,为了更好的理解里面的运作,本章以两个例子为重点,全...

阅读全文

互联网金融系列-支付清算体系介绍-上

一,支付清算体系的简介 支付清算体系是一个国家的金融基础设施,或说公共服务。我国由央行主管此事,目前大体维持“结算-清算”二级制的支付体系。通俗地讲,...

阅读全文

JDK8 – 当尝试使用Maven生成javadoc时,错误“javax.interceptor.InterceptorBinding未找到的类文件”javadoc plugin

我使用JDK8(尝试它在我的Eclipse工作区与Win x64 u25 JDK在Linux上由Jenkins发布 – jdk-8u20-linux-x64,两个相同的问题)。我有多模块Maven项目(我从包装类...

阅读全文

欢迎留言