[jboss-user] [JBoss jBPM] - Confused about variable and token association

mteira do-not-reply at jboss.com
Mon Sep 4 10:34:45 EDT 2006


Hello. I'm just starting with jBPM and doing some JUnits to test if I understand how the thing works. I'm confused about how execution contexts and variables are bound. 

For example, I'm trying to setup a simple task with a fork node, the two branches of the fork are implementing a loop with two nodes: One that increments a variable and another one (a Condition node) where the value of that variable is compared with a maximum value and so , choose a transition to the previous node (the incrementer) or go to the join node.

My idea was to use variables binded to the execution context, and so, taking in mind that any branch should use it's own token, I could share the same name for the counter variable  in the two branches without interaction. But what I get is that it seems that the created variable is always bound to the root execution context. At least, the nodes in the two branches seem to see the same variable.

I try to use token bound variables using:
ContextInstance ci = executionContext.getContextInstance();
  | logger.debug("Executing under Token: " + executionContext.getToken());
  | ci.setVariable(varName, new Integer(0), executionContext.getToken());
  | 

That log shows a different Token when executing from different branches, as:
DEBUG IncrementTokenVar : Executing under Token: Token(/to_incr1)
and
DEBUG IncrementTokenVar : Executing under Token: Token(/to_incr2)

But the log from VariableContainer always shows:
VariableContainer : update variable 'localCounter' in 'TokenVariableMap914f6a' to value 'n'

Always showing 'TokenVariableMap914f6a' that I suppose is the root token variable map.

I'm also confused because ContextInstance has a method:
public Object getLocalVariable(String name, Token token);

but there's not a complementary one as setLocalVariable or so.

What I'm doing wrong?

Regards.



This is how my ActionHandler for the increment nodes action looks:


  | public class IncrementTokenVar implements ActionHandler {
  | 	static private Logger logger =  Logger.getLogger(IncrementTokenVar.class);
  | 	private static final long serialVersionUID = 1L;
  | 	private String varName;
  | 	public void execute(ExecutionContext executionContext) throws Exception {
  | 		ContextInstance ci = executionContext.getContextInstance();
  | 		logger.debug("Executing under Token: " + executionContext.getToken());
  | 		if (!ci.hasVariable(varName, executionContext.getToken())) {
  | 			ci.setVariable(varName, new Integer(0), executionContext.getToken());
  | 		}
  | 		Integer counter = (Integer) ci.getVariable(varName, executionContext.getToken());
  | 		Integer increment = new Integer(counter.intValue() + 1);
  | 		ci.setVariable(varName, increment, executionContext.getToken());
  | 	}
  | 
  | }
  | 

And this is the process XML:

<?xml version="1.0" encoding="UTF-8"?>
  | 
  | <process-definition
  |   xmlns="urn:jbpm.org:jpdl-3.1"  name="Forker">
  | 
  |    <!-- start transictions unconditionally to fork1 -->
  |    <start-state name="start">
  |       <transition name="to_fork" to="fork1"></transition>
  |    </start-state>
  | 
  |    <!-- Forks the process into two branches, doing the same -->
  |    <fork name="fork1">
  |       <transition name="to_incr1" to="incr1"></transition>
  |       <transition name="to_incr2" to="incr2"></transition>
  |    </fork>
  | 
  |   <!-- The first node of the branch where we increment the variable
  |          injected as varName using an ActionHandler-->
  |    <node name="incr1">
  |       <event type="node-enter">
  |          <action name="incrAction" class="com.jbay.IncrementTokenVar">
  |             <varName>localCounter</varName>
  |          </action>
  |       </event>
  |       <transition name="to_repeat1" to="repeat1"></transition>
  |    </node>
  | 
  |   <!-- The second node of the branch, where we decide to go to the
  |          previous node or exit to the join1, if the localCounter is greater
  |          or equal than 10 -->
  |   <decision name="repeat1">
  |    	  <transition name="repeatLoop" to="incr1"></transition>
  |       <transition name="endLoop1" to="join1">
  |       	<condition>#{localCounter >= 10}</condition>
  |       </transition>  
  |    </decision>
  | 
  |    <!-- The second branch, parallel to the first one -->
  |    <node name="incr2">
  |       <event type="node-enter">
  |          <action name="incrAction" class="com.jbay.IncrementTokenVar">
  |             <varName>localCounter</varName>
  |          </action>
  |       </event>
  |       <transition name="to_repeat2" to="repeat2"></transition>
  |    </node>
  |    <decision name="repeat2">
  |       <transition name="repeatLoop" to="incr2"></transition>      
  |       <transition name="endLoop" to="join1">
  |       	<condition>#{localCounter >= 10}</condition>
  |       </transition>
  |    </decision>
  | 
  |    <!-- Join the two branches -->
  |    <join name="join1">
  |       <transition name="to_end" to="end1"></transition>
  |    </join>
  |  
  | 
  |    <!-- End the process -->
  |    <end-state name="end1"></end-state>
  |    
  | </process-definition>
  | 
  | 


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969243#3969243

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969243



More information about the jboss-user mailing list