1.说明

为了更好的在项目中使用Drools,
需要把Drools集成到Spring Boot,
下面介绍集成的方法,
并且开发简单的Demo和测试用例。

2.创建Maven工程

pom.xml工程信息:

<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><groupId>com.ai.prd</groupId><artifactId>drools-spring-boot-demo</artifactId><version>0.0.1-SNAPSHOT</version><description>Drools integrats into the Spring Boot</description>
</project>

3.引入Spring Boot相关依赖

引入spring-boot-starter-web作为Web工程,对外提供Rest服务,
引入spring-boot-starter-log4j2日志框架,打印测试匹配结果,
引入spring-boot-starter-test测试框架,开发Junt5测试用例:

<properties><spring-boot.version>2.3.1.RELEASE</spring-boot.version>
</properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><exclusions><!-- 单元测试不使用Junit4,使用Junit5 --><exclusion><groupId>junit</groupId><artifactId>junit</artifactId></exclusion><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency>
</dependencies>

4.引入Drools相关依赖

通过kie-spring引入Drools相关的jar包,
其依赖的spring版本都排除掉,
以上一步的spring boot依赖为准。

<properties><drools.version>7.47.0.Final</drools.version>
</properties>
<dependencies><dependency><groupId>org.kie</groupId><artifactId>kie-spring</artifactId><version>${drools.version}</version><exclusions><!-- 依赖的spring版本全部以spring boot依赖的为准 --><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>
</dependencies>

5.开发启动类

启动类DroolsApplication.java:

package com.ai.prd.drools;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DroolsApplication {public static void main(String[] args) {SpringApplication.run(DroolsApplication.class, args);}
}

6.开发Rest服务

操作的对象Person,
Person.java:

package com.ai.prd.drools.entity;public class Person {private String name;private int age;
}

对外提供的Rest服务,
可以对Person对象进行规则匹配,
提供了两个接口,
单个和批量的操作接口:
PersonRuleController.java:

package com.ai.prd.drools.controller;import java.util.List;import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.ai.prd.drools.entity.Person;@RestController
@RequestMapping("rule/person")
public class PersonRuleController {@Autowiredprivate KieContainer kieContainer;@PostMapping("one")public void fireAllRules4One(@RequestBody Person person) {KieSession kSession = kieContainer.newKieSession();try {kSession.insert(person);kSession.fireAllRules();} finally {kSession.dispose();}}@PostMapping("list")public void fireAllRules4List(@RequestBody List<Person> persons) {KieSession kSession = kieContainer.newKieSession();try {for (Person person : persons) {kSession.insert(person);}kSession.fireAllRules();} finally {kSession.dispose();}}
}

7.初始化Drools

在上面PersonRuleController中需要用到KieContainer,
这个必须在Spring中先初始化才能使用,
相关功能由DroolsAutoConfiguration.java提供:

package com.ai.prd.drools.config;import java.io.IOException;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.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
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.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;/*** 配置Drools的服务类,方便在Rest接口中调用。 该类负责加载具体的drl规则文件, 不再需要kmodule.xml配置文件了。*/
@Configuration
public class DroolsAutoConfiguration {private static final String RULES_PATH = "rules/com/ai/prd/";@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() {@Overridepublic 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();}// 不能反复被使用,释放资源后需要重新获取。// @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)@Bean@ConditionalOnMissingBean(KieSession.class)public KieSession kieSession() throws IOException {return kieContainer().newKieSession();}@Bean@ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)public KModuleBeanFactoryPostProcessor kiePostProcessor() {return new KModuleBeanFactoryPostProcessor();}
}

8.开发规则文件

在DroolsAutoConfiguration中指定了drl规则文件
所在目录rules/com/ai/prd/,
在src/main/resources/rules/com/ai/prd/目录下新建文件
ai-rules.drl:

package com.ai.prdimport com.ai.prd.drools.entity.Person;
import com.ai.prd.drools.action.PersonRuleAction;// 根据名字匹配指定的人
rule "1.find target person"when$p : Person( name == "bob" )thenPersonRuleAction.doParse($p, drools.getRule());System.out.println("Rule name is [" + drools.getRule().getName() + "]"); System.out.println("Rule package is [" + drools.getRule().getPackageName() + "]");
end// 根据年龄匹配找到打工人
rule "2.find the work person"when$p : Person( age >= 25 && age < 65 )      thenSystem.out.println( $p + " is a work person!" );
end

规则1匹配名字为bob的人,并且调用工具类PersonRuleAction打印相关日志,
同时打印规则的名称和包路径到控制台。
规则2匹配年龄在25到65之间的打工人,
然后把匹配到的人直接打印到控制台。

9.规则处理类

PersonRuleAction.java在匹配到相应规则时被调用,
此处仅实现日志打印的功能:

