Drools或vs和内部地图检查不一样

时间:2016-11-08 09:48:47

标签: java drools

第一个流口水的例子工作和解析:

package teeest
import java.util.ArrayList;
import java.util.HashMap;
rule 'test_and_comprassion'
when
  $result:java.util.ArrayList()
  $e:java.util.HashMap('TEST' == this['test'] && 'TEST2' == this['test'])
then
  $result.add(###DEF_ID###L);
end  

package teeest
import java.util.ArrayList;
import java.util.HashMap;
rule 'test_or_const_right_comprassion'
when
  $result:java.util.ArrayList()
  $e:java.util.HashMap(this['test'] == 'TEST' || this['test'] == 'TEST2')
then
  $result.add(###DEF_ID###L);
end;

逻辑在这里并不重要;)这是一个例子 不幸的是第二个,给我一个错误:

package teeest
import java.util.ArrayList;
import java.util.HashMap;
rule 'test_or_const_left_comprassion'
when
  $result:java.util.ArrayList()
  $e:java.util.HashMap('TEST' == this['test'] || 'TEST2' == this['test'])
then
  $result.add(###DEF_ID###L);
end;

我使用的流口水版本是5.6.0 Final(6.X现在不在选项中)。我不得不更新cmpiler,以便能够在jdk8下编译5.6:org.eclipse.jdt.core.compiler:4.4.2

我构建知识库的方式是:

private static KnowledgeBase knowledgeBase;
public static void addToKnowledgeBase(String droolDefinition) throws Exception {
    if(knowledgeBase == null) {
        createKnowledgeBase();
    }
    droolDefinition = droolDefinition.replaceAll("###DEF_ID###", "1");
    KnowledgeBuilder build = KnowledgeBuilderFactory.newKnowledgeBuilder();

    Resource droolsRule = ResourceFactory.newReaderResource(new StringReader(droolDefinition));
    build.add(droolsRule, ResourceType.DRL); //<-- error in there
    handleBuilderErrors(build);
    knowledgeBase.addKnowledgePackages(build.getKnowledgePackages());
}

public static void createKnowledgeBase() {
    KnowledgeBuilder build = KnowledgeBuilderFactory.newKnowledgeBuilder();
    KnowledgeBaseConfiguration configuration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    KnowledgeBase localKnowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(configuration);
    localKnowledgeBase.addKnowledgePackages(build.getKnowledgePackages());
    knowledgeBase = localKnowledgeBase;
}

在我们使用5.2.1.Final之前。但在那个版本中有很多错误。像左侧的谴责没有奏效。 null == field发出错误。以及“STRING&#39;以及&#39; STRING&#39; == field || &#39; STRING2&#39; ==字段给出了错误。在5.6.0.Final几乎所有都是固定的。但这是在最新的5.6.0.final。我猜这是一个错误。我无法改变左侧到右侧的同情以及更新到6.X版本。这个&#39;这个&#39;我猜是有问题的 有任何想法如何使用它?

堆栈跟踪:

[Line: 1, Column: 1]
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:427)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:140)
at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
at org.mvel2.MVELRuntime.execute(MVELRuntime.java:86)
at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
at org.mvel2.MVEL.executeExpression(MVEL.java:906)
at org.drools.rule.builder.PatternBuilder.getFieldValue(PatternBuilder.java:1521)
at org.drools.rule.builder.PatternBuilder.rewriteOrExpressions(PatternBuilder.java:762)
at org.drools.rule.builder.PatternBuilder.buildExpression(PatternBuilder.java:722)
at org.drools.rule.builder.PatternBuilder.buildCcdDescr(PatternBuilder.java:702)
at org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:653)
at org.drools.rule.builder.PatternBuilder.processConstraintsAndBinds(PatternBuilder.java:507)
at org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:324)
at org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:135)
at org.drools.rule.builder.GroupElementBuilder.build(GroupElementBuilder.java:67)
at org.drools.rule.builder.RuleBuilder.build(RuleBuilder.java:85)
at org.drools.compiler.PackageBuilder.addRule(PackageBuilder.java:3230)
at org.drools.compiler.PackageBuilder.compileRules(PackageBuilder.java:1038)
at org.drools.compiler.PackageBuilder.compileAllRules(PackageBuilder.java:946)
at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:938)
at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:470)
at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:698)
at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:51)
at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:40)
at pl.drools.DroolEvaluationUnitTestHelper.addToKnowledgeBase(DroolEvaluationUnitTestHelper.java:41)
at pl.drools.DroolEvaluationUnitTestHelper.droolTimerTestFromFile(DroolEvaluationUnitTestHelper.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.NullPointerException
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:393)
    ... 55 more
[Error: null pointer: this["test"]]
[Near : {... this["test"] ....}]
             ^
    [Line: 1, Column: 1]
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:427)
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:140)
    at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
    at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
    at org.mvel2.MVELRuntime.execute(MVELRuntime.java:86)
    at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
    at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
    at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
    at org.mvel2.MVEL.executeExpression(MVEL.java:906)
    at org.drools.rule.builder.PatternBuilder.getFieldValue(PatternBuilder.java:1521)
    at org.drools.rule.builder.PatternBuilder.rewriteOrExpressions(PatternBuilder.java:762)
    at org.drools.rule.builder.PatternBuilder.buildExpression(PatternBuilder.java:722)
    at org.drools.rule.builder.PatternBuilder.buildCcdDescr(PatternBuilder.java:702)
    at org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:653)
    at org.drools.rule.builder.PatternBuilder.processConstraintsAndBinds(PatternBuilder.java:507)
    at org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:324)
    at org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:135)
    at org.drools.rule.builder.GroupElementBuilder.build(GroupElementBuilder.java:67)
    at org.drools.rule.builder.RuleBuilder.build(RuleBuilder.java:85)
    at org.drools.compiler.PackageBuilder.addRule(PackageBuilder.java:3230)
    at org.drools.compiler.PackageBuilder.compileRules(PackageBuilder.java:1038)
    at org.drools.compiler.PackageBuilder.compileAllRules(PackageBuilder.java:946)
    at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:938)
    at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:470)
    at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:698)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:51)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:40)
    at pl.drools.DroolEvaluationUnitTestHelper.addToKnowledgeBase(DroolEvaluationUnitTestHelper.java:41)
    at pl.drools.DroolEvaluationUnitTestHelper.droolTimerTestFromFile(DroolEvaluationUnitTestHelper.java:81)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.NullPointerException
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:393)
    ... 55 more

0 个答案:

没有答案