drools规则引擎与Springboot集成
本篇主要介绍一下Drools与Springboot的集成使用方法,也是具体实践的一部分。
pom文件
引入了springboot和drools的依赖,同时引入了kie-spring的集成依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>${drools.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
</dependencies>
配置类
基于springboot的初始化配置:
@Configuration
public class DroolsAutoConfiguration {
private static final String RULES_PATH = "rules/";
@Bean
@ConditionalOnMissingBean(KieFileSystem.class)
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for (Resource file : getRuleFiles()) {
kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
}
return kieFileSystem;
}
private Resource[] getRuleFiles() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
}
@Bean
@ConditionalOnMissingBean(KieContainer.class)
public KieContainer kieContainer() throws IOException {
final KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
}
private KieServices getKieServices() {
return KieServices.Factory.get();
}
@Bean
@ConditionalOnMissingBean(KieBase.class)
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
@ConditionalOnMissingBean(KieSession.class)
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
@Bean
@ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
}
Springboot启动类
@SpringBootApplication
public class SpringBootDroolsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDroolsApplication.class, args);
}
}
需注意此类的放置位置,详情可参考Springboot相关使用教程。
实体类
public class Address {
private String postcode;
private String street;
private String state;
// 省略getter/setter
}
规则返回结果类
public class AddressCheckResult {
private boolean postCodeResult = false; // true:通过校验;false:未通过校验
// 省略getter/setter
}
规则文件
package plausibcheck.adress
import com.secbro.model.Address;
import com.secbro.model.fact.AddressCheckResult;
rule "Postcode should be filled with exactly 5 numbers"
when
address : Address(postcode != null, postcode matches "([0-9]{5})")
checkResult : AddressCheckResult();
then
checkResult.setPostCodeResult(true);
System.out.println("规则中打印日志:校验通过!");
end
测试Controller
@RequestMapping("/test")
@Controller
public class TestController {
@Resource
private KieSession kieSession;
@ResponseBody
@RequestMapping("/address")
public void test(){
Address address = new Address();
address.setPostcode("99425");
AddressCheckResult result = new AddressCheckResult();
kieSession.insert(address);
kieSession.insert(result);
int ruleFiredCount = kieSession.fireAllRules();
System.out.println("触发了" + ruleFiredCount + "条规则");
if(result.isPostCodeResult()){
System.out.println("规则校验通过");
}
}
}
启动Springboot项目之后,默认访问http://localhost:8080/test/address 即可触发规则。
具体的集成步骤不做过多介绍,阅读代码基本上可以了解全部。详细代码参考github:https://github.com/secbr/drools
相关技术视频
QQ技术交流2群:715840230
CSDN学院:《Drools7规则引擎进阶教程》
CSDN学院:《Drools7规则引擎入门教程》
CSDN学院:《Drools7系列优惠套餐》
获得完整的《从Drools规则引擎到风控反洗钱系统》可评论留下邮箱。
关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台
除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接