av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java輕量級(jí)規(guī)則引擎easy-rules使用介紹

瀏覽:2日期:2022-08-31 08:59:11

輕量級(jí)規(guī)則引擎easy-rules--參考

我們?cè)趯?xiě)業(yè)務(wù)代碼經(jīng)常遇到需要一大堆if/else,會(huì)導(dǎo)致代碼可讀性大大降低,有沒(méi)有一種方法可以避免代碼中出現(xiàn)大量的判斷語(yǔ)句呢?答案是用規(guī)則引擎,但是傳統(tǒng)的規(guī)則引擎都比較重,比如開(kāi)源的Drools,不適合在小需求中應(yīng)用。最近在github上面看到一個(gè)傻瓜式的Java規(guī)則引擎Easy-Rules,這里結(jié)合自己寫(xiě)的demo介紹如何使用這個(gè)規(guī)則引擎,希望對(duì)大家有所幫助。

easy-rules的特點(diǎn)

輕量級(jí)類(lèi)庫(kù)和容易上手 基于POJO的開(kāi)發(fā)與注解的編程模型 基于MVEL表達(dá)式的編程模型(適用于極簡(jiǎn)單的規(guī)則,一般不推薦) 支持根據(jù)簡(jiǎn)單的規(guī)則創(chuàng)建組合規(guī)則 方便且適用于java的抽象的業(yè)務(wù)模型規(guī)則

它主要包括幾個(gè)主要的類(lèi)或接口:Rule,RulesEngine,RuleListener,Facts還有幾個(gè)主要的注解:@Action,@Condition,@Fact,@Priority,@Rule

例1:基于POJO開(kāi)發(fā)與注解的編程模型:判斷1-50中,被3或者8整除的數(shù)

首先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>

編寫(xiě)規(guī)則POJO:

規(guī)則1

@Rule(name = '被3整除', description = 'number如果被3整除,打印:number is three')public class ThreeRule { /** * Condition:條件判斷注解:如果return true, 執(zhí)行Action * * @param number * @return */ @Condition public boolean isThree(@Fact('number') int number) { return number % 3 == 0; } /** * Action 執(zhí)行方法注解 * * @param number */ @Action public void threeAction(@Fact('number') int number) { System.out.println(number + ' is three'); } /** * Priority:優(yōu)先級(jí)注解:return 數(shù)值越小,優(yōu)先級(jí)越高 * * @return */ @Priority public int getPriority() { return 1; }}

規(guī)則2

@Rule(name = '被8整除')public class EightRule { /** * 條件 * * @param number * @return */ @Condition public boolean isEight(@Fact('number') int number) { return number % 8 == 0; } /** * 滿足條件的動(dòng)作 * * @param number */ @Action public void eightAction(@Fact('number') int number) { System.out.println(number + ' is eight'); } /** * 條件判斷的優(yōu)先級(jí) * * @return */ @Priority public int getPriority() { return 2; }}

規(guī)則3(組合規(guī)則-同時(shí)執(zhí)行)

@Rule(name = '被3和8同時(shí)整除', description = '這是一個(gè)組合規(guī)則')public class ThreeEightRuleUnitGroup extends UnitRuleGroup { public ThreeEightRuleUnitGroup(Object... rules) { for (Object rule : rules) { addRule(rule); } } @Override public int getPriority() { return 0; }}

規(guī)則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; }}

執(zhí)行規(guī)則

public class ThreeEightRuleLauncher { public static void main(String[] args) { /** * 創(chuàng)建規(guī)則執(zhí)行引擎 * 注意: skipOnFirstAppliedRule意思是,只要匹配到第一條規(guī)則就跳過(guò)后面規(guī)則匹配 */ RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true); RulesEngine rulesEngine = new DefaultRulesEngine(parameters); //創(chuàng)建規(guī)則 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++){ //規(guī)則因素,對(duì)應(yīng)的name,要和規(guī)則里面的@Fact 一致 facts.put('number', i); //執(zhí)行規(guī)則 rulesEngine.fire(rules, facts); System.out.println(); } }}

例2:基于MVEL表達(dá)式的編程模型

本例演示如何使用MVEL表達(dá)式定義規(guī)則,MVEL通過(guò)Easy-Rules MVEL模塊提供。此模塊包含使用MVEL定義規(guī)則的API。我們將在這里使用這些API,其目標(biāo)是實(shí)現(xiàn)一個(gè)簡(jiǎn)單的商店應(yīng)用程序,要求如下:禁止兒童購(gòu)買(mǎi)酒精,成年人的最低法定年齡為18歲。 商店顧客由Person類(lèi)定義:

@Data@AllArgsConstructor@NoArgsConstructorpublic 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; }}

我們定義兩個(gè)規(guī)則:

規(guī)則1:可以更新Person實(shí)例,判斷年齡是否大于18歲,并設(shè)置成人標(biāo)志。 規(guī)則2:判斷此人是否為成年人,并拒絕兒童(即非成年人)購(gòu)買(mǎi)酒精。

顯然,規(guī)則1的優(yōu)先級(jí)要大于規(guī)則2,我們可以設(shè)置規(guī)則1的Priority為1,規(guī)則2的Priority為2,這樣保證規(guī)則引擎在執(zhí)行規(guī)則的時(shí)候,按優(yōu)先級(jí)的順序執(zhí)行規(guī)則。

規(guī)則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);');

規(guī)則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');'

執(zhí)行規(guī)則

public class ShopLauncher { public static void main(String[] args) throws Exception { //創(chuàng)建一個(gè)Person實(shí)例(Fact) Person tom = new Person('Tom', 19); Facts facts = new Facts(); facts.put('person', tom); //創(chuàng)建規(guī)則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);'); //創(chuàng)建規(guī)則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); //創(chuàng)建規(guī)則執(zhí)行引擎,并執(zhí)行規(guī)則 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)); }}

執(zhí)行結(jié)果如下:

java輕量級(jí)規(guī)則引擎easy-rules使用介紹

本篇主要介紹easy-rules的使用

深入了解原理,可以查看github源碼:https://github.com/j-easy/easy-rules

到此這篇關(guān)于java輕量級(jí)規(guī)則引擎easy-rules使用介紹的文章就介紹到這了,更多相關(guān)java easy-rules內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产精品1区2区3区 中文字幕一区二区三区四区 | 久草a√ | 日本成人午夜影院 | 成人免费毛片在线观看 | 久久在线精品 | 成人欧美一区二区 | 四虎网站在线观看 | 午夜精品网站 | 在线第一页 | 久久久久久成人 | 成人av鲁丝片一区二区小说 | 网站国产 | 欧美日一区 | 婷婷色国产偷v国产偷v小说 | 91网在线观看 | 高清久久 | 欧日韩在线观看 | 四虎影院免费在线 | 成人啊啊啊 | 中文字幕av网址 | 欧美精品一区三区 | 一区二区三区中文字幕 | 国产一区欧美 | 国产精品久久久久久妇女 | 欧美国产日韩在线观看成人 | av一区二区三区在线观看 | 精品视频在线观看 | 日韩一区二区福利 | 日韩欧美在线免费观看视频 | 亚洲欧美国产毛片在线 | 亚洲一区视频在线 | 亚洲中国字幕 | 久久97精品 | 一区二区三区视频 | 日本三级日产三级国产三级 | 欧美日韩精品一区二区三区四区 | 日日日日日日bbbbb视频 | 国产一区二区久久久 | 99热在线免费 | 亚洲精品久久久久avwww潮水 | 老司机深夜福利网站 |