[jboss-user] [JBoss jBPM] - Re: replication of task at runtime

WalterTaus do-not-reply at jboss.com
Mon Jun 9 07:01:42 EDT 2008


Hi,
for the sake of completeness, here is now my complete code.


  | public class MultiinstanceLoopForkHandler implements ActionHandler {
  | 	private String loopMaxValue;
  | 	private String loopId;
  | 	private int loopType;
  | 
  | 	@Override
  | 	public void execute(ExecutionContext ctx) throws Exception {
  | 		// TODO Auto-generated method stub
  | 		
  | 		Token token = ctx.getToken();
  | 		Fork fork = (Fork)ctx.getNode();
  | 		int i;
  | 		try {	
  | 			i = Integer.parseInt(loopMaxValue);
  | 		}
  | 		catch (Exception e) {
  | 			try {
  | 				if (ctx.getVariable(loopMaxValue) != null)
  | 					i = (Integer)ctx.getVariable(loopMaxValue);
  | 				else
  | 					i = 1;
  | 			}
  | 			catch(Exception e1) {
  | 				e1.printStackTrace();
  | 				i = 1;
  | 			}
  | 		}
  | 		if (loopType == 1)
  | 			ctx.setVariable(loopId + "MaxCount", 1);
  | 		else
  | 			ctx.setVariable(loopId + "MaxCount", i);
  | 		Transition leavingTrans = (Transition)fork.getLeavingTransitions().get(0);
  |         for (int j = 1; j<i; j++) {
  |             String newTokenName = loopId + j;
  |             String transitionName = leavingTrans.getName();
  |             Token newToken = new Token(token, newTokenName);
  |             ctx.getJbpmContext().getSession().save(newToken);
  |             fork.leave(new ExecutionContext(newToken), transitionName);
  |          }
  | 	}
  | 
  | }
  | 


  | public class MultiinstanceLoopJoinHandler implements ActionHandler {
  | 	private String loopId;
  | 	@Override
  | 	public void execute(ExecutionContext ctx) throws Exception {
  | 		// TODO Auto-generated method stub
  | 		if (ctx.getVariable(loopId + "Housekeeping") != null)
  | 			return;
  | 		ctx.setVariable(loopId + "Housekeeping", 1);
  | 		Token t = ctx.getToken();
  | 		Collection til = ctx.getTaskMgmtInstance().getTaskInstances();
  | 		Map childTokenList = t.getParent().getChildren();
  | 		Set ks = childTokenList.keySet();
  | 		Iterator it = ks.iterator();
  | 		while (it.hasNext()) {
  | 			Token ct = (Token)childTokenList.get(it.next());
  | 			if (ct.getName().equals(t.getName()) && ct.getId() == t.getId())
  | 				continue;
  | 			if (ct.getName() != null && !ct.getName().startsWith(loopId))
  | 				continue;
  | 			Iterator itil = til.iterator();
  | 			while (itil.hasNext()) {
  | 				TaskInstance ti = (TaskInstance)itil.next();
  | 				if (ti.getToken() == ct) {
  | 					ti.cancel();
  | 					ct.end();
  | 					break;
  | 				}
  | 			}
  | 		}
  | 	}
  | 
  | }
  | 
  | 

And here is an example process.

  | <fork name="ForkOne">
  | <event type="node-enter">
  | <action class="com.pi.bpm.workflow.jbpm.MultiinstanceLoopForkHandler"> 
  | <loopMaxValue>3</loopMaxValue> 
  | <loopId>MultinstanceOne</loopId>
  | <loopType>1</loopType>
  | </action>
  | </event>
  | <transition name="MultinstanceOne" to="MultinstanceOne"></transition>
  | </fork>
  | <task-node name="MultinstanceOne">
  | <transition name="ToJoinOne" to="JoinOne"></transition>
  | <task name="MultinstanceOne"></task>
  | </task-node>
  | <join name="JoinOne">
  | <event type="node-enter">
  | <script><expression>
  | if (executionContext.getVariable("MultinstanceOneMaxCount") > 1) executionContext.getNode().setNOutOfM(executionContext.getVariable("MultinstanceOneMaxCount"));
  | else executionContext.getNode().setDiscriminator(true);
  | </expression></script>
  | <action class="com.pi.bpm.workflow.jbpm.MultiinstanceLoopJoinHandler">
  | <loopId>MultinstanceOne</loopId>
  | </action>
  | </event>
  | <transition name="To_cF5fADE6Ed2C6L5mprqn1w" to="../UserTask5/UserTask5">
  | </transition>
  | </join>
  | </super-state>
  | 

BTW with this sequence I can handle the BPMN constructs of Multiinstance Loop All and Multiinstance Loop One.

ANy suggestions and improvements are highly welcome.

  Walter


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

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



More information about the jboss-user mailing list