1 package org.jbpm.graph.node; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.dom4j.Element; 9 import org.jbpm.graph.action.Script; 10 import org.jbpm.graph.def.Node; 11 import org.jbpm.graph.exe.ExecutionContext; 12 import org.jbpm.graph.exe.Token; 13 import org.jbpm.jpdl.xml.JpdlXmlReader; 14 import org.jbpm.jpdl.xml.Parsable; 15 16 public class Join extends Node implements Parsable { 17 18 private static final long serialVersionUID = 1L; 19 20 25 private boolean isDiscriminator = false; 26 27 30 private Collection tokenNames = null; 31 32 35 private Script script = null; 36 37 40 private int nOutOfM = -1; 41 42 43 public Join() { 44 } 45 46 public Join(String name) { 47 super(name); 48 } 49 50 public void read(Element element, JpdlXmlReader jpdlReader) { 51 } 52 53 public void execute(ExecutionContext executionContext) { 54 Token token = executionContext.getToken(); 55 56 if ( token.isAbleToReactivateParent() ) { 59 60 token.setAbleToReactivateParent(false); 63 64 Node joinNode = token.getNode(); 65 66 Token parentToken = token.getParent(); 67 if ( parentToken != null ) { 68 69 boolean reactivateParent = true; 70 71 if ( isDiscriminator ) { 73 reactivateParent = true; 78 79 } else if ( tokenNames != null ) { 81 reactivateParent = mustParentBeReactivated(parentToken, tokenNames.iterator() ); 83 84 } else if ( script != null ) { 86 87 Object result = script.eval( token ); 89 if ( result instanceof Collection ) { 91 Collection runtimeTokenNames = (Collection ) result; 93 reactivateParent = mustParentBeReactivated(parentToken, runtimeTokenNames.iterator() ); 94 95 96 } else if ( result instanceof Boolean ) { 98 reactivateParent = ((Boolean )result).booleanValue(); 100 } 101 102 } else if ( nOutOfM != -1 ) { 104 105 int n = 0; 106 Iterator iter = parentToken.getChildren().values().iterator(); 108 while ( iter.hasNext() ) { 109 Token concurrentToken = (Token)iter.next(); 110 if ( joinNode == concurrentToken.getNode() ) { 111 n++; 112 } 113 } 114 if ( n < nOutOfM ) { 115 reactivateParent = false; 116 } 117 118 } else { 120 reactivateParent = mustParentBeReactivated(parentToken, parentToken.getChildren().keySet().iterator() ); 123 } 124 125 if (reactivateParent) { 127 128 Iterator iter = parentToken.getChildren().values().iterator(); 130 while ( iter.hasNext() ) { 131 ((Token)iter.next()).setAbleToReactivateParent( false ); 132 } 133 134 ExecutionContext parentContext = new ExecutionContext(parentToken); 136 joinNode.leave(parentContext); 137 } 138 } 139 } 140 } 141 142 public boolean mustParentBeReactivated(Token parentToken, Iterator childTokenNameIterator) { 143 boolean reactivateParent = true; 144 while ( (childTokenNameIterator.hasNext()) 145 && (reactivateParent) ){ 146 String concurrentTokenName = (String ) childTokenNameIterator.next(); 147 148 Token concurrentToken = parentToken.getChild( concurrentTokenName ); 149 150 if (concurrentToken.isAbleToReactivateParent()) { 151 log.debug("join will not yet reactivate parent: found concurrent token '"+concurrentToken+"'"); 152 reactivateParent = false; 153 } 154 } 155 return reactivateParent; 156 } 157 158 public Script getScript() { 159 return script; 160 } 161 public void setScript(Script script) { 162 this.script = script; 163 } 164 public Collection getTokenNames() { 165 return tokenNames; 166 } 167 public void setTokenNames(Collection tokenNames) { 168 this.tokenNames = tokenNames; 169 } 170 public boolean isDiscriminator() { 171 return isDiscriminator; 172 } 173 public void setDiscriminator(boolean isDiscriminator) { 174 this.isDiscriminator = isDiscriminator; 175 } 176 public int getNOutOfM() { 177 return nOutOfM; 178 } 179 public void setNOutOfM(int nOutOfM) { 180 this.nOutOfM = nOutOfM; 181 } 182 183 private static final Log log = LogFactory.getLog(Join.class); 184 } 185 | Popular Tags |