规则引擎之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
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Java实现国密算法SM2,SM3,SM4,并且实现ECB和CBC模式

代码中实现了电码本ECB模式和密文分组连接CBC模式,SM3.java和SM4.java为算法实现类,utils的都是根据实现类写的工具,可以根据需要调用杂凑算法SM3的杂凑功...

阅读全文

Spring Data JPA使用复合主键

演示环境 MySQL 5.7 JDK1.8 spring-data-jpa 1.10.4.RELEASE hibernate 5.1.2.Final 这里演示一个余额宝的例子,一个用户一天一条记录,表示一个用户一天的收...

阅读全文

加密机相关

数据加密机主要用于实现对主机应用层数据加/解密、消息来源正确性验证、密钥管理等。全国大多数商业银行,城市一卡通系统、公交卡系统、社保卡系统、加油卡系...

阅读全文

欢迎留言