0
我有麻煩解決了Drools 6.0.1.Final中的一條簡單規則。這是規則:Drools 6 - 如何着火規則
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()[email protected], 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()[email protected], 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:[email protected]] ] ], getKnowledgeRuntime()[email protected]]
==>[BeforeActivationFiredEvent: getActivation()=[[ test rule active=false ] [ null [fact 0:0:1771273200:1306428912:0:DEFAULT:NON_TRAIT:[email protected]] ] ], getKnowledgeRuntime()[email protected]]
success
==>[AfterActivationFiredEvent: getActivation()=[[ test rule active=false ] [ null [fact 0:0:1771273200:1306428912:0:DEFAULT:NON_TRAIT:[email protected]] ] ], getKnowledgeRuntime()[email protected]]
底線
爲什麼試驗1沒有觸發規則,而測試2是?
那不是幫助,如果火勢*方法,如果將知識拋出一個異常基地完全沒有規則?我相信我們也有類似的問題,即使在5.x – laune
感謝Davide,這個伎倆。 –