Java drools 6.0 中的未知 KieSession 名称(尝试将 drools 添加到现有的 maven/eclipse 项目中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21466716/
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-13 08:51:43  来源:igfitidea点击:

Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project)

javaeclipsemavendrools

提问by kashili kashili

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

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

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

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

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

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

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

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

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)

3)关于解决空指针异常的任何提示(假设我正在采取正确且简单的方法将现有项目转换为drools项目)

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

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

采纳答案by David Bernard

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.

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

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

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

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

我采用了一个有效的 eclipse 示例,并确保我可以在 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".
  1. 创建了一个演示 drools 项目 File->New->Other..->Drools->Drools Project
  2. 确保您可以运行测试程序 DroolsTest
  3. 将项目转换为 Maven 项目 - Configure->Convert To Maven Project(这将创建一个具有许多依赖项的 pom.xml 文件。这些可以修剪)
  4. 从构建路径中删除了 Drools 库 - 在项目属性中构建路径 -> 库 - 选择 Drools 库并单击删除
  5. 禁用 Drools 构建器 - 在项目属性 Builders -> 取消选中 Drools Builder
  6. 从 pom.xml 中注释掉依赖 jsr94(不可检索)
  7. 从命令行“mvm clean install”运行 maven。

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

这应该为您提供一个完全从 Maven 构建和运行的项目。

Add to your pom.xml

添加到您的 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"

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.

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

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

您将拥有一个 rules->Sample.drl 文件和一个 kmodule.xml 文件。

<?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:

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

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

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

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

回答by Mark Proctor

We have a number of examples, all documented. that get you started. Each can be run from the command line with maven, and have unit test to show them running. https://github.com/droolsjbpm/drools/tree/master/drools-examples-api

我们有很多例子,都记录在案。让你开始。每个都可以从命令行使用 maven 运行,并进行单元测试以显示它们正在运行。 https://github.com/droolsjbpm/drools/tree/master/drools-examples-api

Docs here: http://docs.jboss.org/drools/release/6.0.1.Final/drools-docs/html/KIEChapter.html#KIEExamplesSection

文档在这里:http: //docs.jboss.org/drools/release/6.0.1.Final/drools-docs/html/KIEChapter.html#KIEExamplesSection

回答by ProfVersaggi

I think there is a much simpler answer... I stumbled upon this just today as I was attempting the very same thing and got it working after following a clue in the discussion lists:

我认为有一个更简单的答案......我今天偶然发现了这个,因为我正在尝试同样的事情并在遵循讨论列表中的线索后让它工作:

I was Trying to get Drools to execute from Spring MVC WEB in Pure Maven Project (no added natures of any kind) and ran head first into this problem:

我试图让 Drools 从纯 Maven 项目中的 Spring MVC WEB 执行(没有任何类型的添加性质),并首先遇到了这个问题:

ERROR KieContainerImpl - Unknown KieSession name: ksession-rules

错误 KieContainerImpl - 未知的 KieSession 名称:ksession-rules

Initial RND: (some direction - but no solutions)

初始 RND:(一些方向 - 但没有解决方案)

Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project)http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project-td4027944.html#a4028011

drools 6.0 中的未知 KieSession 名称(尝试将 drools 添加到现有的 maven/eclipse 项目时)http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project -td4027944.html#a4028011

Related Problem (... but a bigclue):

相关问题(...但一个很大的线索):

WARN ClasspathKieProject - Unable to load pom.properties tried recursing down from/Apache-Tomcat7/webapps/maven-spring-drools/WEB-INF/classes

警告 ClasspathKieProject - 无法加载 pom.properties 尝试从/Apache-Tomcat7/webapps/maven-spring-drools/WEB-INF/classes 向下递归

SOLUTION:

解决方案:

