java輕量級規則引擎easy-rules使用介紹
輕量級規則引擎easy-rules--參考
我們在寫業務程式碼經常遇到需要一大堆if/else,會導致程式碼可讀性大大降低,有沒有一種方法可以避免程式碼中出現大量的判斷語句呢?答案是用規則引擎,但是傳統的規則引擎都比較重,比如開源的Drools,不適合在小需求中應用。最近在github上面看到一個傻瓜式的Java規則引擎Easy-Rules,這裡結合自己寫的demo介紹如何使用這個規則引擎,希望對大家有所幫助。
easy-rules的特點
- 輕量級類庫和容易上手
- 基於POJO的開發與註解的程式設計模型
- 基於MVEL表示式的程式設計模型(適用於極簡單的規則,一般不推薦)
- 支援根據簡單的規則建立組合規則
- 方便且適用於java的抽象的業務模型規則
它主要包括幾個主要的類或介面:Rule,RulesEngine,RuleListener,Facts還有幾個主要的註解:@Action,@Condition,@Fact,@Priority,@Rule
例1:基於POJO開發與註解的程式設計模型:判斷1-50中,被3或者8整除的數
首先maven 引入easy-rules
<dependency> <groupId>org.jeasy</groupId> <artifactId>easy-rules-core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.jeasy</groupId> <artifactId>easy-rules-mvel</artifactId> <version>3.3.0</version> </dependency>
編寫規則POJO:
規則1
@Rule(name = "被3整除",description = "number如果被3整除,列印:number is three") public class ThreeRule { /** * Condition:條件判斷註解:如果return true, 執行Action * * @param number * @return */ @Condition public boolean isThree(@Fact("number") int number) { return number % 3 == 0; } /** * Action 執行方法註解 * * @param number */ @Action public void threeAction(@Fact("number") int number) { System.out.println(number + " is three"); } /** * Priority:優先順序註解:return 數值越小,優先順序越高 * * @return */ @Priority public int getPriority() { return 1; } }
規則2
@Rule(name = "被8整除") public class EightRule { /** * 條件 * * @param number * @return */ @Condition public boolean isEight(@Fact("number") int number) { return number % 8 == 0; } /** * 滿足條件的動作 * * @param number */ @Action public void eightAction(@Fact("number") int number) { System.out.println(number + " is eight"); } /** * 條件判斷的優先順序 * * @return */ @Priority public int getPriority() { return 2; } }
規則3(組合規則-同時執行)
@Rule(name = "被3和8同時整除",description = "這是一個組合規則") public class ThreeEightRuleUnitGroup extends UnitRuleGroup { public ThreeEightRuleUnitGroup(Object... rules) { for (Object rule : rules) { addRule(rule); } } @Override public int getPriority() { return 0; } }
規則4
@Rule(name = "既不被3整除也不被8整除",description = "列印number自己") public class OtherRule { @Condition public boolean isOther(@Fact("number") int number){ return number % 3 != 0 || number % 8 != 0; } @Action public void printSelf(@Fact("number") int number){ System.out.print(number); } @Priority public int getPriority(){ return 3; } }
執行規則
public class ThreeEightRuleLauncher { public static void main(String[] args) { /** * 建立規則執行引擎 * 注意: skipOnFirstAppliedRule意思是,只要匹配到第一條規則就跳過後面規則匹配 */ RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true); RulesEngine rulesEngine = new DefaultRulesEngine(parameters); //建立規則 Rules rules = new Rules(); rules.register(new EightRule()); rules.register(new ThreeRule()); rules.register(new ThreeEightRuleUnitGroup(new EightRule(),new ThreeRule())); rules.register(new OtherRule()); Facts facts = new Facts(); for (int i=1 ; i<=50 ; i++){ //規則因素,對應的name,要和規則裡面的@Fact 一致 facts.put("number",i); //執行規則 rulesEngine.fire(rules,facts); System.out.println(); } } }
例2:基於MVEL表示式的程式設計模型
本例演示如何使用MVEL表示式定義規則,MVEL通過Easy-Rules MVEL模組提供。此模組包含使用MVEL定義規則的API。我們將在這裡使用這些API,其目標是實現一個簡單的商店應用程式,要求如下:禁止兒童購買酒精,成年人的最低法定年齡為18歲。 商店顧客由Person類定義:
@Data @AllArgsConstructor @NoArgsConstructor public class Person { private String name; private boolean adult; private int age; //getter,setter 省略 public Person(String name,int age) { this.name = name; this.age = age; } }
我們定義兩個規則:
- 規則1:可以更新Person例項,判斷年齡是否大於18歲,並設定成人標誌。
- 規則2:判斷此人是否為成年人,並拒絕兒童(即非成年人)購買酒精。
顯然,規則1的優先順序要大於規則2,我們可以設定規則1的Priority為1,規則2的Priority為2,這樣保證規則引擎在執行規則的時候,按優先順序的順序執行規則。
規則1的定義
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);");
規則2的定義,我們放到alcohol-rule.yml檔案中
name: "alcohol rule" description: "children are not allowed to buy alcohol" priority: 2 condition: "person.isAdult() == false" actions: - "System.out.println(\"Shop: Sorry,you are not allowed to buy alcohol\");"
執行規則
public class ShopLauncher { public static void main(String[] args) throws Exception { //建立一個Person例項(Fact) Person tom = new Person("Tom",19); Facts facts = new Facts(); facts.put("person",tom); //建立規則1 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);"); //建立規則2 Rule alcoholRule = new MVELRuleFactory(new YamlRuleDefinitionReader()). createRule(new FileReader(ResourceUtils.getFile("classpath:alcohol-rule.yml"))); Rules rules = new Rules(); rules.register(ageRule); rules.register(alcoholRule); //建立規則執行引擎,並執行規則 RulesEngine rulesEngine = new DefaultRulesEngine(); System.out.println("Tom: Hi! can I have some Vodka please?"); rulesEngine.fire(rules,facts); System.out.println(JSON.toJSONString(tom)); } }
執行結果如下:
本篇主要介紹easy-rules的使用
深入瞭解原理,可以檢視github原始碼:https://github.com/j-easy/easy-rules
到此這篇關於java輕量級規則引擎easy-rules使用介紹的文章就介紹到這了,更多相關java easy-rules內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!