7 Replies Latest reply on Sep 23, 2005 8:52 AM by aguizar

    fork with dynamic number of legs

      Hi,

      I want to make a workflow definition as such:

      .....
      ....
      <fork name="fork" >
       <delegation class="org.jbpm.delegation.fork.DynamicNumberedForkHandler" />
       <transition to="nextstate" />
      </fork>
      <state name="nextstate">
       <transition to="endstate"/>
      </state>
      <end-state name="endstate"/>




      Then, I will put a variable in the workflow which will hold the number of legs I want to fork. I will start the process and when process comes to the fork, my customised fork handler will execute. It will first of all learn the number of legs wanted by getting the varibale I have put, from the executioncontext. And later on, I want to fork that many legs which all point to the same state (this state will be later a subprocess state actually).
      I have tried some tricks of code, but nothing worked out. How shall I write my customised fork handler?




      PS : The code I have tried was as follows:

      public class DynamicNumberedForkHandler implements ForkHandler {
      
       public void fork(ForkContext forkContext) throws ExecutionException
       {
       int i = Integer.valueOf(forkContext.getVariable("number_of_forks").toString()).intValue();
      
       NodeImpl node = (NodeImpl)forkContext.getNode();
       Transition transition = (Transition)node.getLeavingTransitions().iterator().next();
      
       for(int j=1; j<i;j++)
       {
       TransitionImpl newTransition = new TransitionImpl();
       newTransition.setActions(transition.getActions());
       newTransition.setDefinition((DefinitionImpl)transition.getDefinition());
       newTransition.setDescription(transition.getDescription());
       newTransition.setFrom((NodeImpl)transition.getFrom());
       newTransition.setTo((NodeImpl)transition.getTo());
       newTransition.setId(new Long(j));
       node.addLeavingTransition(newTransition);
       }
       i = 1;
       JoinImpl correspondingJoin = (JoinImpl)((Fork)node).getCorrespondingJoin();
       Iterator iter = forkContext.getNode().getLeavingTransitions().iterator();
       while(iter.hasNext())
       {
       Transition transition2 = (Transition) iter.next();
       TokenImpl childToken = (TokenImpl) forkContext.createToken( transition2, getTokenName(i,transition2) );
       childToken.setReactivationJoin( correspondingJoin );
       }
       }
      
       private String getTokenName(int index, Transition transition)
       {
       String tokenName = null;
       String transitionName = transition.getName();
       if ( transitionName != null )
       {
       tokenName = transitionName;
       }
       else
       {
       tokenName = "forked-child-"+Integer.toString( index+1 );
       }
       return tokenName;
       }
      
      }