Java 无法在 drools 上运行 hello world - KieContainer 未从类路径中选取 dlr 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24558451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:59:19  来源:igfitidea点击:

Can't run hello world on drools - dlr files are not picked from classpath by KieContainer

javadrools

提问by Aubergine

Following documentation: 6.1. The BasicsI created a simple class Applicant which should be checked with drl file loaded from the class path by KieContainer.

以下文档:6.1。基础知识我创建了一个简单的类申请人,应该使用 KieContainer 从类路径加载的 drl 文件进行检查。

From the doc:

从文档:

"At this point it is possible to create a KieContainer that reads the files to be built, from the classpath.

此时,可以创建一个 KieContainer,它可以从类路径中读取要构建的文件。

KieServices kieServices = KieServices.Factory.get();

KieContainer kContainer = kieServices.getKieClasspathContainer();

The above code snippet compiles all the DRL files found on the classpath and put the result of this compilation, a KieModule, in the KieContainer. If there are no errors, we are now ready to create our session from the KieContainer and execute against some data:.."

上面的代码片段编译了在类路径上找到的所有 DRL 文件,并将编译的结果,一个 KieModule,放在 KieContainer 中。如果没有错误,我们现在准备从 KieContainer 创建我们的会话并针对一些数据执行:...

The problem is that the drl (rules files) are not loaded into the project by the KieContainer, and not applied to my test object.

问题是 KieContainer 没有将 drl(规则文件)加载到项目中,也没有应用于我的测试对象。

Test method:

测试方法:

first two lines are from the older version just to check that the file is actually on the class path. And it does find the rules file. The rules files is located under: src/main/resources/bla/checkLicense.drl- correctly under resources.

前两行来自旧版本,只是为了检查文件是否确实在类路径上。它确实找到了规则文件。规则文件位于:src/main/resources/bla/checkLicense.drl- 正确地在资源下。

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); 

        kbuilder.add(ResourceFactory.newClassPathResource("bla/checkLicense.drl"), ResourceType.DRL);

        KieServices kieServices = KieServices.Factory.get();

        KieContainer kContainer = kieServices.getKieClasspathContainer();

        KieSession kSession = kContainer.newKieSession();

        Applicant applicant = new Applicant("Mr John Smith",16);

        System.out.println(applicant.toString());

        assertTrue(applicant.isValid());

        kSession.insert(applicant);

        kSession.fireAllRules();

        System.out.printf(applicant.toString());
        assertFalse(applicant.isValid());

The output:

输出:

[main] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Found kmodule: file:/Users/<MyUserName>/Drools/target/classes/META-INF/kmodule.xml
[main] WARN org.drools.compiler.kie.builder.impl.ClasspathKieProject - Unable to find pom.properties in /Users/<MyUserName>/Drools/target/classes
[main] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Recursed up folders,  found and used pom.xml /Users/<MyUserName>/Drools/pom.xml
[main] INFO org.drools.compiler.kie.builder.impl.KieRepositoryImpl - KieModule was added:FileKieModule[ ReleaseId=drools:drools-test:6.2.0-SNAPSHOTfile=/Users/<MyUserName>/Drools/target/classes]

[main] WARN org.drools.compiler.kie.builder.impl.AbstractKieModule - No files found for KieBase HelloWorldKB, searching folder /Users/<MyUserName>/Drools/target/classes

Applicant{name='Mr John Smith', age=16, valid=true}
Applicant{name='Mr John Smith', age=16, valid=true}

The applicant object stayed the same, while should've become invalid after rules invocation if the rule file was actually founded and loaded. The warning message does not appear for the git test projects provided by drools community...

申请人对象保持不变,而如果规则文件被实际建立和加载,则应该在规则调用后失效。drools社区提供的git测试项目没有出现警告信息...

My pom uses the same remote jboss remote repo and 6.2.0 SNAPSHOT dependencies...

我的 pom 使用相同的远程 jboss 远程仓库和 6.2.0 SNAPSHOT 依赖项...

What am I missing?

我错过了什么?

(since I am loosing my hair here, the additional +50/+100 will be awarded to the saviour, post answer acceptance)

(因为我在这里掉头发,额外的+50/+100将奖励给救世主,回复后接受)

(ignore HelloWorld in the picture)

(忽略图中的HelloWorld)

enter image description here

在此处输入图片说明

采纳答案by laune

(This rant is obsolete.It seems 6.2.0 is only available as a SNAPSHOT (which you'd better leave alone). [And I couldn't find a zipped tarfile for the 6.1.0-Final on first try - found this later.] I dislike the obscure ways Drools distributions since 5.6.0 are offered to the "community". The last version I managed to get with a simple download was 6.0.0-Final. And therefore... End of rant.)

这个咆哮已经过时了。似乎 6.2.0 只能作为 SNAPSHOT 使用(你最好不要管它)。[而且我在第一次尝试时找不到 6.1.0-Final 的压缩 tarfile - 找到了这个] 我不喜欢自 5.6.0 以来向“社区”提供 Drools 发行版的晦涩方式。我通过简单下载设法获得的最后一个版本是 6.0.0-Final。因此......咆哮结束。

A simple technique for compiling one or more drl files programmatically that works since 6.0.0 is this:

从 6.0.0 开始,一种以编程方式编译一个或多个 drl 文件的简单技术是:

private KieSession kieSession;

public void build() throws Exception {
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kfs = kieServices.newKieFileSystem();

    // for each DRL file, referenced by a plain old path name:
    FileInputStream fis = new FileInputStream( "simple/simple.drl" );
    kfs.write( "src/main/resources/simple.drl",
                kieServices.getResources().newInputStreamResource( fis ) );

    KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();
    Results results = kieBuilder.getResults();
    if( results.hasMessages( Message.Level.ERROR ) ){
        System.out.println( results.getMessages() );
        throw new IllegalStateException( "### errors ###" );
    }

    KieContainer kieContainer =
    kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );

    KieBase kieBase = kieContainer.getKieBase();
    kieSession = kieContainer.newKieSession();
}

回答by Aubergine

Found the solution, the package with DLR must be declared inside your: kmodule.xml

找到解决方案,必须在您的:kmodule.xml 中声明带有 DLR 的包

Like this:

像这样:

<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="HelloWorldKB" packages="bla" default="true">
        <ksession name="HelloWorldKS" default="true"/>
    </kbase>

Started to pick up my drls after this.

在这之后开始拿起我的drls。

community.jboss.org/thread/242518

community.jboss.org/thread/242518