drools 6.0中的未知KieSession名称(同时尝试将流水添加到现有的maven / eclipse项目) [英] Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project)

查看:3173
本文介绍了drools 6.0中的未知KieSession名称(同时尝试将流水添加到现有的maven / eclipse项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使drools6.0适应现有的代码库(它是eclipse下的maven项目)。
我没有需要学习流口水或maven(虽然他们是我之前的项目的一部分),足以说我迷失在我想做的事情。
根据我的理解(googling),java类文件根据包名(?)挂钩到规则。这需要处理编译时问题。但是在运行时我看到空指针异常。为了将流口水适应我现有的代码库:I 1)创建helloworld drools项目,成功运行2)将java文件复制到我现有的包中,3)使用正确的包在Eclipse中创建规则文件:FIle-> New-> other - >规则资源; 3)通过右键单击项目将配置文件转换为drools包,并配置 - >转换为drools项目



这一切都负责编译问题,但是我得到以下运行时错误

  [main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl  -  Unknown KieSession name:ksession-rules 
java.lang.NullPointerException
在main.java.com.harmonia.cbm.afloat.dataaquisition.dql.DroolsTest.main(DroolsTest.java:23)

这是因为从kcontainer返回的ksession为null,并在最后一行抛出空指针异常

  KieServices ks = KieServices.Factory.get(); 
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession(ksession-rules);
//以上行返回null
消息消息= new Message();
message.setMessage(Hello World);
message.setStatus(Message.HELLO);
kSession.insert(message);

已经花了一天多的时间来弄清楚流口水如何工作,以及如何修复。 Pl建议



1)我采取正确的方法将现有项目转换为drools项目。我想要我的代码库的所有现有功能;但是希望为未来的增强添加基于规则的方法。来到以下链接,但不清楚,如果它有助于我的情况
http://drools.46999.n3.nabble.com/Retrofitting-a-project-with-JBoss-Rules-td48656.html



2)任何有用的drools教程,以更好地理解以下3行(除了java文档)

  KieServices ks = KieServices.Factory.get(); 
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession(ksession-rules);

3)解决空指针异常的任何提示(假设我采取正确和简单的转换方法现有项目进入流水项目)



更新
@David:感谢详细的帖子。我意识到,将现有项目转换为maven项目,同时工作,并没有吸引我,因为现有的目录结构/命名被保留(很可能不同于maven默认创建的)。我发布了替代解决方案,我认为这个问题与类路径问题有关 http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project-td4027944.html# a4028011

解决方案

我遇到类似的问题。



我认为这个问题的一部分是试图生活在这两个世界。 JBoss Drools eclipse插件世界和maven世界。



我有安装了各种Jboss / Drools插件的Eclipse 4.3.1(Kepler)。



我采取了一个工作的日食示例,并确保我可以在maven中运行它。


  1. 创建一个演示流口水项目文件 - >新建 - >其他..-> Drools-> Drools项目

  2. 确保您可以运行测试程序DroolsTest

  3. 将项目转换为maven项目 - 配置 - >转换为Maven项目
    (这将创建一个具有许多依赖关系的pom.xml文件,可以是prunes)

  4. 从构建中删除Drools库路径 - 在项目属性中构建路径 - >库 - 选择Drools库并单击删除

  5. 禁用Drools构建器 - 在项目属性中构建器 - >取消选中Drools Builder

  6. 从pom.xml中注释出依赖关系jsr94(不可检索)

  7. 从命令行mvm clean install运行maven。

这应该给你一个完全从Maven构建和运行的项目。



添加到您的pom.xml

 < plugin> 
< groupId> org.codehaus.mojo< / groupId>
< artifactId> exec-maven-plugin< / artifactId>
< version> 1.2.1< / version>
< / plugin>

 <依赖性> 
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-simple< / artifactId>
< version> 1.7.2< / version>
< scope>运行时< / scope>
< / dependency>

尝试:



mvn -e exec :java -Dexec.mainClass =com.sample.DroolsTest



它应该产生:

 

