easy-rules-centraldogma-spring-boot-starter 引入外部rule
Posted rongfengliang-荣锋亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于easy-rules-centraldogma-spring-boot-starter 引入外部rule的主要内容,如果未能解决你的问题,请参考以下文章