easy-rules-centraldogma-spring-boot-starter 引入外部rule
easy-rules-centraldogma-spring-boot-starter 当前是基于json 以及只支持spel 格式的解析的
如果我们需要添加其他格式的,当前只直接不能支持的,但是也可以使用其他模式解决
解决方法
自己定义rule,添加到starter提供的bean 中
参考代码
- rule
package com.dalong.rule;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;
@Rule(priority = 0,description = "demo")
public class MyRule {
@Condition
public boolean itRains(@Fact("biz") User user) {
System.out.println(user.toString());
return true;
}
@Action
public void takeAnUmbrella(@Fact("biz") User user) {
System.out.println("run from first");
user.setAge((int)(Math.random()*100));
System.out.println(user.toString());
}
}
- 集成
public class RuleApi {
@Autowired
private Map<String, Rules> centralDogmaRules;
@Autowired
private RulesEngine rulesEngine;
@Autowired
private ObjectMapper objectMapper;
@RequestMapping(value = "/myrule", method = RequestMethod.POST)
public Object info(@RequestBody CloudEvent user) throws Exception {
centralDogmaRules.forEach(new BiConsumer<String, Rules>() {
@Override
public void accept(String s, Rules rules) {
System.out.println(s);
rules.forEach(new Consumer<Rule>() {
@Override
public void accept(Rule rule) {
System.out.println(rule.getDescription());
}
});
}
});
Rules rules = centralDogmaRules.get("demoapp");
PojoCloudEventData<User> cloudEventData = mapData(
user,
PojoCloudEventDataMapper.from(objectMapper,User.class)
);
// 注册了我们的rule
rules.register(new MyRule());
Facts facts = new Facts();
// 生成一个唯一id,方便基于数据id规则流程查询
facts.put("biz",cloudEventData.getValue());
facts.put("result",new MyResult());
rulesEngine.fire(rules,facts);
Object userResult= facts.get("biz");
MyResult result =facts.get("result");
if(Objects.isNull(result.getJsonNode())){
return "not execute";
}
System.out.println(result.getJsonNode().toPrettyString());
System.out.println("result from final ruls"+userResult.toString());
return result;
}
}
说明
此内容主要是关于如果扩展的,具体关于starter 的使用建议参考一些写的文章
参考资料
https://github.com/rongfengliang/easy-rules-centraldogma-spring-boot-starter
https://www.cnblogs.com/rongfengliang/p/15171851.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2020-08-28 wso2 关于graphql 的方案
2020-08-28 Meeting Modern Business Needs with GraphQL
2018-08-28 prisma 服务器端订阅试用