背景
某项规则分类或是校验规则繁多,使用规则引擎将业务规则与代码逻辑解耦。
规则引擎
-
具体使用
-
操作
- 引入依赖:
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>2.12.2.1</version>
</dependency>
- 在yaml文件中配置规则文件
liteflow:
rule-source: config/flow.el.xml
- 定义规则文件
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
THEN(a, b, c);
</chain>
</flow>
- 注入FlowExecutor直接使用
@Autowired
private FlowExecutor flowExecutor;
2. ### 案例实现
现在需要实现的逻辑是根据入参的code值,选择对应的价格
- 实体类:
//用于传参赋值的实体类
@Data
public class TestEntity{
String code;
String price;
}
//FlowExecutor接口所需的流程初始参数,此处无实际含义
@Data
public class FirstParam {
String param;
}
- 定义规则文件:
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<!-- 选择组件-->
<chain name="chooseModule">
SWITCH(judgeCode).to(basic, higher, lower).DEFAULT(basic);
</chain>
</flow>
- 组件:
package com.betterwood.user.cmp.test2;
import com.betterwood.user.cmp.FirstParam;
import com.betterwood.user.cmp.TestEntity;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeSwitchComponent;
import org.springframework.stereotype.Component;
/**
* @description :
* @author : wenjingwang
*/
@LiteflowComponent("judgeCode")//选择组件
public class JudgeCodeCmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
FirstParam firstParam = this.getRequestData();
TestEntity testEntity = this.getContextBean(TestEntity.class);
return testEntity.getCode();
}
}
package com.betterwood.user.cmp.test2;
import com.betterwood.user.cmp.FirstParam;
import com.betterwood.user.cmp.TestEntity;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
/**
* @description :
* @author : wenjingwang
*/
@Component("basic")//基础价格
public class BasicCmp extends NodeComponent {
@Override
public void process() {
FirstParam firstParam = this.getRequestData();
TestEntity testEntity = this.getContextBean(TestEntity.class);
testEntity.setPrice("10.0");
}
}
package com.betterwood.user.cmp.test2;
import com.betterwood.user.cmp.FirstParam;
import com.betterwood.user.cmp.TestEntity;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
/**
* @description :
* @author : wenjingwang
*/
@Component("higher")//更高价格
public class HigherCmp extends NodeComponent {
@Override
public void process() {
FirstParam firstParam = this.getRequestData();
TestEntity testEntity = this.getContextBean(TestEntity.class);
testEntity.setPrice("20.0");
}
}
package com.betterwood.user.cmp.test2;
import com.betterwood.user.cmp.FirstParam;
import com.betterwood.user.cmp.TestEntity;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
/**
* @description :
* @author : wenjingwang
*/
@Component("lower") //更低价格
public class LowerCmp extends NodeComponent {
@Override
public void process() {
FirstParam firstParam = this.getRequestData();
TestEntity testEntity = this.getContextBean(TestEntity.class);
testEntity.setPrice("5.0");
}
}
- 测试方法:
@PostMapping("/check/testPrice")
public void testPrice(@RequestBody TestEntity testEntity){
FirstParam firstParam = new FirstParam();
LiteflowResponse response = flowExecutor.execute2Resp("chooseModule", firstParam, testEntity);
System.out.println("testEntity的价格为:"+testEntity.getPrice());
}
- 测试结果: