java規則引擎easy-rules使用指南 1 - 基本用法


規則引擎能干什么

規則引擎的工作方式有點像if-else,它允許你設置一些條件和動作,然后在程序運行時判斷某些動作該不該執行。
easy-rules是一款輕量級的java規則引擎,目前它的長期支持版本是4.1.x,所以我們就以4.1.0版本來看一下如何使用。

如何引入

如果使用maven,可以直接在pom中加入:

<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-core</artifactId>
    <version>4.1.0</version>
</dependency>

如果需要對MVEL, SpEL和JEXL表達式的支持,還需要引入相應的支持包:

<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-mvel</artifactId>
    <version>4.1.0</version>
</dependency>

<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-spel</artifactId>
    <version>4.1.0</version>
</dependency>

<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-jexl</artifactId>
    <version>4.1.0</version>
</dependency>

一個簡單的例子

使用easy-rules非常簡單,只需要兩個步驟:

  • 創建規則和動作
  • 運行引擎

以下是一個簡單的例子:

public class Test {
    public static void main(String[] args) {
		    // define rules 
				Rule weatherRule = new RuleBuilder()
        .name("weather rule")
        .description("if it rains then take an umbrella")
        .when(facts -> facts.get("rain").equals(true))
        .then(facts -> System.out.println("It rains, take an umbrella!"))
        .build();
        Rules rules = new Rules();
        rules.register(weatherRule);
		
        // define facts
        Facts facts = new Facts();
        facts.put("rain", true);

        // fire rules on known facts
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }
}

例子中的weatherRule是構建的條件和動作,其中“when”里面的是條件,“then”里面的是滿足條件后需要執行的動作。
facts是運行中實際的數據。
這個例子會在控制台打印出:It rains, take an umbrella!

如何使用Rule

1 介紹

Rule用於定義規則和行為,它可以設置以下這些屬性:

  • name: 命名空間中的唯一規則名稱
  • description: 描述
  • priority: 優先級
  • when:規則
  • then:行為

當when中的表達式返回true時,將執行then中的表達式。
then里面除了可以執行方法外,也可以修改Facts里面的數據。

2 Rule的多種寫法

easy-rules提供了多種定義Rule的寫法,還是以上面的例子舉例,下面的Rule寫法與上面例子中的寫法是等價的。

2.1 使用RuleBuilder

Rule weatherRule = new RuleBuilder()
        .name("weather rule")
        .description("if it rains then take an umbrella")
        .when(facts -> facts.get("rain").equals(true))
        .then(facts -> System.out.println("It rains, take an umbrella!"))
        .build();

2.2 使用注解

@Rule(name = "weather rule", description = "if it rains then take an umbrella")
public class WeatherRule {

    @Condition
    public boolean itRains(@Fact("rain") boolean rain) {
        return rain;
    }
    
    @Action
    public void takeAnUmbrella() {
        System.out.println("It rains, take an umbrella!");
    }
}

2.3 使用表達式語言

Rule weatherRule = new MVELRule()
        .name("weather rule")
        .description("if it rains then take an umbrella")
        .when("rain == true")
        .then("System.out.println(\"It rains, take an umbrella!\");");

使用表達式語言需要引入對應的支持包,比如這里使用了MVEL,那么需要在pom中引入easy-rules-mvel這個包

2.4 使用yml配置文件

name: "weather rule"
description: "if it rains then take an umbrella"
condition: "rain == true"
actions:
  - "System.out.println(\"It rains, take an umbrella!\");"
MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
Rule weatherRule = ruleFactory.createRule(new FileReader("weather-rule.yml"));

3 復合規則

有時候我們需要把多個規則放在一起使用,就好像寫多層if-else一樣。
easy-rules為此提供了三個對象來支持復合規則的使用:

  • UnitRuleGroup:要么應用所有rule,要么不應用任何rule。
  • ActivationRuleGroup:它觸發第一個適用的rule並忽略組中的其他rule(XOR 邏輯)。rule首先按其在組內的自然順序排序,如果設置了priority,那么數字越小的優先級越高。
  • ConditionalRuleGroup::只有具有最高優先級的rule評估為真,才觸發其余rule。

