要在Spring Boot項(xiàng)目中集成Drools,你需要遵循以下步驟:
在你的pom.xml
文件中,添加以下依賴:
<!-- Spring Boot Starters -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Drools Dependencies -->
<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.kie</groupId>
<artifactId>kie-api</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-internal</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.kie.modules</groupId>
<artifactId>org-drools-module</artifactId>
<version>${drools.version}</version>
</dependency>
</dependencies>
將${drools.version}
替換為你想使用的Drools版本,例如7.60.0.Final
。
創(chuàng)建一個配置類,用于初始化KieContainer
。這個類將負(fù)責(zé)加載和管理你的Drools規(guī)則。
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;
@Configuration
public class DroolsConfig {
@Bean
public KieContainer kieContainer() {
KieServices kieServices = KieServices.Factory.get();
KieRepository kieRepository = kieServices.getRepository();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
// 從類路徑加載規(guī)則文件
kieFileSystem.write(kieServices.getResources().newClassPathResource("rules/sample-rule.drl"));
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
}
}
在resources/rules
目錄下創(chuàng)建一個名為sample-rule.drl
的文件,并編寫你的Drools規(guī)則。例如:
package com.example.drools
import com.example.domain.Person;
rule "Check if person is adult"
when
$person: Person(age >= 18)
then
System.out.println("Person is an adult.");
end
現(xiàn)在你可以在你的應(yīng)用程序中使用KieContainer
來執(zhí)行Drools規(guī)則。例如,你可以創(chuàng)建一個服務(wù)類來處理規(guī)則執(zhí)行:
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(Object... facts) {
KieSession kieSession = kieContainer.newKieSession();
for (Object fact : facts) {
kieSession.insert(fact);
}
kieSession.fireAllRules();
kieSession.dispose();
}
}
最后,你可以在你的控制器或其他類中調(diào)用DroolsService
來執(zhí)行規(guī)則。例如:
import com.example.domain.Person;
import com.example.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 SampleController {
@Autowired
private DroolsService droolsService;
@GetMapping("/check-adult")
public String checkAdult() {
Person person = new Person();
person.setAge(20);
droolsService.executeRules(person);
return "Checked if person is adult.";
}
}
現(xiàn)在,當(dāng)你訪問/check-adult
端點(diǎn)時,Drools將根據(jù)sample-rule.drl
中定義的規(guī)則檢查Person
對象是否是成年人。