As it turns out, creating the directory WEB-INF/classesand placing the files pom.propertiesand pom.xmlin it solves the problem. The clue came from this link: http://drools.46999.n3.nabble.com/Drools-6-Unable-to-build-index-of-kmodule-xml-td4026791.html- where Mark Proctor indicated that the project has to be a maven project, and it needs to find pom.properties.

事实证明,创建目录WEB-INF/classes并将文件pom.propertiespom.xml放在其中解决了问题。线索来自这个链接:http: //drools.46999.n3.nabble.com/Drools-6-Unable-to-build-index-of-kmodule-xml-td4026791.html- Mark Proctor 指出该项目必须是 Maven 项目,并且需要找到 pom.properties

As soon as the KIE finds the pom.properties file, it figures out the KieSession name and the problem is solved.

一旦 KIE 找到 pom.properties 文件,它就会找出 KieSession 名称,问题就解决了。

Note: I'd suspect you'd have to manually keep that POM.XML file updated but the pom.properties file never changes.

注意:我怀疑您必须手动更新 POM.XML 文件,但 pom.properties 文件永远不会更改。

This has been my experience - hope it helps someone....

这是我的经验-希望它可以帮助某人....

回答by Alex

I faced a similar issue while trying out the JBoss Examples at: 4.2.6.1. Default KieSession http://docs.jboss.org/drools/release/6.4.0.Final/drools-docs/html/ch04.html#KIEExamplesSection

我在尝试 JBoss 示例时遇到了类似的问题:4.2.6.1。默认 KieSession http://docs.jboss.org/drools/release/6.4.0.Final/drools-docs/html/ch04.html#KIEExamplesSection

I was getting the following exception on trying out the default KieSession example:

我在尝试默认的 KieSession 示例时遇到以下异常:

org.drools.compiler.kie.builder.impl.ClasspathKieProject - Unable to build index of kmodule.xml url=file:/C:/Repo/Git/temp/drools-examples/drools-examples/drools-examples-service/target/classes/META-INF/kmodule.xml
org.xml.sax.SAXParseException; systemId: file:/C:/Repo/Git/temp/drools-examples/drools-examples/drools-examples-service/target/classes/META-INF/kmodule.xml; lineNumber: 1; columnNumber: 52; cvc-elt.1.a: Cannot find the declaration of element 'kmodule'.
10:29:18.243 [main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession name: testsession

I realised that the issue was due to the xmlns used in the default kiession example from Jboss Docs: From JBoss Docs - HAS ISSUE:

我意识到这个问题是由于 Jboss Docs 的默认 kiession 示例中使用的 xmlns:来自 JBoss Docs - HAS ISSUE:

kmodule xmlns="http://www.drools.org/xsd/kmodule"

</kmodule>

What I used - WORKS WITHOUT ISSUE:

我使用的 - 工作没有问题:

kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"

</kmodule>

回答by V.Dev

Little late on the answer here, but this might help others.

这里的答案有点晚,但这可能会帮助其他人。

Check your META-INF/Maven/pom.properties file, and it might contain this:

检查您的 META-INF/Maven/pom.properties 文件,它可能包含以下内容:

groupId=
artifactId=
version=

Nothing was defined! Changed it to:

什么都没有定义!改为:

groupId=com.sample  (package name)
artifactId=DroolsTest (class name)
version=0.0.1-SNAPSHOT  

Copy and paste the same data from pom.properties file to pom.xml . Runs fine now.

将相同的数据从 pom.properties 文件复制并粘贴到 pom.xml 。现在运行良好。

回答by Kumaresan Perumal

In my experience I was trying to add the following maven dependency in pom.xml after removing those dependencies. it worked for me.

根据我的经验,我试图在删除这些依赖项后在 pom.xml 中添加以下 maven 依赖项。它对我有用。

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>${runtime.version}</version>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-decisiontables</artifactId>
        <version>${runtime.version}</version>
    </dependency>

    <dependency>
        <groupId>org.jbpm</groupId>
        <artifactId>jbpm-test</artifactId>
        <version>${runtime.version}</version>
    </dependency>