我无法在drools 6.0.1.Final中触发一个简单的规则。这是规则:
package org.drools.test
rule "test rule"
when
eval(true)
then
System.out.println("success");
end
这是测试代码,控制台不输出success
。
测试1
// Create kie file system
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
// Add rule
Resource resource = ResourceFactory.newByteArrayResource(src.getBytes());
kfs.write("org.drools.test.testrule.drl", resource);
kieServices.newKieBuilder(kfs).buildAll();
// Create session
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieBaseConfiguration kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, RuleBaseConfiguration.class.getClassLoader());
KieBase kieBase = kieContainer.newKieBase(kbaseConf);
KieSession kieSession = kieBase.newKieSession();
kieSession.insert("Test object");
// Fire
kieSession.fireAllRules();
kieSession.dispose();
相比之下,下面的代码段按预期运行,控制台输出success
。问题是方法newStatefulKnowledgeSession
已被弃用。据我所知,这不是在Drools 6中做事的方式。
测试2
Resource resource = ResourceFactory.newByteArrayResource(src.getBytes());
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(resource, ResourceType.DRL);
StatefulKnowledgeSession ksession = kbuilder.newKnowledgeBase().newStatefulKnowledgeSession();
ksession.insert("Test object");
ksession.fireAllRules();
ksession.dispose();
调试信息
我添加了一些调试行,试图了解测试1 中出现的问题。
kieSession.addEventListener(new DebugAgendaEventListener());
kieSession.addEventListener(new DebugRuleRuntimeEventListener());
输出结果为:
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:1:1784206373:1217562221:1:DEFAULT:NON_TRAIT:Test object], getObject()=Test object, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@4984cabc, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:1:1784206373:1217562221:1:DEFAULT:NON_TRAIT:Test object], leftTuple=null, originOffset=-1, propagationNumber=2, rule=null, type=0]]
而在测试2 中,输出为:
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:1:1784206373:1217562221:1:DEFAULT:NON_TRAIT:Test object], getObject()=Test object, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@9738e1b, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:1:1784206373:1217562221:1:DEFAULT:NON_TRAIT:Test object], leftTuple=null, originOffset=-1, propagationNumber=2, rule=null, type=0]]
==>[ActivationCreatedEvent: getActivation()=[[ test rule active=false ] [ null [fact 0:0:1771273200:1306428912:0:DEFAULT:NON_TRAIT:org.drools.core.reteoo.InitialFactImpl@4dde85f0] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@9738e1b]
==>[BeforeActivationFiredEvent: getActivation()=[[ test rule active=false ] [ null [fact 0:0:1771273200:1306428912:0:DEFAULT:NON_TRAIT:org.drools.core.reteoo.InitialFactImpl@4dde85f0] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@9738e1b]
success
==>[AfterActivationFiredEvent: getActivation()=[[ test rule active=false ] [ null [fact 0:0:1771273200:1306428912:0:DEFAULT:NON_TRAIT:org.drools.core.reteoo.InitialFactImpl@4dde85f0] ] ], getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@9738e1b]
底线
为什么测试1 没有触发规则,而测试2 是?
答案 0 :(得分:3)
如果直接使用KieFileSystem,则必须确保内容反映所需的maven结构。所以,你应该写:
kfs.write("src/main/resources/org.drools.test.testrule.drl", resource);
或者,在资源上设置sourcePath并直接写入:
resource.setSourcePath( "org.drools.test.testrule.drl" );
kfs.write( resource );