当前位置: 首页 > news >正文

规则引擎----easy rules

一、规则引擎的作用

将复杂的if else判断剥离出来

二、使用

2.1、引入POM

		<!--easy rules核心库--><dependency><groupId>org.jeasy</groupId><artifactId>easy-rules-core</artifactId><version>3.3.0</version></dependency><!--规则定义文件格式,支持json,yaml等--><dependency><groupId>org.jeasy</groupId><artifactId>easy-rules-support</artifactId><version>3.3.0</version></dependency><!--支持mvel规则语法库--><dependency><groupId>org.jeasy</groupId><artifactId>easy-rules-mvel</artifactId><version>3.3.0</version>

2.2、编写规则

2.2.1、注解

package com.xx.rule.number;import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;/*** @author aqi* @date 2023/5/6 16:03*/
@Rule(name = "被3整除", description = "number如果被3整除,打印:number is 3", priority = 1)
public class ThreeRule {/*** Condition:条件判断注解:如果return true, 执行Action*/@Conditionpublic boolean isThree(@Fact("number") int number) {return number % 3 == 0;}/*** Action 执行方法注解*/@Actionpublic void threeAction(@Fact("number") int number) {System.out.println(number + ":可以被3整除");}
}

2.2.2、表达式

MVELRule ageRule = new MVELRule().name("age rule").description("判断这个人是否成年,是否可以饮酒").priority(1).when("person.age > 18").then("person.setAdult(true);");

2.2.3 yml配置文件

name: "alcohol rule"
description: "children are not allowed to buy alcohol"
priority: 2
condition: "person.isAdult() == false"
actions:- "System.out.println(\"你还未满18岁!\");"

2.2.4 组合规则

UnitRuleGroup myUnitRuleGroup = new UnitRuleGroup("myUnitRuleGroup", "组合规则");myUnitRuleGroup.addRule(ageRule);myUnitRuleGroup.addRule(alcoholRule);

2.2.5 组合规则说明

说明
UnitRuleGroup要么应用所有规则,要么不应用任何规则(AND逻辑)
ActivationRuleGroup它触发第一个适用规则,并忽略组中的其他规则(XOR逻辑)
ConditionalRuleGroup如果具有最高优先级的规则计算结果为true,则触发其余规则

2.2.6 字段说明

字段说明
name规则名称
description规则描述
priority规则执行顺序,越小越先执行
condition条件
actions满足条件之后的执行动作

2.3、规则使用

2.3.1、注解规则使用

package com.xx;import com.xx.rule.number.ThreeRule;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;public class Main {public static void main(String[] args) {// 创建规则执行引擎RulesEngine rulesEngine = new DefaultRulesEngine();// 创建规则Rules rules = new Rules();rules.register(new ThreeRule());// 设置参数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.3.2、表达式和yml规则使用

package com.xx;import com.xx.domain.Person;
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;
import org.jeasy.rules.core.RuleBuilder;
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.jeasy.rules.support.YamlRuleDefinitionReader;
import org.springframework.util.ResourceUtils;import java.io.FileReader;/*** 基于MVEL表达式实现规则引擎* @author aqi*/
public class ShopMain {public static void main(String[] args) throws Exception {Person person = new Person();person.setName("tom");person.setAge(20);Facts facts = new Facts();facts.put("person", person);// 表达式Rule ageRule = new RuleBuilder().name("age rule").description("判断这个人是否成年,是否可以饮酒").when(e -> Integer.parseInt(e.get("person.age")) > 18).then(e -> e.put("person.adult", true)).build();// yml配置文件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();rulesEngine.fire(rules, facts);System.out.println(person);}
}

2.3.1、规则引擎可配参数

参数说明
skipOnFirstAppliedRule当一个规则成功应用时,跳过余下的规则
skipOnFirstFailedRule当一个规则失败时,跳过余下的规则
skipOnFirstNonTriggeredRule当一个规则未触发时,跳过余下的规则
rulePriorityThreshold当优先级超过指定的阈值时,跳过余下的规则

通过如下方式设置

RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true);RulesEngine rulesEngine = new DefaultRulesEngine(parameters);
http://www.lryc.cn/news/65011.html

相关文章:

  • 你手写过一把锁吗?你对轮询缓存怎么看?
  • 深入理解 spring-boot-starter-parent
  • 基于SpringBoot的线上日志阅读器
  • 【Leetcode -405.数字转换为十六进制数 - 409.最长回文串】
  • 剑指 Offer:003 前 n 个数字二进制中 1 的个数
  • DDD系列:二、应用架构设计演变
  • Spring-IOC
  • 基于Java语言开发B/S架构实现的云HIS
  • 清洁赛道新势力,米博凭“减法”突围?
  • 代码随想录训练营Day6| 242、349、202、1
  • IP-GUARD如何通过网络控制策略禁止应用程序联网?
  • Java RSA密钥转换,从RSAPrivateKey得到RSAPublicKey
  • Android 12.0 Launcher3仿ios长按app图标实现抖动动画开始拖拽停止动画
  • 【五一创作】50道Java面试题
  • 4。计算机组成原理(3)指令系统
  • 【Elasticsearch】NLP简单应用
  • 3. 云计算的落地实践(下)
  • javaEE+mysql学生竞赛管理系统
  • 车辆出险记录查询API接口
  • MySQL的概念,编译及安装
  • 系统性能压力测试
  • 从零开始学习Linux运维,成为IT领域翘楚(三)
  • 轻松搭建自己的ChatGPT聊天机器人,让AI陪你聊天!
  • CompletableFutrue异步处理
  • 【前端面经】JS-对象的可枚举性
  • 沁恒 CH32V208(三): CH32V208 Ubuntu22.04 Makefile VSCode环境配置
  • 日撸 Java 三百行day38
  • 玩转肺癌目标检测数据集Lung-PET-CT-Dx ——④转换成PASCAL VOC格式数据集
  • 两种使用 JavaScript 实现网页高亮关键字的方法
  • 【SpringBoot】SpringBoot集成ElasticSearch