gpt4 book ai didi

drools - 无法从 DRL 更新 jbpm 流程变量

转载 作者:行者123 更新时间:2023-12-02 04:40:02 26 4
gpt4 key购买 nike

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

关注 this question 的结果和 this 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

变量为空!

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

附加信息:

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

任何建议表示赞赏。

最佳答案

  • 将 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 ########");
    哈,
    加尔

    关于drools - 无法从 DRL 更新 jbpm 流程变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38347949/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com