Drools与SpringBoot整合

Drools与SpringBoot整合

1、新建一个springboot项目
2、新建Fact对象

package com.mc.entity;

/**
 * @author chenmingcai
 * @date 2021/12/9 10:36
 */
public class Order {
    private String name;
    private Integer score;
    private Integer amount;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "Order{" +
                "name='" + name + '\'' +
                ", score='" + score + '\'' +
                ", amount=" + amount +
                '}';
    }
}

3、在resources目录下新建rules目录,在rules目录下新建规则文件

package rules;

import com.mc.entity.Order

rule "rule_9"
when
    eval(true)
then

    System.out.println("执行了规则eval=================");
end

rule "rule_1"
when
    $order:Order(amount<100)
then
    $order.setScore(100);
    System.out.println("执行了规则rule_1");
end

rule "rule_2"
when
    $order:Order(amount>=100 && amount <500)
then
    $order.setScore(500);
    System.out.println("执行了规则rule_2");
end

rule "rule_3"
when
    $order:Order(amount>=500 && amount<1000)
then
    $order.setScore(1000);
    System.out.println("执行了规则rule_3");
end

4、编写Drools配置类:DroolsConfig

package com.mc.config;

import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * @author chenmingcai
 * @date 2021/12/11 9:45
 */

/**
 * Drools 规则引擎配置类
 */
@Configuration
public class DroolsConfig {
    // 指定规则文件的存放位置
    private static final String RULES_PATH = "rules/";
    private final KieServices kieServices = KieServices.Factory.get();

    @Bean
    @ConditionalOnMissingBean
    public KieFileSystem kieFileSystem() throws IOException {
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] files = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*");
        String path = null;
        for (Resource file : files) {
            path = RULES_PATH + file.getFilename();
            kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
        }
        return kieFileSystem;
    }
    @Bean
    @ConditionalOnMissingBean
    public KieContainer kieContainer() throws IOException {
        KieRepository kieRepository = kieServices.getRepository();
        kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
        kieBuilder.buildAll();
        return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
    }
    @Bean
    @ConditionalOnMissingBean
    public KieBase kieBase() throws IOException {
        return kieContainer().getKieBase();
    }
    @Bean
    @ConditionalOnMissingBean
    public KModuleBeanFactoryPostProcessor kiePostProcessor() {
        return new KModuleBeanFactoryPostProcessor();
    }
}

6、service层

package com.mc.service;

import com.mc.entity.Order;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenmingcai
 * @date 2021/12/11 10:08
 */
@Service
public class RuleService {
    @Autowired
    private KieBase kieBase;

    public Order excuteRule(Order order){
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(order);
        kieSession.fireAllRules();
        kieSession.dispose();
        return order;
    }
}

7、controller层

package com.mc.controller;

import com.mc.entity.Order;
import com.mc.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenmingcai
 * @date 2021/12/11 10:22
 */
@RestController
public class OrderController {
    @Autowired
    private RuleService ruleService;

    @GetMapping("/saveOrder")
    public Order excuteOrder(@RequestBody Order order) {
        return ruleService.excuteRule(order);
    }
}

即整合完成!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值