如何在JBPM 6中获取和设置进程的本地变量列表?

时间:2015-04-01 19:24:24

标签: java jbpm

有没有办法通过JBPM 6中的代码获取和设置全局/局部变量列表?

我看到了JBPM版本3的文档,如下所示:

ProcessInstance processInstance = ...;
ContextInstance contextInstance = (ContextInstance) processInstance.getInstance(ContextInstance.class);

但这似乎已被弃用,并给我一个错误。

另外,我可以使用

通过bpmn编辑器设置变量
kcontext.setVariable("isApproved", false);

但我不太确定如何在代码中检索此kcontext变量。我对KieSession对象和ProcessInstance对象都进行了getter方法,但没有运气。

更新: 我可以通过传递给params方法的ksession.startProcess(...) map对象访问这些局部变量。这是获取/设置本地/全局变量列表的唯一方法吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

以下是我访问流程变量的方法

String variableName = "Your_Variable_Name_here";
KieSession ksession = runtime.getKieSession();
ProcessInstance pi = ksession.getProcessInstance(processInstanceId);
RuleFlowProcessInstance rfpi = (RuleFlowProcessInstance)pi;
Object variable = rfpi.getVariable(variableName);

然后,您应该将变量转换为适当的类。

答案 1 :(得分:2)

参见this thread,您可以访问在KieSession中执行此命令的流程实例变量:

Map<String, Object> variables = ksession.execute(new GenericCommand<Map<String, Object>>() {  

            public Map<String, Object> execute(Context context) {  
                StatefulKnowledgeSession ksession = ((KnowledgeCommandContext) context).getStatefulKnowledgesession();  
                ProcessInstance processInstance = (ProcessInstance) ksession.getProcessInstance(piId);  
                VariableScopeInstance variableScope = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);  
                Map<String, Object> variables = variableScope.getVariables();  
                return variables;  
            }  
        }); 

如果您只想获得一个给定的过程变量:

WorkflowProcessInstance p = (WorkflowProcessInstance)ksession.startProcess("the.process");
p.getVariable("the_process_variable")

要使所有全局变量使用ksession.getGlobals()