package com.ai.prd.drools.action;import org.drools.core.definitions.rule.impl.RuleImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.ai.prd.drools.entity.Person;/*** 触发Person相关的规则后的处理类* * @author yuwen**/
public class PersonRuleAction {private static Logger LOG = LoggerFactory.getLogger(PersonRuleAction.class);// 目前只实现记录日志功能public static void doParse(Person person, RuleImpl rule) {LOG.debug("{} is matched Rule[{}]!", person, rule.getName());}
}

10.新建日志配置文件

在src/main/resources目录下,
新建日志配置文件Log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"><Appenders><Console name="Console" target="SYSTEM_OUT"><PatternLayoutpattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /></Console><RollingFile name="RuleResultFile"fileName="log/rule_result.log"filePattern="log/backup/rule_result-%i.log"><PatternLayoutpattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%l] - %msg%n" /><Policies><SizeBasedTriggeringPolicy size="10MB" /></Policies><DefaultRolloverStrategy max="10" /></RollingFile></Appenders><Loggers><Logger name="com.ai.prd.drools.action" level="DEBUG"additivity="true"><AppenderRef ref="RuleResultFile" /></Logger><Root level="INFO"><AppenderRef ref="Console" /></Root></Loggers>
</Configuration>

日志文件配置后,
PersonRuleAction类打印的日志
不仅会输出到log/rule_result.log,
也会输出到控制台。

11.开发Junit5测试用例

针对上面PersonRuleController提供的Rest接口,
开发两个Junit5的测试用例,
在src/test/java/目录下
创建PersonRuleControllerTest.java:

package com.ai.prd.drools.controller;import java.util.Arrays;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import com.ai.prd.drools.entity.Person;@SpringBootTest
public class PersonRuleControllerTest {@AutowiredPersonRuleController controller;@Testpublic void testOnePerson() {Person bob = new Person();bob.setName("bob");controller.fireAllRules4One(bob);}@Testpublic void testTwoPerson() {Person bob = new Person();bob.setAge(33);Person other = new Person();other.setAge(88);controller.fireAllRules4List(Arrays.asList(bob, other));}
}

12.运行测试用例

PersonRuleControllerTest执行后,
控制台输出:

14:22:04.851 [main] WARN  org.drools.compiler.kie.builder.impl.KieBuilderImpl - File 'rules/com/ai/prd/ai-rules.drl' is in folder 'rules/com/ai/prd' but declares package 'com.ai.prd'. It is advised to have a correspondance between package and folder names.
14:22:06.753 [main] INFO  org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
14:22:07.063 [main] INFO  com.ai.prd.drools.controller.PersonRuleControllerTest - Started PersonRuleControllerTest in 3.279 seconds (JVM running for 4.452)
14:22:07.279 [main] DEBUG com.ai.prd.drools.action.PersonRuleAction - Person [name=bob, birthDay=null, age=0, address=null] is matched Rule[1.find target person]!
Rule name is [1.find target person]
Rule package is [com.ai.prd]
Person [name=null, birthDay=null, age=33, address=null] is a work person!
14:22:07.649 [SpringContextShutdownHook] INFO  org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Shutting down ExecutorService 'applicationTaskExecutor'

13.错误解决

cannot be cast to org.drools.compiler.kie.builder.impl.InternalKieModule
大概率是rule规则文件有问题,
格式,中英文字符,语法等问题,
请确保规则文件正确。
可以安装相应插件打开规则文件,
请参考:Drools的Eclipse_IDEA插件安装

14.参考文章

Drools规则引擎 系列教程(一)SpringBoot整合 & 快速集成上手《Drools7.0.0.Final规则引擎教程》之Springboot集成Drools创建Maven工程


http://www.taodudu.cc/news/show-1250968.html

相关文章:

  • Drools集成SpringBootStarter
  • Jsonschema2pojo从JSON生成Java类(Maven)
  • YangTools从YANG生成Java类(Maven)
  • GitBash添加tree命令
  • SpringBoot集成Maven工程
  • SpringBoot开发Restful接口
  • Notepad++便签模式
  • SpringBoot集成Cache缓存(Ehcache缓存框架,注解方式)
  • PowerDesigner生成数据库刷库脚本
  • PowerDesigner生成数据库设计文档
  • Eclipse配置国内镜像源
  • PingInfoView批量PING工具
  • Git合并两个不同的仓库
  • Guava事件处理组件Eventbus使用入门
  • Junit4集成到Maven工程
  • Redis集成到Maven工程(Jedis客户端)
  • SpringBoot集成Cache缓存(Redis缓存,RedisTemplate方式)
  • Junit5集成到Maven工程
  • Junit5集成到SpringBoot工程
  • 语言代码表
  • Protobuf生成Java代码(Maven)
  • Protobuf生成Java代码(命令行)
  • Maven查看插件信息
  • SpringBoot脚手架工程快速搭建
  • SpringBoot集成MyBatis-Plus分页插件
  • SNMP客户端工具MIB Browser
  • PowerDesigner运行自定义VBS脚本,复制Name到Comment
  • BitMap-BitSet(JDK1.8)基本使用入门
  • IDEA查看Java类的UML关系图
  • 30. 包含min函数的栈

