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

规则引擎 - Easy Rules

Easy Rules

      • 依赖
      • demo
        • demo1
        • demo2
      • 总结

Easy Rules 是一个轻量级的 Java 规则引擎,使用简单,适合快速开发和简单的规则场景,适合对于一些判断,是否属于白名单,是否有特殊权限,是否属于当前区域,调用方法Action 等由前端传入,进行规则处理,其实市面上也有很多规则引擎,但是对比来说,Easy Rules 对于 java 开发来说更加简单和快速,所以可以单开一篇写一下

依赖

<dependency><groupId>org.jeasy</groupId><artifactId>easy-rules-core</artifactId><version>4.4.0</version>
</dependency>

demo

demo1
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.core.RuleBuilder;
/*** FileName: FactsExample.java* Author:   寿春* Date:     2025/4/21 19:54* <p>*/
class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;}
}public class FactsExample {public static void main(String[] args) {// 创建 Person 对象Person person = new Person("John", 20);// 使用流式 API 定义规则var ageRule = RuleBuilder.begin().name("Age Rule").description("Check if the person's age is greater than 18").when(facts -> ((Person) facts.get("person")).getAge() > 18).then(facts -> System.out.println(((Person) facts.get("person")).getName() + " is an adult.")).build();// 创建规则集合Rules rules = new Rules();rules.register(ageRule);// 创建事实集合Facts facts = new Facts();// 向事实集合中添加数据facts.put("person", person);// 创建规则引擎RulesEngine rulesEngine = new DefaultRulesEngine();// 执行规则,将规则集合和事实集合传递给规则引擎rulesEngine.fire(rules, facts);}
}    
demo2
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Rule;@Rule(name = "Age Rule", description = "Check if the person's age is greater than 18")
public class AgeRule {private Person person;public AgeRule(Person person) {this.person = person;}@Conditionpublic boolean isAdult() {return person.getAge() > 18;}@Actionpublic void printMessage() {System.out.println(person.getName() + " is an adult.");}
}    

/*** FileName: FactsExample.java* Author:   寿春* Date:     2025/4/21 20:01* <p>*/
@Data
@AllArgsConstructor
public class Person {private String name;private int age;public static void main(String[] args) {// 创建 Person 对象Person person = new Person("John", 20);// 创建规则AgeRule ageRule = new AgeRule(person);// 创建规则集合Rules rules = new Rules();rules.register(ageRule);// 创建事实集合Facts facts = new Facts();facts.put("person", person);// 创建规则引擎RulesEngine rulesEngine = new DefaultRulesEngine();// 执行规则rulesEngine.fire(rules, facts);}
}    

总结

逻辑很简单

  1. 创建一个规则, Rules rules = new Rules();
  2. 需要什么规则 RuleBuilder
  3. 创建一个事实 Facts facts = new Facts();
  4. 创建规则引擎 RulesEngine rulesEngine = new DefaultRulesEngine();
  5. 执行规则 rulesEngine.fire(rules, facts); 这个规则 这个事实
http://www.xdnf.cn/news/59815.html

相关文章:

  • 使用openssl为localhost创建自签名
  • 运维概述(linux 系统)
  • 【Python核心库实战指南】从数据处理到Web开发
  • MCP + 数据库,一种比 RAG 检索效果更好的新方式!
  • Python+CoppeliaSim+ZMQ remote API控制机器人跳舞
  • 【c语言】指针进阶
  • 2025年首届人形机器人半程马拉松比赛(附机器人照片)
  • [BJDCTF2020]EzPHP
  • JVM有什么调优参数?
  • RSS 2025|苏黎世提出「LLM-MPC混合架构」增强自动驾驶,推理速度提升10.5倍!
  • 阿里云人工智能大模型MCP协议
  • Node.js和js到底什么关系
  • Hyperlane:Rust Web框架的性能新标杆
  • Vue如何获取Dom
  • Oracle DBA培训一般多长时间?
  • 递归的模板 (以反转链表为例)
  • .net core 中directory , directoryinfo ,file, fileinfo区别,联系,场景
  • mindspeed-rl使用注意事项
  • unity TEngine学习4
  • 驱动开发硬核特训 · Day 16:字符设备驱动模型与实战注册流程
  • AIWS全链路开发与MCP框架下的高可用服务架构设计
  • AI问答Bug修改
  • 教育行业网络安全:守护学校终端安全,筑牢教育行业网络安全防线!
  • 数据结构——栈和队列
  • Debian GNU/Linux的新手入门介绍
  • 动态规划-零钱兑换
  • MCP 框架中,stdio 模式和 SSE(Server-Sent Events) 模式的区别是什么
  • 01_Flask快速入门教程介绍
  • 楼宇自控怎样推动能源高效利用与建筑设备的科学管理
  • 玩转Docker | 使用Docker部署nullboard任务管理工具