1 package org.jbpm.graph.node; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.dom4j.Element; 10 import org.jbpm.db.JbpmSession; 11 import org.jbpm.graph.action.Script; 12 import org.jbpm.graph.def.Node; 13 import org.jbpm.graph.exe.ExecutionContext; 14 import org.jbpm.graph.exe.Token; 15 import org.jbpm.jpdl.xml.JpdlXmlReader; 16 import org.jbpm.jpdl.xml.Parsable; 17 18 40 public class Fork extends Node implements Parsable { 41 42 private static final long serialVersionUID = 1L; 43 44 47 private Script script = null; 48 49 public Fork() { 50 } 51 52 public Fork(String name) { 53 super(name); 54 } 55 56 public void read(Element forkElement, JpdlXmlReader jpdlReader) { 57 Element scriptElement = forkElement.element("script"); 58 if (scriptElement!=null) { 59 script = new Script(); 60 script.read(scriptElement, jpdlReader); 61 } 62 } 63 64 public void execute(ExecutionContext executionContext) { 65 Token token = executionContext.getToken(); 66 Node forkNode = token.getNode(); 67 68 Collection transitionNames = null; 70 List forkedTokens = new ArrayList (); 71 72 if (script==null) { 74 transitionNames = forkNode.getLeavingTransitionsMap().keySet(); 75 76 } else { Map outputMap = script.eval(token); 80 if (outputMap.size()==1) { 81 Object result = outputMap.values().iterator().next(); 82 if (result instanceof Collection ) { 83 transitionNames = (Collection ) result; 84 } 85 } 86 if (transitionNames==null) { 87 throw new RuntimeException ("script for fork '"+name+"' should produce one collection (in one writable variable): "+transitionNames); 88 } 89 } 90 91 Iterator iter = transitionNames.iterator(); 93 while (iter.hasNext()) { 94 String transitionName = (String ) iter.next(); 95 forkedTokens.add(createForkedToken(token, transitionName)); 96 } 97 98 iter = forkedTokens.iterator(); 100 while( iter.hasNext() ) { 101 ForkedToken forkedToken = (ForkedToken) iter.next(); 102 Token childToken = forkedToken.token; 103 String leavingTransitionName = forkedToken.leavingTransitionName; 104 ExecutionContext childExecutionContext = new ExecutionContext(childToken); 105 if (leavingTransitionName!=null) { 106 leave(childExecutionContext, leavingTransitionName); 107 } else { 108 leave(childExecutionContext); 109 } 110 } 111 } 112 113 protected ForkedToken createForkedToken(Token parent, String transitionName) { 114 Token childToken = new Token(parent, getTokenName(parent, transitionName)); 116 117 JbpmSession jbpmSession = JbpmSession.getCurrentJbpmSession(); 119 if (jbpmSession!=null) { 120 jbpmSession.getSession().save(childToken); 122 } 123 124 ForkedToken forkedToken = null; 126 forkedToken = new ForkedToken(childToken, transitionName); 127 128 return forkedToken; 129 } 130 131 protected String getTokenName(Token parent, String transitionName) { 132 String tokenName = null; 133 if ( transitionName != null ) { 134 if ( ! parent.hasChild( transitionName ) ) { 135 tokenName = transitionName; 136 } else { 137 int i = 2; 138 tokenName = transitionName + Integer.toString( i ); 139 while ( parent.hasChild( tokenName ) ) { 140 i++; 141 tokenName = transitionName + Integer.toString( i ); 142 } 143 } 144 } else { int size = ( parent.getChildren()!=null ? parent.getChildren().size()+1 : 1 ); 146 tokenName = Integer.toString(size); 147 } 148 return tokenName; 149 } 150 151 public Script getScript() { 152 return script; 153 } 154 public void setScript(Script script) { 155 this.script = script; 156 } 157 158 private static class ForkedToken { 159 Token token = null; 160 String leavingTransitionName = null; 161 public ForkedToken(Token token, String leavingTransitionName) { 162 this.token = token; 163 this.leavingTransitionName = leavingTransitionName; 164 } 165 } 166 } 167 | Popular Tags |