...
[com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - 找到kmodule :file:/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes/META-INF/kmodule.xml
[com.sample.DroolsTest.main()] INFO org.drools.compiler .kie.builder.impl.KieRepositoryImpl - 添加了KieModule:FileKieModule [ReleaseId = x:x:1.0file = / Users / davidbernard / Projects / action-deducing-diff / xx / target / classes]
[ sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Found kmodule:file:/ Users / davidbernard / Projects / action-deducing-diff / xx / target / classes / META-INF /kmodule.xml
[com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.KieRepositoryImpl - 添加了KieModule:FileKieModule [ReleaseId = x:x:1.0file = /用户/天avidbernard / Projects / action-deducing-diff / xx / target / classes]
Hello World
再见残酷世界
...

您现在还可以从eclipse运行DroolsTest。



您将拥有规则 - > Sample.drl文件和kmodule.xml文件。

 <?xml version =1.0encoding = UTF-8\" >?; 
< kmodule xmlns =http://jboss.org/kie/6.0.0/kmodule>
< kbase name =rulespackages =rules>
< ksession name =ksession-rules/>
< / kbase>
< / kmodule>

ksession名称应与创建ksession的代码相匹配:

  KieSession kSession = kContainer.newKieSession(ksession-rules); 

包应与规则文件所在的目录相匹配。


I am trying to adapt drools6.0 for an existing code base (it is maven project under eclipse). I didnt had need to learn drools or maven before (though they were part of my previous project), suffice to say I am lost in what I wanted to do. Based on my understanding (googling), java class files get hooked to rules based on the package name(?). That takes care of compile time issues. But I am seeing null pointer exception at run time. Inorder to adapt drools into my existing code base: I 1)created helloworld drools project, ran it successfully 2)copied the java file to my existing package, 3)created rule file in Eclipse with correct package: FIle->New->other->Rule Resource; 3)converted existing project into drools package by right clicking project and configure->convert to drools project

This all takes care of compilation issues, but I get following run time error

[main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession    name: ksession-rules
java.lang.NullPointerException
at main.java.com.harmonia.cbm.afloat.dataaquisition.dql.DroolsTest.main(DroolsTest.java:23)

This is because ksession that is returned from kcontainer is null and throws null pointer exception in last line

KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// above line is returning null
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
kSession.insert(message);

Already spent more than a day trying to figure out how drools works and how above can be fixed. Pl suggest

1) am I taking the right approach to convert existing project into drools project. I want all existing functionality of my code base; but want to add rules based approach for future enhancements. Came across following link, but not clear if it helps my situation http://drools.46999.n3.nabble.com/Retrofitting-a-project-with-JBoss-Rules-td48656.html

2)Any useful drools tutorials in better understanding following 3 lines (besides java docs)

KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");

3)Any hints on resolving null pointer exception (assuming I am taking the right and easy approach of converting existing project into drools project)

UPDATE @David: thanks for detailed post. I realized that converting existing project into maven project, while works, did not appeal to me since existing directory structure/naming is preserved (most likely different from what maven creates by default). I posted alternative solution where I thought this problem has to do with classpath issues http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project-td4027944.html#a4028011

解决方案

I hit similar problems.

I think that part of the problem is trying to live in both worlds. The JBoss Drools eclipse plugin world and the maven world.

I have Eclipse 4.3.1 (Kepler) with various Jboss/Drools plugins installed.

I took a working eclipse example and made sure I could run it in maven.

  1. Created a demo drools project File->New->Other..->Drools->Drools Project
  2. Ensured you could run the test programs DroolsTest
  3. Converted project to maven project - Configure->Convert To Maven Project (This will create a pom.xml file with many dependencies. These can be prunes)
  4. Removed the Drools Library from the build path - in the project properties Build Path -> Libraries - select Drools Library and click Remove
  5. Disable the Drools builder - in project properties Builders -> uncheck Drools Builder
  6. Comment out dependancy jsr94 from the pom.xml(not retrievable)
  7. Run maven from the command line "mvm clean install".

This should give you a project that builds and runs entirely from Maven.

Add to your pom.xml

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
  </plugin>

And

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.2</version>
  <scope>runtime</scope>
</dependency>

Try:

mvn -e exec:java -Dexec.mainClass="com.sample.DroolsTest"

It should produce:


...
[com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Found kmodule: file:/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes/META-INF/kmodule.xml
[com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.KieRepositoryImpl - KieModule was added:FileKieModule[ ReleaseId=x:x:1.0file=/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes]
[com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Found kmodule: file:/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes/META-INF/kmodule.xml
[com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.KieRepositoryImpl - KieModule was added:FileKieModule[ ReleaseId=x:x:1.0file=/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes]
Hello World
Goodbye cruel world
...

You should now also be able to run DroolsTest from eclipse.

You will have a rules->Sample.drl file and a kmodule.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
    </kbase>
</kmodule>

The "ksession" name should match the code creating the ksession:

KieSession kSession = kContainer.newKieSession("ksession-rules");

The "packages" should match the directory the rule file is in.

这篇关于drools 6.0中的未知KieSession名称(同时尝试将流水添加到现有的maven / eclipse项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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