Reputation: 724
Is there a way to get and set the global/local variable list through code in JBPM 6?
I saw the documentation for JBPM version 3 like below:
ProcessInstance processInstance = ...;
ContextInstance contextInstance = (ContextInstance) processInstance.getInstance(ContextInstance.class);
But this seems deprecated, and gives me an error.
Additionally, I can set the variables through the bpmn editor using
kcontext.setVariable("isApproved", false);
But I'm not quite sure how to do retrieve this kcontext
variable in code. I went through the getter methods for both an KieSession
object and a ProcessInstance
object, but no luck.
Update:
I can access these local variables through the params
map object which is passed to the ksession.startProcess(...)
method. Is this the only way to get/set local/global variable lists?
Thanks!
Upvotes: 3
Views: 11351
Reputation: 832
Here is how I have been accessing my process variables
String variableName = "Your_Variable_Name_here";
KieSession ksession = runtime.getKieSession();
ProcessInstance pi = ksession.getProcessInstance(processInstanceId);
RuleFlowProcessInstance rfpi = (RuleFlowProcessInstance)pi;
Object variable = rfpi.getVariable(variableName);
You should then cast your variable into the proper class.
Upvotes: 3
Reputation: 2033
See this thread, you can access process instance variables executing this command in the 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;
}
});
If you just want to get one given process variable:
WorkflowProcessInstance p = (WorkflowProcessInstance)ksession.startProcess("the.process");
p.getVariable("the_process_variable")
To get all globals use ksession.getGlobals()
.
Upvotes: 2