当前位置: 首页 > 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); 这个规则 这个事实

相关文章:

  • Spark和Hadoop的区别和联系
  • 【AI提示词】数据分析专家
  • 系统安全及应用
  • 一个关于相对速度的假想的故事-3
  • Linux 入门十一:Linux 网络编程
  • PyCharm 在 Linux 上的完整安装与使用指南
  • arxml文件中的schema是什么?有什么作用?
  • Kafka 在小流量和大流量场景下的顺序消费问题
  • typedef MVS_API CLISTDEF0IDX(ViewScore, IIndex) ViewScoreArr;
  • Vue3 源码解析(六):响应式原理与 reactive
  • DePIN驱动的分布式AI资源网络
  • Python 爬虫如何获取淘宝商品的 SKU 详细信息
  • 云服务器怎么选择防御最合适
  • 深度学习中的归一化技术:从原理到实战全解析
  • 使用 Logstash 迁移 MongoDB 数据到 Easysearch
  • C语言中联合体(Union)和结构体(Struct)的嵌套用法
  • Unity打开项目时目标平台被改变
  • 新能源汽车充电桩运营模式的发展与优化路径探析
  • 【Hive入门】Hive概述:大数据时代的数据仓库桥梁
  • 【KWDB 创作者计划】_本地化部署与使用KWDB 深度实践
  • 世界读书日丨这50本书,商务印书馆推荐给教师
  • “代课老师被男友杀害案”一审开庭,将择期宣判
  • 《蛮好的人生》上海特色鲜明,聚焦荧屏甚少出现的保险业
  • 俄官员称乌克兰未遵守停火,乌方暂无回应
  • 马拉松夺冠机器人将小批量量产:价格与一台入门级小轿车差不多
  • 大悦城:去年净亏损29.77亿元,三年累计亏损超70亿元