Drools规则引擎介绍及实践

   日期:2024-12-27    作者:gmxe8 移动:http://3jjewl.riyuangf.com/mobile/quote/65602.html

@Service
@Slf4j
public class KieServiceImpl implements KieService {

    private StatelessKieSession kieSession;

    private KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    @Resource
    private RiskConfigService riskConfigService;
    //商品规则处理
    @Resource
    private ProductRiskService productRiskService;

    @Override
    public void loadRules(String ruleCode) {
        RiskConfig riskConfig = riskConfigService.queryRiskConfig(ruleCode);
        if(riskConfig!=null){
            loadRule(ruleCode,riskConfig.getRiskContent());
        }
    }

    
    @Override
    public void removeRule(String ruleName) {
        if (kbase.getRule("rules", ruleName) != null) {
            kbase.removeRule("rules", ruleName);
            log.info("remove rule: rules." + ruleName);
            kieSession = kbase.newStatelessKieSession();
            printRules();
        } else {
            log.error("no rule: rules." + ruleName);
        }
    }

    
    @Override
    public void execute(Object object) {
        this.kieSession.execute(object);
    }

    
    @PostConstruct
    public void init() {
        loadRulesAll();
    }

    
    private void loadRulesAll() {
        RiskConfig config = new RiskConfig();
        List<RiskConfig> riskConfigList = riskConfigService.queryRiskConfigs(config);
        if(riskConfigList!=null){
            for(RiskConfig riskConfig : riskConfigList){
                loadRule(riskConfig.getRiskCode(),riskConfig.getRiskContent());
            }
        }
    }

    
    private void loadRule(String ruleCode,String ruleContent){
        try {
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
            kbuilder.add(ResourceFactory.newByteArrayResource(ruleContent.getBytes()), ResourceType.DRL);
            if(kbuilder.hasErrors()){
                log.error("加载规则模板引擎异常{},{}",ruleCode,kbuilder.getErrors());
                return;
            }
            kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
            kieSession = kbase.newStatelessKieSession();
            printRules();
            setGlobal();
        }catch (Exception e){
            log.error("加载规则模板引擎异常",e);
        }
    }

    
    private void setGlobal(){
        kieSession.setGlobal("productRiskService", productRiskService);
    }

    
    private void printRules() {
        log.info("print rule start: ---------");
       kbase.getKnowledgePackages().forEach(knowledgePackage ->
                knowledgePackage.getRules().forEach(rule ->
                        log.info("print rule: " + knowledgePackage.getName() + "." + rule.getName())));
        log.info("print rule end: ---------");
    }
}

Drools规则引擎介绍及实践


特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


举报收藏 0评论 0
0相关评论
相关最新动态
推荐最新动态
点击排行
{
网站首页  |  关于我们  |  联系方式  |  使用协议  |  隐私政策  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号