1

我正在使用 Gradle 和 7.20 drools 版本创建一个 spring boot 应用程序。我们正在从位于路径的外部资源文件中加载 drools 知识库。drl_file_path下面是我的配置代码。

@Configuration
public class DroolConfig {
    KieServices kieServices = KieServices.Factory.get();
    @Bean
    public KieFileSystem kieFileSystem() throws IOException {
        KieFileSystem kieFileSystem =kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newFileResource(new File("drl_file_path")));
        return kieFileSystem;
    }

    @Bean
    public KieContainer kieContainer() throws IOException{
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
        kieBuilder.buildAll();
        KieModule kieModule = kieBuilder.getKieModule();
        KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
        KieScanner kScanner = kieServices.newKieScanner(kieContainer);
        kScanner.start( 10000L );
        return kieContainer;
    }

    @Bean
    public KieScanner kieScanner() throws IOException{
        return kieServices.newKieScanner(kieContainer());
    }
}

有机会我们必须更新drools规则,我们想在不重新启动服务器的情况下更新drools知识库,我们正在使用KieScanner.sacnNow()哪个不起作用,我缺少什么关键点。下面是重新加载和应用drools规则的代码-

@Component
public class DroolComponent {
    @Autowired private KieContainer kieContainer;
    @Autowired private KieScanner kieScanner;

    public Customer reloadDroolsAndApply(Customer customer) {
        kieScanner.scanNow();
        //above scanning not applying updated rules. 
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(customer);
        kieSession.fireAllRules();
        kieSession.dispose();
        return customer;
    }
}
4

1 回答 1

0

您可以使用以下方法为 Drools 获取新容器。

private  KieContainer loadContainerFromString() throws IOException{
    long startTime = System.currentTimeMillis();
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();

    InputStream stream = null;
    String drl = null;
    String RULE_PATH = "src/main/resources/";
    SpreadsheetCompiler converter = new SpreadsheetCompiler();

    File file = new File("src/main/resources/finance.xls");
    FileInputStream fIP = new FileInputStream(file);
    if(file.isFile() && file.exists()) {
        System.out.println("finance.xls file open successfully.");
    }
    Workbook workbook = new HSSFWorkbook(fIP);

    for (Sheet sheet : workbook) {
        System.out.println("Loading Sheet " + sheet.getSheetName());
        stream = new FileInputStream("src/main/resources/finance.xls");
        drl = converter.compile(stream, sheet.getSheetName());
        System.out.println("Drool drl added ::: " + drl);
        String DRL_FILE = RULE_PATH + sheet.getSheetName() + ".drl";
        System.out.println("Drool file added ::: " + DRL_FILE);
        kfs.write(DRL_FILE, ResourceFactory.newReaderResource(new StringReader(drl)));
        stream.close();
        break;
    }

    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();
    if (kb.getResults().hasMessages(Message.Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    long endTime = System.currentTimeMillis();
    System.out.println("Time to build rules : " + (endTime - startTime)  + " ms" );
    startTime = System.currentTimeMillis();
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    endTime = System.currentTimeMillis();
    System.out.println("Time to load container: " + (endTime - startTime)  + " ms" );

    return kContainer;
}

制作一个静态变量来分配和使用容器。

于 2020-03-26T06:35:46.857 回答