无法从DRL更新jbpm进程变量

时间:2016-07-13 09:39:38

标签: drools jbpm kie-wb

我目前正在开始使用jbpm / drools并尝试使用"业务规则任务"从我的DRL修改一些流程变量。我尝试了以下过程,它声明了一个变量" var"类型" MyCustomObject"。

根据this questionthis recommendation的结果,我创建了一个任务,该任务应该执行规则流组" testgroup"并具有以下onEntry脚本:

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

我的DRL现在看起来像这样:

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

在业务规则任务之后,我放置了一个简单的脚本任务:

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

我现在得到的输出(注意:MyCustomObject不会覆盖toString):

对象在内存中的值:MyCustomObject @ XYZ

变量中对象的值:MyCustomObject @ XYZ

var为null!

此时我不知道,出了什么问题。正如输出所暗示的那样,工作内存中的ProcessInstance已正确设置其变量,但该过程本身不存在该值(其他节点可以访问)。

其他信息:

我目前在JBoss EAP 6.4上使用工作台版本6.4.0.Final,并将容器部署到在单独的EAP 6.4实例上运行的KieExecutionServer(6.4.0.Final)。

任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

  1. 将名为qrr的Object类型的变量添加到您的流程
  2. 在业务规则任务的onEntry脚本中:

    // 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 "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
    

    现在在你的onExit脚本中 只写出类似的东西:

    System.out.println(" ######## qrr包含:" + qrr.size()+"规则结果########& #34);

    HTH, 伽