无法在drools上运行hello world - KieContainer不会从类路径中选择dlr文件 [英] Can't run hello world on drools - dlr files are not picked from classpath by KieContainer

查看:180
本文介绍了无法在drools上运行hello world - KieContainer不会从类路径中选择dlr文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



来自doc:



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

  KieServices kieServices = KieServices.Factory.get(); 

KieContainer kContainer = kieServices.getKieClasspathContainer();

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



问题是drl(规则文件)没有被KieContainer加载到项目中,也没有应用到我的测试对象。<​​/ p>

测试方法:



前两行来自旧版本只是为了检查文件实际上是在类路径上。它确实找到了规则文件。规则文件位于: 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();

申请人申请人=新申请人(John Smith先生,16);

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

assertTrue(applicant.isValid());

kSession.insert(申请人);

kSession.fireAllRules();

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

输出:

  [main] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject  - 找到kmodule:file:/ Users /< MyUserName> /Dolols/target/classes/META-INF/kmodule.xml 
[main] WARN org.drools.compiler.kie.builder.impl.ClasspathKieProject - 无法在/ Users /< MyUserName> / Drools / target / classes中找到pom.properties
[main] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - 递归文件夹,找到并使用pom.xml /Users/<MyUserName>/Drools/pom.xml
[main] INFO org.drools.compiler .kie.builder.impl.KieRepositoryImpl - 添加了KieModule:FileKieModule [ReleaseId = drools:drools-test:6.2.0-SNAPSHOTfile = / Users /< MyUserName> / Drools / target / classes]

[main] WARN org.drools.compiler.kie.builder.impl.AbstractKieModule - 找不到KieBase HelloWorldKB的文件,搜索文件夹/ Users /< MyUserName> / Drools / target / classes

申请人{name ='Mr John Smith',年龄= 16岁,v alid = true}
申请人{name ='Mr John Smith',年龄= 16,有效=真}

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



我的pom使用相同的远程jboss远程repo和6.2.0 SNAPSHOT依赖项。 。



我缺少什么?



(因为我在这里失去了头发,额外的+ 50 / +100将被授予救世主,回复后接受)



(图片中忽略HelloWorld)



解决方案

这个咆哮已经过时了。似乎6.2.0只能作为SNAPSHOT(你最好单独留下)。[我找不到一个压缩的tarfile文件6.1.0-Final第一次尝试 - 稍后发现。]我不喜欢Drools发行版自5.6.0以来提供给社区的模糊方式。我通过简单下载得到的最后一个版本是6.0.0-最后。因此...... 咆哮结束。



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

  private KieSession kieSession ; 

public void build()抛出异常{
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();每个DRL文件的

//,由普通旧路径名引用:
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 = kieBuilder.getResults();
if(results.hasMessages(Message.Level.ERROR)){
System.out.println(results.getMessages());
抛出新的IllegalStateException(### errors ###);
}

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

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


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

From the doc:

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

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:.."

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

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.

        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...

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

What am I missing?

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

(ignore HelloWorld in the picture)

解决方案

(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.)

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

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

这篇关于无法在drools上运行hello world - KieContainer不会从类路径中选择dlr文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