0
votes

I can not fire a rule from jbpm business rule task. It seems that process not found rule file. Try to explain.

First, I extend a Drools example adding a business rule task in witch I set a ruleFlowGroup: enter image description here

This is properties for business rule task Rule1: enter image description here

The processTest.java simple set Message and start process:

package com.sample;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.sample.DroolsTest.Message;

/**
 * This is a sample file to launch a process.
 */
public class ProcessTest {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            KieSession kSession = kContainer.newKieSession("ksession-process");

            // go !
            Message message = new Message();
            message.setMessage("Hello Giorgio");
            message.setStatus(Message.HELLO);
            kSession.insert(message);

            // start a new process instance
            kSession.startProcess("com.sample.bpmn.hello");
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

}

Then I run and it fire correctly the script task ("Hello world") but non found rules in RuleGroup1.drl:

//created on: 21-gen-2019
package com.sample

//list any import classes here.
import com.sample.DroolsTest.Message;


//declare any global variables here


rule "First Giorgio rule"
    ruleflow-group "Group1"

    when
        m : Message( status == Message.HELLO, myMessage : message )
    then
        System.out.println( "rule0 Group1" );

end

rule "Your First Rule"
    ruleflow-group "Group1"
    when
        //conditions
    then
        System.out.println( "rule1 Group1" );

end

rule "Your Second Rule"
    ruleflow-group "Group1"
    //include attributes such as "salience" here...
    when
        //conditions
    then
        System.out.println( "rule2 Group1" );

end

Follow Project folders:

enter image description here

Thanks for your help.

2
To trigger rule execution you have to add 'ksession.fireAllRules()' in code. Try adding it.Abhijit Humbe
Correct, I forgot kSession.fireAllRules() but... it was not enough. Other think to do is modify kmodule.xml to add new package in which store all process and rules file. Now it work.Giorgio A.

2 Answers

1
votes

Make sure that the Rules file and the Process file are in the same folder. On the top of that, make sure that the package declared in the Rules file is consistent with the directory name where it's contained and the packages declaration in kmodule.xml. If they are out of sync, you will end up with your Rules not being fired. Check this example project. Using Drools Rules with jBPM

0
votes

I resolved to add a kSession.fireAllRules() and to store process and rule files in the same package.

  1. Modify kmodule.xml to add ksession name "processrules":

    <?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>
        <kbase name="dtables" packages="dtables">
            <ksession name="ksession-dtables"/>
        </kbase>
        <kbase name="process" packages="process">
            <ksession name="ksession-process"/>
        </kbase>
        <kbase name="processrules" packages="processrules">
            <ksession name="ksession-processrules"/>
        </kbase>
    </kmodule>
    
  2. Create a new package that link with ksession name. In my case processrules: enter image description here