在 Spring Boot 中使用 Drools 規則引擎主要包括以下幾個步驟:
在你的 pom.xml
文件中添加 Drools 和 Spring Boot 相關的依賴:
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Drools -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.59.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.59.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>7.59.0.Final</version>
</dependency>
</dependencies>
在 src/main/resources
目錄下創建一個名為 rules
的文件夾,然后在該文件夾中創建一個名為 sample.drl
的規則文件。在這個文件中編寫你的 Drools 規則:
package com.example.drools
import com.example.drools.domain.Person;
rule "Sample Rule"
when
$person: Person(age >= 18)
then
System.out.println("Person is eligible for voting.");
end
在 com.example.drools.domain
包下創建一個名為 Person
的實體類:
package com.example.drools.domain;
public class Person {
private String name;
private int age;
// Getters and setters
}
創建一個名為 DroolsConfig
的配置類,用于初始化 Drools 的 KieContainer
:
package com.example.drools.config;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
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 DroolsConfig {
@Bean
public KieContainer kieContainer() throws IOException {
KieServices kieServices = KieServices.Factory.get();
KieRepository kieRepository = kieServices.getRepository();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resourcePatternResolver.getResources("classpath*:/rules/*.*");
for (Resource resource : resources) {
kieFileSystem.write(resource.getFilename(), resource.getInputStream());
}
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
}
}
在你的服務類中注入 KieContainer
,并使用它來執行規則:
package com.example.drools.service;
import com.example.drools.domain.Person;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DroolsService {
@Autowired
private KieContainer kieContainer;
public void executeRules(Person person) {
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(person);
kieSession.fireAllRules();
kieSession.dispose();
}
}
在你的控制器或測試類中調用服務類的 executeRules
方法來測試 Drools 規則引擎:
package com.example.drools.controller;
import com.example.drools.domain.Person;
import com.example.drools.service.DroolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DroolsController {
@Autowired
private DroolsService droolsService;
@GetMapping("/test")
public String test() {
Person person = new Person();
person.setName("John Doe");
person.setAge(20);
droolsService.executeRules(person);
return "Rules executed successfully.";
}
}
現在,當你訪問 /test
端點時,Drools 規則引擎將根據定義的規則執行。