4

我目前开始使用 jbpm/drools 并尝试使用“业务规则任务”从我的 DRL 修改一些流程变量。我用一个进程尝试了以下操作,该进程声明了一个类型为“MyCustomObject”的变量“var”。

根据这个问题的结果和这个建议,我创建了一个任务,它应该执行规则流组“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 为空!

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

附加信息:

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

任何建议表示赞赏。

4

1 回答 1

0
  1. 将 Object 类型的名为 qrr 的变量添加到您的进程中

在业务规则任务的 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 contains: " + qrr.size() + " rule results ########");

HTH,
加尔

于 2016-09-06T14:06:38.193 回答