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);
}
}
即整合完成!