Springboot + Drools入门
文章目录
- 0.搭建项目流程图
- 1.项目结构
- 2.pom.xml文件
- 3.配置类
- 4.实体类
- 5.控制器层
- 6.规则文件
- 该项目代码下载地址:
这段时间需要用到“ ** 规则引擎 ** ”,在对比了很多开源工具后,选择了Drools。目前正在进行入门学习,感觉drools的使用还是非常方便的。
对于drools的优点、作用和具体的使用教程还请自行查阅资料,这里仅仅使用使用springboot+drools搭建一个小demo.
0.搭建项目流程图
1.项目结构
2.pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wllfengshu</groupId>
<artifactId>drools</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>drools</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<drools.version>7.11.0.Final</drools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>${drools.version}</version>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.wllfengshu.drools.DroolsApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.配置类
这里稍微做了优化
package com.wllfengshu.drools.configs;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
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 wangll */
@Configuration
public class DroolsConfig {/** * rule文件的存放位置 */
private static final String RULES_PATH = "rules/";
/** * 获取到的KieServices其实是一个单例 */
private final KieServices kieServices = KieServices.Factory.get();
@Bean
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
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
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
@Bean
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
}
4.实体类
package com.wllfengshu.drools.model;
import lombok.Data;
/** * @author wangll */
@Data
public class Address {
private String postcode;
private String street;
private String state;
}
5.控制器层
package com.wllfengshu.drools.rest;
import com.wllfengshu.drools.model.Address;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/** * @author wangll */
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private KieSession kieSession;
@ResponseBody
@RequestMapping(value = "/address/{postCode}",method = RequestMethod.GET)
public void test(@PathVariable(value = "postCode") String postCode){
// 以下的数据可以从数据库来,这里写死了
Address address = new Address();
address.setPostcode(postCode);
// 使用规则引擎
kieSession.insert(address);
int ruleFiredCount = kieSession.fireAllRules();
System.out.println("触发了" + ruleFiredCount + "条规则");
System.out.println("---------------------------------");
}
}
6.规则文件
这个规则是检查“地址”中“邮编”是否是5个字符以内的数字
package com.rules
import com.wllfengshu.drools.model.Address;
rule “Postcode should be filled with exactly 5 numbers”
when
$address : Address(postcode != null, postcode matches "([0-9]{5})")
then
System.out.println("规则中打印日志:校验通过!");
end
该项目代码下载地址:
https://download.csdn.net/download/tiandixuanwuliang/11215152
(ps:这个csdn的积分取消不掉,后期我会在github上做一个开源项目。包括Drools小Demo,同时也准备做一个界面拖拽生成规则的开源项目。欢迎大家关注,谢谢)
参考:https://blog.csdn.net/qq\_21383435/article/details/82873711
在上述参考文献中做了如下修改:
1、去掉了spring-boot-autoconfigure依赖
2、去掉了kmodule.xml配置文件
3、修改controller层中的Resource为Autowired注解
4、优化部分代码
还没有评论,来说两句吧...