# rule_engine
**Repository Path**: elfbobo_admin_admin/rule_engine
## Basic Information
- **Project Name**: rule_engine
- **Description**: Drule业务规则可视化平台。基于Drools 封装的业务规则可视化平台,用于简化规则开发,将规则表达式、规则条件和规则动作等进行可视化展示,并降低学习和开发成本,帮助用户快速创建业务规则。并在其基础之上,拓展更多功能,如规则文件的变更差异、规则对象的热加载、KieBase管理与监控、规则执行信息监控等。
- **Primary Language**: Java
- **License**: MulanPSL-2.0
- **Default Branch**: release-1.3
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 15
- **Created**: 2025-03-10
- **Last Updated**: 2025-03-10
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Drule
> 本文档持续更新
## 📚 介绍
Drule业务规则可视化平台。基于Drools 封装的业务规则可视化平台,用于简化规则开发,将规则表达式、规则条件和规则动作等进行可视化展示,并降低学习和开发成本,帮助用户快速创建业务规则。并在其基础之上,拓展更多功能,如规则文件的变更差异、规则对象的热加载、KieBase管理与监控、规则执行信息监控等。
## 🌈 体验地址
链接:[http://ahaoweb.cn/drule/visual](http://ahaoweb.cn/drule/visual),账号:test,密码:123456
使用文档链接:[https://blog.csdn.net/qq_51513626/category_12897737.html](https://blog.csdn.net/qq_51513626/category_12897737.html)
## 🪙 技术栈
Java8,Drools 7.60.0.Final
## 🛠️ 模块关系
```xml
drule-core
```
`drule-core`:drule核心包。当前版本包含 `drule-models` , `drule-compile` 和`drule-execute` 共3个模块。
- `drule-models`:对象配置模块,用于辅助配置规则对象的信息,能够自动扫描对象结构,有利于后续相关处理。
- `drule-compile`:内容编译模块,借助于 `drule-models` 生成的规则对象结构以及某些可视化结构,生成最终的DRL文件。
- `drule-execute`:执行模块,对规则内容进行编译,生成可执行的规则服务`RuleService`。
## 📖 使用说明
### 1.自动扫描对象结构
`info.cn.ahaoweb.drule.core.model.TreeNode#treeNode`,用于自动扫描对象结构,注意所扫描的Class必须`@ClassAnn` 注释。见如下测试代码`enums.cn.ahaoweb.drule.core.model.NodeTypeTest#scan`:
```java
public void scan(){
// 设置默认的配置信息
RuleModelConfiguration.setChildDepth(2);
// 扫描规则对象,转换为树形结构
TreeNode treeNode = TreeNode.treeNode(TestObj.class);
System.out.println(treeNode);
// 通过路径获取对于节点。(匹配cn.ahaoweb.drule.core.model.info.BaseInfo#path)
System.out.println(treeNode.find("testObj.treeNodes.cesu"));
}
```
### 2.可视化编译
`cn.ahaoweb.drule.core.compile.DefaultDruleCompile` 用于将可视化结构的规则块集合编译为DRL文件。
```java
public class DefaultDruleCompile{
/**
* 构建编译器
*
* @param rules 规则块集合
* @param input 入参类型的树型结构
* @param output 出参类型的树型结构
*/
public DefaultDruleCompile(List rules, TreeNode input, TreeNode output) {
}
/**
* 引入包/类
*
* @param im
*/
public void addImport(String im) {
}
// 编译方法
public DruleCompileStructure compile(){
DruleCompileStructure druleCompileStructure = new DruleCompileStructure();
// 编译规则
List ruleContents = new ArrayList<>();
for (DruleBlock rule : rules) {
ruleContents.add(compileRule(rule));
}
druleCompileStructure.setImports(this.imports);
druleCompileStructure.setGlobals(this.globals);
druleCompileStructure.setRules(ruleContents);
// 暂不支持函数,后续拓展
druleCompileStructure.setFunctions(new ArrayList<>());
return druleCompileStructure;
}
/**
* 编译规则的条件部分
* @param when
* @return
*/
protected String compileWhen(DruleBlock.When when) {
}
/**
* 编译条件组集合
* @param whenContent 条件内容
* @param outerRelated 外部关联关系
* @param group 条件组集合
*/
protected void processWhenCondition(StringBuilder whenContent, ConditionRelated outerRelated, List group) {
}
/**
* 编译规则结果部分
* @param then 规则结果部分
* @return
*/
protected String compileThen(DruleBlock.Then then) {
}
/**
* 效验 规则结构
*
* @return
*/
public ValidateResult validateRules() {
}
}
```
使用方法如下,参考`cn.ahaoweb.drule.core.compile.DefaultDruleCompileTest#compileRule`:
```java
// 配置好规则块集合
List rules = ...;
// 入参对象的树形结构
TreeNode inNode = ...;
// 出参对象的树形结构
TreeNode outNode = ...;
// 构建默认编译器
DefaultDruleCompile defaultDruleCompile = new DefaultDruleCompile(rules, inNode, outNode);
// 执行编译方法
DruleCompileStructure compile = defaultDruleCompile.compile();
// 输出最终编译的DRL内容
System.out.println(compile.toDrl());
```
### 3.构建规则执行服务
两种方式,第一种通过构造器方法`com.ahaoweb.drule.core.RuleServiceImpl`。
```java
// 构造器方法
RuleService ruleService1 = new RuleServiceImpl("id01");
RuleService ruleService2 = new RuleServiceImpl("id01","1.0.0");
RuleService ruleService3 = new RuleServiceImpl("id01","1.0.0","cn.ahaoweb");
RuleService ruleService4 = new RuleServiceImpl("id01","1.0.0","cn.ahaoweb",null);
// 构造后需要添加规则内容然后初始化
String drl = DruleUtil.getFileContentByClassPath("test.drl");
ruleService4 = ruleService4
// 需要指定类型(必要)
.addContent(RuleType.RULE_BLOCK,drl)
// 添加配置(非必要)
.addConfig(RuleServiceConfiguration.RULE_CONTENT_CACHE.PROPERTY_NAME
,RuleServiceConfiguration.RULE_CONTENT_CACHE.OPEN.value())
// 初始化(必要)
.init();
```
第二种通过`cn.ahaoweb.drule.core.DruleServiceHelper` 构造(Builder模式)。
```java
RuleService test_rule = new DruleServiceHelper()
// 唯一标识
.identifier("test_rule")
// 添加规则内容
.addContent(RuleType.RULE_BLOCK, DruleUtil.getFileContentByClassPath("test.drl"))
// 修改配置
.addConfig(RuleServiceConfiguration.RULE_CONTENT_CACHE.CLOSE)
.addConfig(RuleServiceConfiguration.EXECUTE_MODE.DEBUG)
// 获取构建好的规则服务
.getRuleService();
```
### 4.执行规则
首先需要通过 `cn.ahaoweb.drule.core.RuleService#createSession` 获取 RuleSession,配置好RuleSession之后可通过`cn.ahaoweb.drule.core.RuleSession#execute(cn.ahaoweb.drule.core.FactInfo)`方法执行规则。
```java
// 获取规则内容
String drl = DruleUtil.getFileContentByClassPath("test.drl");
// 构建规则服务
RuleService ruleService = new DruleServiceHelper()
// 唯一标识
.identifier("test_rule")
// 添加规则内容
.addContent(RuleType.RULE_BLOCK,drl)
// 修改配置
.addConfig(RuleServiceConfiguration.RULE_CONTENT_CACHE.CLOSE)
.addConfig(RuleServiceConfiguration.EXECUTE_MODE.DEBUG)
// 获取构建好的规则服务
.getRuleService();
// 通过 RuleService 创建规则会话
RuleSession ruleSession = ruleService.createSession()
// 添加前置处理器
.addPreExecuteHandler(new PreExecuteHandler() {
@Override
public void preHandler(FactInfo factInfo) {
TestIn input = factInfo.getInput();
input.setAge(10);
System.out.println("前置处理器:"+input);
}
})
// 添加后置处理器
.addPostExecuteHandler(new PostExecuteHandler() {
@Override
public void postHandler(FactInfo factInfo) {
TestOut output = (TestOut) factInfo.getOutput();
System.out.println("后置处理器:"+output);
}
});
// ---------------- 执行方式1: execute(FactInfo factInfo)---------------------
// 构建事实信息
FactInfo factInfo = FactInfo.FactInfoBuilder.builder()
.input(new TestIn())
.inputClass(TestIn.class)
.inputName("testIn")
.outputClass(TestOut.class)
.build();
// 通过规则会话执行事实信息
ruleSession.execute(factInfo);
// 输出事实信息
System.out.println(factInfo);
// ---------------- 执行方式2:execute(Object in, Object out) ---------------------
TestIn input = new TestIn();
input.setName("1");
// 通过入参和出参对象,调用执行方法
FactInfo execute2 = ruleSession.execute(input,new TestOut());
// 输出事实信息
System.out.println(execute2);
```
实际执行方法为`cn.ahaoweb.drule.core.RuleSessionImpl#doExecute`,其大致流程如下:
1. cn.ahaoweb.drule.core.RuleSessionImpl#enter(可执行拓展)
2. 执行前置处理器 cn.ahaoweb.drule.core.RuleSessionImpl#preExecuteHandlers
3. cn.ahaoweb.drule.core.RuleSessionImpl#afterPreHandlers(可执行拓展)
4. 添加规则执行监听器 cn.ahaoweb.drule.core.RuleSessionImpl#listeners,并真正执行规则
5. cn.ahaoweb.drule.core.RuleSessionImpl#afterExecuted(可执行拓展)
6. 执行前置处理器 cn.ahaoweb.drule.core.RuleSessionImpl#postExecuteHandlers
7. cn.ahaoweb.drule.core.RuleSessionImpl#afterPostHandlers(可执行拓展)
8. 销毁会话 cn.ahaoweb.drule.core.KieSessionWrapper#destroy
9. cn.ahaoweb.drule.core.RuleSessionImpl#exit(可执行拓展)
### 5.规则内容差异对比
转译规则内容,目前仅支持将规则内容进行转译为中文。
```java
// 构建中文转移器
ChineseTranslation chineseTranslation = DruleChineseTranslation.buildTranslation(rules, treeNode, treeNode);
// 方式一:转译规则内容结构体集合
List translatedRuleTexts = chineseTranslation.translateRules();
// 方式二:转译后的规则文本内容
String translatedRuleTexts = chineseTranslation.generateText();
```
规则内容差异对比工具。
```java
// 需要对比的内容
List old_content = ...
List new_content = ...
// 执行对比方法
List list = DiffHandleUtils.diffList(old_content, new_content, "原内容","新内容");
// 如需,将差异内容可视化显示,可使用该方法生成一个Html文件
DiffHandleUtils.generateDiffHtml(list,"生成文件的地址");
```
## 🎋 分支说明
| 分支 | 说明 |
| ----------- | ------------------------------------------------------------ |
| release-1.3 | 1.新增中文转译器ChineseTranslation,支持将规则块转译为中文。
2.新增文本差异对比工具DiffHandleUtils,将转译后的规则内容进行差异对比,支持转Html可视化显示。
3.修复问题:节点类型中新增布尔类型 |
| release-1.2 | 1.新增效验可视化规则块的方法:DefaultDruleCompile.validateRules
2.根据类型的选择不同的值处理逻辑:Operator.Eq.doCompile |
| release-1.1 | 新增drule-execute模块。基于KieBase构建可执行的规则服务RuleService,统一执行框架,并对部分过程开放接口,允许自定义实现。 |
| release-1.0 | 完成初版的规则对象配置和规则编译功能。
1.支持根据注解描述规则对象,转换为TreeNode结构;
2.支持简单的规则编译功能,不支持函数;
3.支持6种简单条件运算符:等于、不等于、小于、小于等于、大于、大于等于; |
## 📦 合作与支持
QQ:1298894906
邮箱:1298894906@qq.com