下面是使用示范:

//Create a composite rule from two primitive rules
UnitRuleGroup myUnitRuleGroup =
    new UnitRuleGroup("myUnitRuleGroup", "unit of myRule1 and myRule2");
myUnitRuleGroup.addRule(myRule1);
myUnitRuleGroup.addRule(myRule2);

//Register the composite rule as a regular rule
Rules rules = new Rules();
rules.register(myUnitRuleGroup);

RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.fire(rules, someFacts);

如何使用facts

Fact是用來裝需要判斷的數據的,它的API定義如下:

public class Fact<T> {
   private final String name;
   private final T value;
}

使用的時候直接用Facts,就跟Map用法差不多:

Facts facts = new Facts();
facts.put("foo", "bar");

Rule的“then”代碼中可以修改facts的數據,可以使用這個特性來獲取返回值。
例如:

Rule ageRule = new MVELRule()
        .name("age rule")
        .description("Check if person's age is > 18 and marks the person as adult")
        .priority(1)
        .when("person.age > 18")
        .then("person.setAdult(true);");

注意:

  • 如果when方法中缺少注入的Fact,引擎將記錄一個警告並認為條件評估為false。
  • 如果then方法中缺少注入的Rule,則不會執行該操作,並且引擎將拋出一個org.jeasy.rules.core.NoSuchFactException.

如何使用Engine

1 Engine的兩種實現

Easy Rules 提供了兩種RulesEngine接口實現:

DefaultRulesEngine:根據其自然順序應用規則(默認為優先級)。
InferenceRulesEngine:不斷地對已知事實應用規則,直到不再適用規則為止。

DefaultRulesEngine的作用很好理解,就像上面那些例子表現出來的一樣。
InferenceRulesEngine則相當於在DefaultRulesEngine的基礎上加了一個循環。還是以開頭的代碼舉例,換成InferenceRulesEngine。
這時控制台將重復打印“It rains, take an umbrella!”。

public class Test {
    public static void main(String[] args) {
		    // define rules 
				Rule weatherRule = new RuleBuilder()
        .name("weather rule")
        .description("if it rains then take an umbrella")
        .when(facts -> facts.get("rain").equals(true))
        .then(facts -> System.out.println("It rains, take an umbrella!"))
        .build();
        Rules rules = new Rules();
        rules.register(weatherRule);
		
        // define facts
        Facts facts = new Facts();
        facts.put("rain", true);

        // fire rules on known facts
        RulesEngine rulesEngine = new InferenceRulesEngine();
        rulesEngine.fire(rules, facts);
    }
}

2 Engine的配置參數

Engine支持以下幾個參數配置:

范圍 類型 必需的 默認
rulePriorityThreshold int no MaxInt
skipOnFirstAppliedRule boolean no false
skipOnFirstFailedRule boolean no false
skipOnFirstNonTriggeredRule boolean no false
  • skipOnFirstAppliedRule:在應用規則時跳過下一個規則。
  • skipOnFirstFailedRule:在規則失敗時跳過下一個規則。
  • skipOnFirstNonTriggeredRule:在未觸發規則時跳過下一個規則。
  • rulePriorityThreshold:priority 大於rulePriorityThreshold時跳過下一個規則。

寫法如下:

RulesEngineParameters parameters = new RulesEngineParameters()
    .rulePriorityThreshold(10)
    .skipOnFirstAppliedRule(true)
    .skipOnFirstFailedRule(true)
    .skipOnFirstNonTriggeredRule(true);

RulesEngine rulesEngine = new DefaultRulesEngine(parameters);

本文由博客一文多發平台 OpenWrite 發布!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM