2026-03-27 10:27:04 +08:00
|
|
|
|
package com.solution.rule.config;
|
|
|
|
|
|
|
|
|
|
|
|
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.KieRepository;
|
2026-03-27 16:56:07 +08:00
|
|
|
|
import org.kie.api.builder.Message;
|
2026-03-27 10:27:04 +08:00
|
|
|
|
import org.kie.api.runtime.KieContainer;
|
2026-03-27 16:56:07 +08:00
|
|
|
|
import org.kie.api.runtime.KieSession;
|
2026-03-27 10:27:04 +08:00
|
|
|
|
import org.kie.internal.io.ResourceFactory;
|
|
|
|
|
|
import org.kie.spring.KModuleBeanFactoryPostProcessor;
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
2026-03-27 16:56:07 +08:00
|
|
|
|
import org.springframework.core.io.Resource;
|
2026-03-27 10:27:04 +08:00
|
|
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
|
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
|
public class DroolsConfig {
|
|
|
|
|
|
|
2026-03-27 16:56:07 +08:00
|
|
|
|
private static final String RULE_PATH = "classpath*:rules/**/*.drl";
|
2026-03-27 10:27:04 +08:00
|
|
|
|
|
|
|
|
|
|
private final KieServices kieServices = KieServices.Factory.get();
|
|
|
|
|
|
|
2026-03-27 16:56:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 加载规则文件
|
|
|
|
|
|
*/
|
2026-03-27 10:27:04 +08:00
|
|
|
|
@Bean
|
|
|
|
|
|
public KieFileSystem kieFileSystem() throws IOException {
|
|
|
|
|
|
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
|
2026-03-27 16:56:07 +08:00
|
|
|
|
|
|
|
|
|
|
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
|
|
|
Resource[] resources = resolver.getResources(RULE_PATH);
|
|
|
|
|
|
|
|
|
|
|
|
for (Resource resource : resources) {
|
|
|
|
|
|
kieFileSystem.write(
|
|
|
|
|
|
ResourceFactory.newInputStreamResource(resource.getInputStream())
|
|
|
|
|
|
.setSourcePath("rules/" + resource.getFilename())
|
|
|
|
|
|
);
|
2026-03-27 10:27:04 +08:00
|
|
|
|
}
|
2026-03-27 16:56:07 +08:00
|
|
|
|
|
|
|
|
|
|
return kieFileSystem;
|
2026-03-27 10:27:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-27 16:56:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 构建 KieContainer
|
|
|
|
|
|
*/
|
2026-03-27 10:27:04 +08:00
|
|
|
|
@Bean
|
2026-03-27 16:56:07 +08:00
|
|
|
|
public KieContainer kieContainer(KieFileSystem kieFileSystem) {
|
|
|
|
|
|
|
|
|
|
|
|
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
|
2026-03-27 10:27:04 +08:00
|
|
|
|
kieBuilder.buildAll();
|
2026-03-27 16:56:07 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查错误
|
|
|
|
|
|
if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
|
|
|
|
|
|
throw new RuntimeException("Drools规则编译错误:\n" +
|
|
|
|
|
|
kieBuilder.getResults().toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KieRepository repository = kieServices.getRepository();
|
|
|
|
|
|
|
|
|
|
|
|
return kieServices.newKieContainer(repository.getDefaultReleaseId());
|
2026-03-27 10:27:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-27 16:56:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* KieBase
|
|
|
|
|
|
*/
|
2026-03-27 10:27:04 +08:00
|
|
|
|
@Bean
|
2026-03-27 16:56:07 +08:00
|
|
|
|
public KieBase kieBase(KieContainer kieContainer) {
|
|
|
|
|
|
return kieContainer.getKieBase();
|
2026-03-27 10:27:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-27 16:56:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* KieSession(推荐加)
|
|
|
|
|
|
*/
|
2026-03-27 10:27:04 +08:00
|
|
|
|
@Bean
|
2026-03-27 16:56:07 +08:00
|
|
|
|
public KieSession kieSession(KieContainer kieContainer) {
|
|
|
|
|
|
return kieContainer.newKieSession();
|
2026-03-27 10:27:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-27 16:56:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 必须是 static
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
public static KModuleBeanFactoryPostProcessor kiePostProcessor() {
|
|
|
|
|
|
return new KModuleBeanFactoryPostProcessor();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|