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

posted on   荣锋亮  阅读(148)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.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 服务器端订阅试用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示