Drools-能否在运行时定义规则?

问题描述 投票:15回答:4

可以在运行时完全动态地定义规则吗?同时在运行时更改规则集。

例如

at time = t_1 the ruleset is { R1, R2, R3 } 
at time = t_2 the ruleset is { R1, R3, R4 } 
..... 

R1,R2是在运行时定义的规则。

谢谢。

dynamic runtime rules drools
4个回答
19
投票

是的,Drools是完全动态的。您可以在运行时添加/删除/替换规则。所有现有会话都会自动更新,以保持一致性。检查文档/ javadoc:

http://www.jboss.org/drools/documentation

这里您可以看到一些集成测试:

DynamicRulesTest.java


2
投票

阅读文档,并查看有关“ KnowledeAgent”的测试,以了解您可以做什么以及如何去做。


0
投票

在Drools 5中,您只需要更改KnowledgeBase,它将隐式更新KnowledgeSession。这就是集成测试告诉您的。实际上,您实际上不需要KAgent。


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;
}

使用静态变量分配该方法容器。我创建了一个端点来调用此方法。

© www.soinside.com 2019 - 2024. All rights reserved.