KieScanner 在运行时不更新 KieSessions

KieScanner not updating KieSessions at runtime

我将 Drools 与 Eclipse 和 Maven 一起用于执行多对多模式匹配的应用程序。我想使用 KieScanner 自动更新 运行 KieSession 而无需重新启动应用程序。但这似乎不起作用。

我为 org.kie 和 org.drools 使用 7.24.0.t043。

我只为此使用我的本地 Maven 存储库,所以我指定了路径并在我的 settings.xml 中设置为 true。我还验证了使用最新版本创建新的 KieContainer 会注册更改的规则。 KieScanner.scanNow() 似乎对我不起作用。我查看了错误日志,似乎根本没有任何错误,一切正常 运行。我很确定我也将每个版本正确地打包到 kjars 中并将它们安装在我的本地 Maven 存储库中。此外,我将我的新 kjar 版本设置为 2.0.0-SNAPSHOT。

kContainer.updateToVersion() 仅当我在 KieSession 设置期间在我的跑步者 class 开始时调用它时才对我有用,但如果我在下面的 while 循环中调用它则不起作用。

这里我在我的跑步者中设置我的 KieSession class:

KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("com.app", "my-app", "1.0.0");
KieContainer kContainer = ks.newKieContainer(releaseId);
KieScanner kScanner = ks.newKieScanner(kContainer);
kScanner.scanNow();
KieSession kSession = kContainer.getKieBase().newKieSession();

设置完成后,我将我的对象插入 kSession 并调用一次 fireAllRules()。

我正在使用 while 循环打印出对我的对象所做的更改(我已将其插入到 kSession 中):

while (true) {
  System.out.println("Matches of a1: " + a1.getMatches());
  TimeUnit.SECONDS.sleep(10);
}

我希望在更新规则后打印出更新的匹配项,但实际打印出来并没有改变匹配项。

Pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.app</groupId>
    <artifactId>genentech-maven-poc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Genentech Maven POC</name>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <runtime.version>7.24.0.t043</runtime.version>
    </properties>
    <repositories>
        <repository>
            <id>local-repo</id>
            <name>Local Maven Repo</name>
            <url>file://Users/mtsiang/.m2/repository</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.26</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>${runtime.version}</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-ci</artifactId>
            <version>${runtime.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>${runtime.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>${runtime.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

如果您需要我提供更多信息,请告诉我,我不确定我还需要包含哪些内容才能让您了解问题所在。

所以,我找到了原因。

指定 KieContainer 创建和 releaseId 时,请确保使用 "LATEST",如:

ReleaseId releaseId = ks.newReleaseId("com.app", "my-app", "LATEST"); //instead of "1.0.0"
KieContainer kContainer = ks.newKieContainer(releaseId);

这样,KieScanner 将读取 Maven 存储库 metadata.xml 文件中的 <release></release> 标记。

要触发新规则,您还需要调用 kSession.update(FACTHANDLE, OBJ);,然后调用 kSession.fireAllRules();