简体   繁体   中英

Drools 6 - How to Fire Rule

I am having trouble firing a simple rule in drools 6.0.1.Final. This is the rule :

package org.drools.test
rule "test rule"
when
    eval(true)
then
    System.out.println("success");
end

This is the test code, the console does not output success .

Test 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();

In comparison, the code snippet below runs as expected, and the console outputs success . The problem is that the method newStatefulKnowledgeSession is deprecated. As I understand it, this is not the way to do things in Drools 6.

Test 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();

Debug Info

I added some debug lines, to try and understand what is going wrong in Test 1 .

kieSession.addEventListener(new DebugAgendaEventListener());
kieSession.addEventListener(new DebugRuleRuntimeEventListener());

The output is :

==>[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]]

Whereas in Test 2 , the output is :

==>[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]

Bottom line

Why is Test 1 not firing the rule, whereas Test 2 is ?

If you use the KieFileSystem directly, you must ensure that the content reflects the required maven structure. So, you should write:

kfs.write("src/main/resources/org.drools.test.testrule.drl", resource);

In alternative, set the sourcePath on the resource and write it directly:

resource.setSourcePath( "org.drools.test.testrule.drl" );
kfs.write( resource );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM