1 package org.jbpm.bpel.def; 2 3 import java.util.Collection ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import org.jbpm.bpel.exe.LinkInstance; 9 import org.jbpm.graph.exe.ExecutionContext; 10 import org.jbpm.graph.exe.Token; 11 12 18 public class Flow extends StructuredActivity { 19 20 private Map links = new HashMap (); 21 22 private static final long serialVersionUID = 1L; 23 24 public Flow() { 25 } 26 27 public Flow(String name) { 28 super(name); 29 } 30 31 33 public void execute(ExecutionContext context) { 34 Token token = context.getToken(); 35 if(createVariableContext()) token = new Token(token, getName()); 36 initLinks(token); 37 38 Map flowTokens = createFlowTokens( token ); 40 Iterator iter = flowTokens.keySet().iterator(); 42 while( iter.hasNext() && !token.hasEnded()) { 44 Activity child = (Activity) iter.next(); 45 Token forkedToken = (Token) flowTokens.get(child); 46 ExecutionContext childExecutionContext = new ExecutionContext(forkedToken); 47 start.leave(childExecutionContext, child.getDefaultArrivingTransition()); 48 } 49 } 50 51 public void leave(ExecutionContext context) { 52 Token token = context.getToken(); 53 if ( !token.isAbleToReactivateParent() ) return; 56 token.setAbleToReactivateParent(false); 59 Token parentToken = token.getParent(); 61 if (mustParentBeReactivated(parentToken)) { 62 if(createVariableContext()) { 64 parentToken.setAbleToReactivateParent( false ); 65 parentToken = parentToken.getParent(); 66 } 67 getEnd().leave(new ExecutionContext(parentToken)); 68 } 69 } 70 71 boolean createVariableContext() { 72 return !isInitial() && !links.isEmpty(); 73 } 74 75 Map createFlowTokens(Token parent) { 76 Collection children = getNodes(); 77 Map flowTokens = new HashMap (children.size()); 78 Iterator iter = children.iterator(); 79 while (iter.hasNext()) { 80 Activity child = (Activity) iter.next(); 81 Token forkedToken = new Token(parent, getTokenName(parent, child.getName())); 82 flowTokens.put(child, forkedToken); 83 } 84 return flowTokens; 85 } 86 87 public static String getTokenName(Token parent, String transitionName) { 88 String tokenName = null; 89 if ( transitionName != null ) { 90 if ( ! parent.hasChild( transitionName ) ) { 91 tokenName = transitionName; 92 } else { 93 int i = 2; 94 tokenName = transitionName + Integer.toString( i ); 95 while ( parent.hasChild( tokenName ) ) { 96 i++; 97 tokenName = transitionName + Integer.toString( i ); 98 } 99 } 100 } else { int size = ( parent.getChildren()!=null ? parent.getChildren().size()+1 : 1 ); 102 tokenName = Integer.toString(size); 103 } 104 return tokenName; 105 } 106 107 public static boolean mustParentBeReactivated(Token parentToken) { 108 boolean reactivateParent = true; 109 Iterator childTokenNameIterator = parentToken.getChildren().keySet().iterator(); 110 while ( (childTokenNameIterator.hasNext()) 111 && (reactivateParent) ){ 112 String concurrentTokenName = (String ) childTokenNameIterator.next(); 113 114 Token concurrentToken = parentToken.getChild( concurrentTokenName ); 115 116 if (concurrentToken.isAbleToReactivateParent()) { 117 reactivateParent = false; 118 } 119 } 120 return reactivateParent; 121 } 122 123 public boolean isChildInitial(Activity activity) { 124 return true; 125 } 126 127 129 public Link findLink(String linkName) { 130 Link link = getLink(linkName); 131 return link != null ? link : super.findLink(linkName); 132 } 133 134 void initLinks(Token token) { 135 if(links.size() > 0) { 136 for(Iterator it = links.keySet().iterator(); it.hasNext();) 137 LinkInstance.create(token, (String )it.next()); 138 } 139 } 140 141 public void addLink(Link link) { 142 links.put(link.getName(), link); 143 } 144 145 public Collection getLinks() { 146 return links.values(); 147 } 148 149 public Link getLink(String linkName) { 150 return (Link) links.get(linkName); 151 } 152 153 154 public void accept(BpelVisitor visitor) { 155 visitor.visit(this); 156 } 157 } 158 | Popular Tags |