最详细的spring boot集成drools,手把手教你操作
背景:
使用的基础项目来自https://gitee.com/smell2/ruoyi-vue-activiti.git
引入drools的步骤如下:
一、创建ruoyi-drools模块
parent选ruoyi,点确定
二、在ruoyi-admin模块的pom.xml引入drools依赖和junit依赖
<!-- drools--> <dependency> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-drools</artifactId> <version>3.2.0</version> </dependency> <!-- junit--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
三、在ruoyi-admin模块配置drools
1.新建src/main/java/com/ruoyi/web/core/config/KiaSessionConfig.java,代码如下:
package com.ruoyi.web.core.config; 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.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; @Configuration public class KiaSessionConfig { private static final String RULES_PATH = "rules/"; @Bean 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(); final Resource[] resources = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*"); return resources; } @Bean 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 public KieBase kieBase() throws IOException { return kieContainer().getKieBase(); } @Bean public KieSession kieSession() throws IOException { return kieContainer().newKieSession(); } }
2.新建model目录,并创建src/main/java/com/ruoyi/web/model/People.java,代码如下:
package com.ruoyi.web.model; public class People { private int sex; private String name; private String drlType; public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDrlType() { return drlType; } public void setDrlType(String drlType) { this.drlType = drlType; } }
3.在resources目录下创建rules文件夹,并在rules文件夹下创建drools规则文件rule01.drl,代码如下:
import com.ruoyi.web.model.People; dialect "java" rule "boy" when $p : People(sex == 1 && drlType == "people") then System.out.println($p.getName() + " is a boy"); end rule "girl" when $p : People(sex == 0 && drlType == "people") then System.out.println($p.getName() + "is a girl"); end
4.新建单元测试类,src/test/java/com/ruoyi/test/DroolsJunitTest.java,代码如下:
package com.ruoyi.test; import com.ruoyi.RuoYiApplication; import com.ruoyi.web.model.People; import org.junit.Test; import org.junit.runner.RunWith; import org.kie.api.runtime.KieSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = RuoYiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class DroolsJunitTest { @Autowired private KieSession session; @Test public void people() { People people = new People(); people.setName("drools"); people.setSex(1); people.setDrlType("people"); session.insert(people); // 插入 session.fireAllRules(); // 执行规则 } }
参考资料: