3
votes

I am currently starting to work with jbpm/drools and tried to modify some process variables from my DRL using the "Business Rule Task". I tried the following with a process, which declared a variable "var" of type "MyCustomObject".

Following the results of this question and this recommendation I created a Task which should execute the ruleflow-group "testgroup" and has the following onEntry script:

kcontext.getKnowledgeRuntime().insert(kcontext.getProcessInstance());

My DRL now looks like this:

import mypackage.MyCustomObject;
import org.kie.api.runtime.process.WorkflowProcessInstance;

rule "generate object"
ruleflow-group "testgroup"

when
  //some stuff applies
then
  insert(new MyCustomObject());
end

rule "modify variable"
ruleflow-group "testgroup" 

when
  $process: WorkflowProcessInstance()
  $obj: MyCustomObject()
then
  WorkflowProcessInstance $p = (WorkflowProcessInstance)kcontext.getKieRuntime().getProcessInstance($process.getId());
  $p.setVariable( "var", $obj);
  System.out.println("Value of object in memory: "+$obj);
  System.out.println("Value of object in variable:+$p.getVariable("var"));
  retract($process);
end

After the Business Rule Task, I placed a simple Script Task:

if(var != null) {
  System.out.println("var: "+var);
} else{
  System.out.println("var is null!");  
}

The output I get is now (Note: MyCustomObject does not override toString):

Value of object in memory: MyCustomObject@XYZ

Value of object in variable: MyCustomObject@XYZ

var is null!

At this point I have no idea, what went wrong. As implied by the output, the ProcessInstance in the working memory has correctly set its variable, but the value is not present in the process itself (ergo for other nodes to access).

Additional Information:

I currently use the workbench version 6.4.0.Final on a JBoss EAP 6.4 and deploy the containers to a KieExecutionServer (6.4.0.Final) running on a separate EAP 6.4 instance.

Any suggestions are appreciated.

1

1 Answers

0
votes
  1. Add a variable named qrr of type Object to your process

In your onEntry script of the business rule task:

// Add the process instance into working memory so we can access it from rules!!!
insert(kcontext.getProcessInstance());

// get the current process context (the running process) where I have already
// defined a variable named qrr as a type Object.
org.jbpm.workflow.instance.WorkflowProcessInstance pi = (org.jbpm.workflow.instance.WorkflowProcessInstance)kcontext.getProcessInstance();
    
// create a new array list
qrr = new java.util.ArrayList();

// to be able to access qrr from the business process I set the new qrr
// instance to the BP variable named qrr
pi.setVariable("qrr", qrr);

// log to log file    
System.out.println("=======> qrr inserted ");

rule example

rule "check states"
ruleflow-group "build-results"
dialect "java"
when
  /* when there is an object of type PatientState with an attribute named trasferrable(boolean) equals to true in the working memory */
  $s : PatientState(trasferrable==true)
then
  
  String str = "We found our PatientState in working memory and it has transferable==true!";
    
  /* get the process context we inserted into the working memory before we ran our business rule task with ruleflow: build-results */
  Object o = drools.getContext(org.kie.api.runtime.process.ProcessContext.class);
    
  if (o != null) {
    // we found the ProcessContext in working memory so cast it and than get our variable named qrr and cast it as well
    List qrr = (List)drools.getContext(org.kie.api.runtime.process.ProcessContext.class).getVariable("qrr");
    // add the string object to the list
    qrr.add(str);
  }
  else {
    LoggerUtil.info("Context not found in working memory");
  }
end

Now in your onExit script
just write something like:

System.out.println("######## qrr contains: " + qrr.size() + " rule results ########");

HTH,
Gal