echo編輯整理,歡迎轉載,轉載請聲明文章來源。歡迎添加echo微信(微信號:t2421499075) 交流學習。java
Drools的實現並不難,關鍵是瞭解他的語法便可web
入門案例
- 建立一個簡單的SpringBoot項目
- 選擇依賴的時候,添加一個web依賴便可
- 在新項目的pom.xml中添加drools的相關依賴
<properties> <java.version>1.8</java.version> <drools.version>7.14.0.Final</drools.version> </properties> <dependencies> <!-- drools依賴 --> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>${drools.version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>${drools.version}</version> </dependency> <!-- 決策表 --> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>${drools.version}</version> </dependency> <!-- 模板 --> <dependency> <groupId>org.drools</groupId> <artifactId>drools-templates</artifactId> <version>${drools.version}</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>${drools.version}</version> </dependency> </dependencies>
- 建立Kie相關 Bean 使其被spring管理
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.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.Resource; import java.io.IOException; /** * @author tang.sl * @date 2021-01-05 09:37 */ @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(); } }
到目前爲止,咱們的基本準備工做就已經完成了,咱們能夠建立實際的案例來使用Drools了。spring
- 根據前面的需求定義一個實體類
@Data @ToString @NoArgsConstructor @AllArgsConstructor public class User { private String name; private String age; private String address; private String phone; private String sex; }
- 建立drl規則文件 根據前幾篇文章描述的內容,咱們來設計一個規則文件。文件放在resources目錄下面,在該目錄下面建立一個rules統一存放後面全部的rule文件
package com.echo.drools.dto import com.echo.drools.dto.User dialect "java" rule "address eq beijing" when $user: User(address == "北京") then System.out.println($user.getName() + "是個北漂"); end rule "address eq shanghai" when $user: User(address == "上海") then System.out.println($user.getName() + "是個上漂"); end
- 而後咱們就能夠來測試使用這個規則
import com.echo.drools.dto.User; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.kie.api.runtime.KieSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class DroolsApplicationTests { @Autowired private KieSession session; @Test void contextLoads() { User user = new User(); user.setAddress("北京"); user.setAge("20"); user.setName("echo"); user.setPhone("136123456"); user.setSex("1"); // 插入用戶 session.insert(user); // 執行規則 session.fireAllRules(); } /** * 記得執行完成以後釋放資源 */ @AfterEach public void runDispose() { session.dispose(); } }
- 屢次測試 結果和咱們的符合咱們原有的預期,好比上面的實例,最終會在控制檯輸出「echo是北漂」。
注意:咱們這裏僅僅寫了兩個規則,而且也沒有寫對應的業務代碼,這個後面再講,僅僅只是一個入門展現案例。不要多糾結爲何沒有業務代碼api