5

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.

Community
  • 1
  • 1
Muto
  • 140
  • 1
  • 10

1 Answers1

0
  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

Bashir
  • 2,057
  • 5
  • 19
  • 44
Gal Nitzan
  • 391
  • 2
  • 12
  • Hi there, thanks for your answer. I have to say I am confused. Could you please clarify what exactly your rule does and why it does that? Especially I am not familiar with the `drools.getContext(org.kie.api.runtime.process.ProcessContext.class)`. Could you extend the example to include not only a modification but also a replacement of a variable? – Muto Sep 13 '16 at 09:49
  • Hi again, i am sorry it took so much time to respond again, but I used another way to set my variables using script tasks and completely forgot about this question... I would gladly approve your answer, but I am still missing an important point: With the stuff you provided I was able to modify properties of an existing process variable. Sadly it was still impossible for me, to assign a new value to it. Could you provide an additional example for that? – Muto Nov 03 '16 at 13:14
  • Hi, I do not understand what you mean by "modify properties of existing process variable". Please look at the above code where I set a new java.util.List to qrr variable. Later on I add objects to this list in the rule. I am sorry if you are unable to use the above code but this is how it works and I have no more to offer. – Gal Nitzan Nov 06 '16 at 10:53
  • Sorry if I could not explain my needs explicitly enough. In your example the variable ist set in the Script and modified during the rule execution. I would like to generate an object during rule evaluation which is in the end Set as process-variable, without adding it to an existing collection. – Muto Nov 06 '16 at 12:30