我正在尝试执行一个非常基本的示例来演示accumulate()
Drools 功能的使用,但是遇到了java.lang.NullPointerException
异常。
这是代码:
公制.java:
package com.sample;
public class Metric
{
public String id;
public double value;
public Metric(String id, double value)
{
this.id = id;
this.value = value;
}
}
规则文件1.drl:
package com.sample
declare Metric
@role( event )
end
rule "rule1"
when
Metric()
metricAverage: Number() from accumulate(
Metric( $value : value ),
average( $value ) )
then
System.out.println("metric value average " + metricAverage);
end
KSessionGenerator.java:
package com.sample;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class KSessionGenerator {
public KieSession buildKSession(String ruleFilePath){
KieServices kieServices = KieServices.Factory.get();
FileInputStream fis = null;
try {
fis = new FileInputStream(ruleFilePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
String virtualRuleFilePath = "src/main/resources/ruleFile1.drl";
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write(virtualRuleFilePath, 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());
// Change the engine mode to 'stream'
KieBaseConfiguration config = kieServices.newKieBaseConfiguration();
config.setOption( EventProcessingOption.STREAM );
KieBase kieBase = kieContainer.newKieBase( config );
KieSession kSession = kieBase.newKieSession();
return kSession;
}
}
DroolsTest1.java:
package com.sample;
import org.kie.api.runtime.KieSession;
public class DroolsTest1 {
public static final void main(String[] args) {
try {
String ruleFilePath = "src/main/resources/rules/ruleFile1.drl";
KieSession kSession = (new KSessionGenerator()).buildKSession(ruleFilePath);
// go !
Metric metric1 = new Metric("m1", 50);
Metric metric2 = new Metric("m2", 60);
kSession.insert(metric1);
kSession.insert(metric2);
System.out.println("Firing all rules ...");
kSession.fireAllRules();
System.out.println("Rules fired!");
System.out.println("Bye");
} catch (Throwable t) {
t.printStackTrace();
}
}
}
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>
异常的完整堆栈跟踪:
Firing all rules ...
java.lang.RuntimeException: java.lang.NullPointerException
at org.drools.core.rule.SingleAccumulate.accumulate(SingleAccumulate.java:90)
at org.drools.core.phreak.PhreakAccumulateNode.addMatch(PhreakAccumulateNode.java:759)
at org.drools.core.phreak.PhreakAccumulateNode.doLeftInserts(PhreakAccumulateNode.java:163)
at org.drools.core.phreak.PhreakAccumulateNode.doNode(PhreakAccumulateNode.java:80)
at org.drools.core.phreak.RuleNetworkEvaluator.switchOnDoBetaNode(RuleNetworkEvaluator.java:562)
at org.drools.core.phreak.RuleNetworkEvaluator.evalBetaNode(RuleNetworkEvaluator.java:533)
at org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:334)
at org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:161)
at org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:116)
at org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:235)
at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:106)
at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:1016)
at org.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1302)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1289)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1262)
at com.sample.DroolsTest1.main(DroolsTest1.java:20)
Caused by: java.lang.NullPointerException
at org.drools.core.rule.Declaration.getValue(Declaration.java:228)
at com.sample.Rule_rule11017979978AccumulateExpression0Invoker.evaluate(Rule_rule11017979978AccumulateExpression0Invoker.java:19)
at org.drools.core.base.accumulators.JavaAccumulatorFunctionExecutor.accumulate(JavaAccumulatorFunctionExecutor.java:109)
at org.drools.core.rule.SingleAccumulate.accumulate(SingleAccumulate.java:82)
... 15 more
我通过 Eclipse Mars 4.5.2 上的插件使用 Drools 版本 6.2.0 Final
谁能告诉我错误的原因及其解决方案?