简体   繁体   English

无法在drools上运行hello world - KieContainer不会从类路径中选择dlr文件

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

Following documentation: 6.1. 以下文件: 6.1。 The Basics I created a simple class Applicant which should be checked with drl file loaded from the class path by KieContainer. 基础知识我创建了一个简单的类申请人,应该使用KieContainer从类路径加载的drl文件进行检查。

From the doc: 来自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. 上面的代码片段编译了在类路径中找到的所有DRL文件,并将此编译的结果(KieModule)放在KieContainer中。 If there are no errors, we are now ready to create our session from the KieContainer and execute against some data:.. " 如果没有错误,我们现在准备从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. 问题是drl(规则文件)没有被KieContainer加载到项目中,也没有应用到我的测试对象。

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远程repo和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)

在此输入图像描述

( 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 - 发现这个我不喜欢Drools发行版自5.6.0以来提供给“社区”的晦涩方式。我通过简单的下载得到的最后一个版本是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();
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM