LifeAndHope
LifeAndHope

Reputation: 724

How to get and set Local Variable List for a process in JBPM 6?

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

Answers (2)

Mike
Mike

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

isalgueiro
isalgueiro

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

Related Questions