规则引擎之EasyRules

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

你可能感兴趣的:(规则引擎之EasyRules)