Java規則引擎-Easy Rules使用
技術標籤:規則引擎Easy Rules規則引擎Easy Rules的使用Java開源規則引擎
Java規則引擎-Easy Rules使用
規則引擎由推理引擎發展而來,是一種嵌入在應用程式中的元件,實現了將業務決策從應用程式程式碼中分離出來,並使用預定義的語義模組編寫業務決策。接受資料輸入,解釋業務規則,並根據業務規則做出業務決策。
開源的規則引擎有很多,像Drools,本文中用到的Easy Rules,Apache Camel,等等。
Easy Rules 介紹
Easy Rules是Java規則引擎,其靈感來自名為“我應該使用規則引擎?”的文章。 馬丁·福勒(Martin Fowler)在其中說:
您可以自己構建一個簡單的規則引擎。 您所需要做的就是建立一堆帶有條件和動作的物件,
將它們儲存在一個集合中,然後遍歷它們以評估條件並執行這些動作。
這正是Easy Rules所做的,它提供Rule抽象以建立帶有條件和動作的規則,並提供RuleEngine API,該API通過一組規則執行以評估條件和執行動作。
github地址: https://github.com/j-easy/easy-rules
Easy Rules特性
- 輕量級庫和易於學習的API
- 基於POJO的開發與註釋程式設計模型
- 有用的抽象定義業務規則並通過Java輕鬆應用
- 從原始規則建立複合規則的能力
- 使用表示式語言(如MVEL,SpEL和JEXL)定義規則的能力
Easy Rules的使用
使用註解的方式進行申明規則和使用
建立一個maven專案,專案名為:easy-rules-example,結構如下:
在pom.xml中引入相關的包,如下:
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</ artifactId>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-support</artifactId>
</dependency>
建立一個OperationRules.java,程式碼如下:
/**
* 定義一個相關的規則
*
* @author tony
* @date 2021/1/14 10:28
*/
//註釋以將類標記為規則
@Rule(name = "Operation rule", description = "根據不同的操作進行處理相關事務")
public class OperationRules {
//將方法標記為規則條件的註釋。
//必須註釋任何不帶引數的公共方法,並且該方法返回布林值。
//@Fact註解:將引數標記為事實的註釋。 比對operation這個屬性值是否為true,為true將進行action的操作
@Condition
public boolean operation(@Fact("operation") boolean operation) {
return operation;
}
//將方法標記為規則操作的註釋。
//必須註釋任何不帶引數的公共方法。 該方法的返回值將被引擎忽略。
//註解中的order屬性定義action的順序
@Action
public void operationDesc() {
System.out.println("當操作為true的時候會使用進來");
}
}
建立一個測試類:TestQuickStart.java,程式碼如下:
public static void main(String[] args) {
//定義相應的facts 一組事實並表示一個事實名稱空間。事實在事實物件中具有唯一的名稱。
Facts facts = new Facts();
//看相關的原始碼,是一個set集合儲存fact,這裡是需要全域性唯一的name
facts.put("operation", false);
//此類封裝了一組規則並表示規則名稱空間。 規則在規則名稱空間中必須具有唯一的名稱。
//規則將基於Comparable.compareTo(Object)方法相互比較,因此Rule的實現應正確
//實現compareTo以確保單個名稱空間中的唯一規則名稱。
Rules rules = new Rules();
//register方法註冊一個或多個新規則
rules.register(new OperationRules());
//申明一個規則引擎
RulesEngine engine = new DefaultRulesEngine();
engine.fire(rules, facts);
}
執行上面的程式碼,將facts中operation設定不同的值,將會有不同的結果。
使用MVL表示式進行規則的定義
在pom.xml中引入相關的包,如下:
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-mvel</artifactId>
</dependency>
建立名為:OperationMVLExpression.java,程式碼如下:
/**
* 使用mvl 表示式進行操作
*
* @author tony
* @date 2021/1/14 11:31
*/
public class OperationMVLExpression {
public static Rule operationRule() {
return new MVELRule()
.name("operation rule")
.description("這是一個規則描述")
.when("operation == true")
.then("System.out.println(\"進行了相關操作\");");
}
}
建立一個測試類:TestOperationMVLExpression.java,程式碼如下:
public static void main(String[] args) {
Facts facts = new Facts();
facts.put("operation", false);
Rules rules = new Rules();
rules.register(OperationMVLExpression.operationRule());
RulesEngine engine = new DefaultRulesEngine();
engine.fire(rules, facts);
}
執行程式進行驗證即可
使用fluent進行建立使用規則
在pom.xml中引入相關的包,如下:
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-support</artifactId>
</dependency>
建立一個類:OperationRuleFluent.java,程式碼如下:
/**
* 使用fluent進行操作
*
* @author tony
* @date 2021/1/14 11:23
*/
public class OperationRuleFluent {
public static Rule operationRule() {
//生成器來建立規則例項
return new RuleBuilder()
.name("operation rule")
.description("這是一個規則描述")
.when(facts -> facts.get("operation").equals(true))
.then(facts -> System.out.println("進行了相關操作"))
.build();
}
}
建立一個測試類:TestOperationRuleFluent.java,程式碼如下:
public static void main(String[] args) {
Facts facts = new Facts();
facts.put("operation", true);
//定義一個規則
Rules rules = new Rules();
rules.register(OperationRuleFluent.operationRule());
//申明一個規則引擎
RulesEngine engine = new DefaultRulesEngine();
engine.fire(rules, facts);
}
載入yml進行配置規則
建立一個yml檔案:operation-rule.yml
name: "operation rule"
description: "這是規則的描述"
condition: "operation == true"
actions:
- "System.out.println(\"進行了相關的操作\");"
進行載入檔案,進行定義規則:
public static Rule operation() throws Exception {
MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
return ruleFactory.createRule(new FileReader("operation-rule.yml"));
}
建立一個測試類:TestOperationYml.java,程式碼如下:
public static void main(String[] args) throws Exception {
Facts facts = new Facts();
facts.put("operation", false);
Rules rules = new Rules();
rules.register(OperationRuleYmlFile.operation());
RulesEngine engine = new DefaultRulesEngine();
engine.fire(rules, facts);
}
上面是Easy Rules使用的幾種方式,有已進行多action的定義,規則滿足的時候可以同事進行多個操作。還提供相關的監聽機制,在規則觸發之前和之後執行什麼操作等,感覺使用起來還是比較簡單你,大家可以試試,一起學習!