自助 建站,网络营销与传统营销的区别,网址给我一个,芜湖哪里做网站目录 一、序言二、Easy Rules介绍三、定义规则(Rules)1、规则介绍2、编程式规则定义3、声明式规则定义 四、定义事实(Facts)五、定义规则引擎(Rules Engine)1、规则引擎介绍2、InferenceRulesEngine规则引擎示例(1) 定义触发条件(2) 定义规则触发后的执行行为(3) 测试用例 一、… 目录 一、序言二、Easy Rules介绍三、定义规则(Rules)1、规则介绍2、编程式规则定义3、声明式规则定义 四、定义事实(Facts)五、定义规则引擎(Rules Engine)1、规则引擎介绍2、InferenceRulesEngine规则引擎示例(1) 定义触发条件(2) 定义规则触发后的执行行为(3) 测试用例 一、序言
最近团队在做一些Visa、Master卡的交易风控运营团队提供了一些交易风控的规则比如针对卡号MCC设置单笔交易限额24小时交易限额72小时交易限额等等还有触发风控规则是否拦截交易还是只发告警邮件等等等。
虽然写各种条件判断也能实现但是随着后面规则增加维护成本也会越来越高所以想尝试引入规则引擎同时考虑到开发和学习成本还是决定学习轻量级的Easy Rules。 二、Easy Rules介绍
Easy Rules是一个Java规则引擎它提供了规则抽象通过触发条件和触发后的行为去创建规则。还提供了规则引擎API通过这些API可以基于一系列的规则去判断规则是否触发以及触发后执行什么动作。
核心特性
轻量级Java库易于学习的API。注解式编程模型实现基于POJO开发。通过抽象定义业务规则并且轻松应用规则。支持通过简单规则可以创建组合规则。支持通过表达式语言(MVEL、SPEL和JEXL)定义规则。
相关依赖如下
!--Easy Rule--
!--核心库--dependencygroupIdorg.jeasy/groupIdartifactIdeasy-rules-core/artifactIdversion4.1.0/version/dependency!--组合规则支持--dependencygroupIdorg.jeasy/groupIdartifactIdeasy-rules-support/artifactIdversion4.1.0/version/dependency!--SPEL表达式语言支持--dependencygroupIdorg.jeasy/groupIdartifactIdeasy-rules-spel/artifactIdversion4.1.0/version
/dependency!--MVEL表达式语言支持--
dependencygroupIdorg.jeasy/groupIdartifactIdeasy-rules-mvel/artifactIdversion4.1.0/version
/dependency三、定义规则(Rules)
1、规则介绍
大多数的业务规则可以通过如下定义来描述
Name唯一的规则名称。Description简单规则描述。Priority规则执行优先级。Facts触发规则时的一系列事实。Condition给定事实后应该被满足的一系列条件。Actions条件满足时应该执行的一系列行为。
Easy Rules中的规则由Rule接口来代表如下
public interface Rule extends ComparableRule {/*** 判断规则是否应该被触发true-是false-否*/boolean evaluate(Facts facts);/*** 规则触发后执行的行为* throws Exception 执行时触发的异常*/void execute(Facts facts) throws Exception;
}2、编程式规则定义
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;/*** 编程式规则定义* author Nick Liu* date 2023/8/3*/
public class ProgrammaticHelloWorldRule implements Rule {Overridepublic boolean evaluate(Facts facts) {return facts.get(enabled);}Overridepublic void execute(Facts facts) throws Exception {System.out.println(Hello World);}Overridepublic int compareTo(Rule o) {return 0;}public static void main(String[] args) {// 定义事实Facts facts new Facts();facts.put(enabled, true);// 注册编程式规则Rules rules new Rules();rules.register(new ProgrammaticHelloWorldRule());// 使用默认规则引擎根据事实触发规则RulesEngine rulesEngine new DefaultRulesEngine();rulesEngine.fire(rules, facts);}
} 备注运行程序控制台会输出Hello World。 3、声明式规则定义
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;/*** 声明式规则定义* author Nick Liu* date 2023/8/3*/
Rule(name Hello world rule, description Always say hello world)
public class DeclarativeHelloWorldRule {Conditionpublic boolean when(Fact(enabled) boolean enabled) {return enabled;}Action(order 1)public void then(Fact(enabled) boolean enabled) throws Exception {System.out.println(Hello World);}Action(order 2)public void finalAction(Facts facts) throws Exception {System.out.println(Final Hello World);}public static void main(String[] args) {Facts facts new Facts();facts.put(enabled, true);Rules rules new Rules();rules.register(new DeclarativeHelloWorldRule());RulesEngine rulesEngine new DefaultRulesEngine();rulesEngine.fire(rules, facts);}
}控制台运行结果如下
Hello World
Final Hello World四、定义事实(Facts)
在Easy Rules中事实由Fact类来定义如下
public class FactT {private final String name;private final T value;
}事实有name和value两个属性两者都不能为空且name属性值充当命名空间的角色需要唯一。
下面是定义事实的例子
第1种方式
FactString fact new Fact(foo, bar);
Facts facts new Facts();
facts.add(fact);第2种方式
Facts facts new Facts();
facts.put(foo, bar);备注两者方式都定义了一个name为foovalue为bar的事实实例第二种方式更加简洁。 五、定义规则引擎(Rules Engine)
1、规则引擎介绍
Easy Rules提供了两种规则引擎的实现
DefaultRulesEngine默认规则引擎根据规则的自然顺序默认为优先级应用规则。InferenceRulesEngine推理规则引擎持续性应用单条规则直到规则触发条件不满足。
Easy Rules规则引擎支持下面参数配置
参数名称参数类型必选默认值rulePriorityThresholdint否Integer.MAX_VALUEskipOnFirstAppliedRuleboolean否falseskipOnFirstFailedRuleboolean否falseskipOnFirstNonTriggeredRuleboolean否false
skipOnFirstAppliedRule: 当规则被触发并且成功执行行为后是否跳过下条规则。skipOnFirstFailedRule : 当判断规则是否触发抛出异常或者触发成功但行为执行后抛出异常是否跳过下条规则。skipOnFirstNonTriggeredRule : 当规则未被触发是否跳过下条规则。rulePriorityThreshold : 如果规则优先级超过默认阈值则跳过下条规则。
参数配置示例如下
RulesEngineParameters parameters new RulesEngineParameters().rulePriorityThreshold(10).skipOnFirstAppliedRule(true).skipOnFirstFailedRule(true).skipOnFirstNonTriggeredRule(true);RulesEngine rulesEngine new DefaultRulesEngine(parameters);通过下面的代码可以获取规则引擎参数
RulesEngineParameters parameters myEngine.getParameters();2、InferenceRulesEngine规则引擎示例
DefaultRulesEngine默认规则引擎的使用示例前面已经有提到过下面我们看下InferenceRulesEngine推理规则引擎的代码示例。
(1) 定义触发条件
import org.jeasy.rules.api.Condition;
import org.jeasy.rules.api.Facts;/*** author Nick Liu* date 2023/8/5*/
public class HighTemperatureCondition implements Condition {Overridepublic boolean evaluate(Facts facts) {int temperature facts.get(temperature);return temperature 25;}
}(2) 定义规则触发后的执行行为
import org.jeasy.rules.api.Action;
import org.jeasy.rules.api.Facts;/*** author Nick Liu* date 2023/8/5*/
public class DecreaseTemperatureAction implements Action {Overridepublic void execute(Facts facts) throws Exception {int temperature facts.get(temperature);System.out.printf(Current temperature: %d, Its hot! cooling air...%n, temperature);facts.put(temperature, temperature - 1);}
}(3) 测试用例
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.InferenceRulesEngine;
import org.jeasy.rules.core.RuleBuilder;/*** author Nick Liu* date 2023/8/5*/
public class AirConditionLauncher {public static void main(String[] args) {Facts facts new Facts();facts.put(temperature, 30);// 通过规则构建API定义规则Rule rule new RuleBuilder().name(Air Condition Rule).when(new HighTemperatureCondition()).then(new DecreaseTemperatureAction()).build();Rules rules new Rules();rules.register(rule);// 基于事实重复应用规则的推理规则引擎直到规则不再满足RulesEngine rulesEngine new InferenceRulesEngine();rulesEngine.fire(rules, facts);}
}控制台输出结果如下
Current temperature: 30, Its hot! cooling air...
Current temperature: 29, Its hot! cooling air...
Current temperature: 28, Its hot! cooling air...
Current temperature: 27, Its hot! cooling air...
Current temperature: 26, Its hot! cooling air...备注可以看到定义的规则会持续触发直到temperature的值为25。 文章转载自: http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.fnczn.cn.gov.cn.fnczn.cn http://www.morning.qrpx.cn.gov.cn.qrpx.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.ygkq.cn.gov.cn.ygkq.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.qrwnj.cn.gov.cn.qrwnj.cn http://www.morning.dswtz.cn.gov.cn.dswtz.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.fylqz.cn.gov.cn.fylqz.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.snlxb.cn.gov.cn.snlxb.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.cfynn.cn.gov.cn.cfynn.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.xmhpq.cn.gov.cn.xmhpq.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.spnky.cn.gov.cn.spnky.cn