Drools集成SpringBoot相关推荐

  1. Drools集成SpringBootStarter

    1.说明 基于fast-drools-spring-boot-starter, 能够方便的将规则引擎Drools集成到Spring Boot, 基于前面介绍过的文章Drools集成SpringBoot ...

  2. 第13章 Kotlin 集成 SpringBoot 服务端开发(1)

    第13章 Kotlin 集成 SpringBoot 服务端开发 本章介绍Kotlin服务端开发的相关内容.首先,我们简单介绍一下Spring Boot服务端开发框架,快速给出一个 Restful He ...

  3. RocketMQ集成SpringBoot

    RocketMQ集成SpringBoot RocketMQ总体架构 RocketMQ基本特性

  4. 【Java从0到架构师】RocketMQ 使用 - 集成 SpringBoot

    RocketMQ 消息中间件 集成 SpringBoot 入门案例 生产消息类型 - 同步.异步.一次性 消费模式 - 集群.广播 延时消息 设置消息标签 设置消息的 Key 自定义属性设置 消息过滤 ...

  5. zookeeper-常用命令,集成springboot,分布式锁实现和原理 ,dock集群zookeeper搭建,

    (一)zookeeper数据模型 树形结构 每个节点里面保存信息 节点拥有子节点 节点是临时的也可以是持久的 四大节点 PERSISTENT-持久化目录节点 客户端与zookeeper断开连接后,该节 ...

  6. ElasticSearch集成SpringBoot+实战

    ElasticSearch集成SpringBoot+搜索页面实战(仿京东) SpringBoot和es相关操作 es集成SpringBoot 使用springboot操作es API 索引相关 文档相 ...

  7. 第13章 Kotlin 集成 SpringBoot 服务端开发(2)

    13.2.10 搜索关键字管理 本节我们开发爬虫爬取的关键字管理的功能. 数据库实体类 首先,新建实体类SearchKeyWord 如下 package com.easy.kotlin.picture ...

  8. win10下kafka集群安装+集成springboot

    kafka安装+集成springboot 记录kafka安装.学习.继承springboot的过程 文章目录 kafka安装+集成springboot 前言 一.kafka + zk的安装 1.zk的 ...

  9. 狂神聊 ElasticSearch(IK分词器+Rest+集成SpringBoot+实战爬虫项目+完整代码及资料)

    Bilibili 搜索关注:狂神说 Java(和狂神一起学习,共同进步) 公众号:狂神说(文章日更) 狂神聊 ElasticSearch 版本:ElasticSearch 7.6.1(全网最新了) 6 ...

最新文章

  1. 从1400篇机器学习文章中精选出Top 10,帮你找找上班的感觉!
  2. flutter listview 滚动到指定位置_flutter入门
  3. 360浏览器的收藏栏不见了怎么办?
  4. 利用计算机来模仿人,如何模仿人的学习模式来教计算机程序解数学题?
  5. 机器学习基础6--集群模型和算法
  6. eth显卡算力2020最新排行_最新三大主流币IPFS比特币ETH挖矿全网算力动态速递单周报(12.3更新)...
  7. Spring系列合并
  8. linux oracle 运维_oracle数据库常用命令整理
  9. Entity Framework 6 和 MVC5
  10. 【已解决】Windows Ink中没有便签怎么办
  11. 火山引擎对外开放推荐算法等字节跳动核心技术
  12. zuul源码分析之Request生命周期管理
  13. python中时间的处理
  14. RFID将成为物联网革命的首战
  15. 谷歌,IE,火狐浏览器内核
  16. 实用供暖通风空调设计手册 第三版_实用供热空调设计手册第三版即将出版随想...
  17. 网页设计常用字体(转)
  18. 布谷鸟沙盒分析静态文件_布谷鸟cuckoo
  19. JSP危险化学品管理系统myeclipse开发mysql数据库bs框架java编程jdbc详细设计
  20. CSS - 制作三角形

热门文章

  1. Centos7安装并配置mysql5.6
  2. 清北学堂----北京集训
  3. java常见命名规则
  4. [jQuery基础] jQuery对象 -- 选择器
  5. css3实现的一些灰色的导航条按钮
  6. LeetCode--84.柱状图中最大的矩形(暴力法,单调栈)
  7. 二叉树的创建、前序遍历、中序遍历、后序遍历
  8. Meanshift 均值飘移实现图像聚类 MATLAB实现(4)
  9. GBDT和XGBoost
  10. 7-7 12-24小时制 (15 分)