KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > def > Flow


1 package org.jbpm.bpel.def;
2
3 import java.util.Collection JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.jbpm.bpel.exe.LinkInstance;
9 import org.jbpm.graph.exe.ExecutionContext;
10 import org.jbpm.graph.exe.Token;
11
12 /**
13  * The flow activity provides concurrency and synchronization.
14  * @see "WS-BPEL 2.0 §12.5"
15  * @author Juan Cantú
16  * @version $Revision: 1.7 $ $Date: 2005/06/23 20:27:17 $
17  */

18 public class Flow extends StructuredActivity {
19
20   private Map JavaDoc links = new HashMap JavaDoc();
21   
22   private static final long serialVersionUID = 1L;
23   
24   public Flow() {
25   }
26   
27   public Flow(String JavaDoc name) {
28     super(name);
29   }
30   
31   // behaviour methods /////////////////////////////////////////////////////////////////////////////
32

33   public void execute(ExecutionContext context) {
34     Token token = context.getToken();
35     if(createVariableContext()) token = new Token(token, getName());
36     initLinks(token);
37     
38     // phase one: collect all the flow tokens
39
Map JavaDoc flowTokens = createFlowTokens( token );
40     // phase two: launch child tokens from the fork over the given transitions
41
Iterator JavaDoc iter = flowTokens.keySet().iterator();
42     //stop spawning the new childs if the parent token is completed abruptly
43
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 this token is not able to reactivate the parent,
54
// we don't need to check anything
55
if ( !token.isAbleToReactivateParent() ) return;
56     // the token arrived in the join and can only reactivate
57
// the parent once
58
token.setAbleToReactivateParent(false);
59     // if the parent token needs to be reactivated from this join node
60
Token parentToken = token.getParent();
61     if (mustParentBeReactivated(parentToken)) {
62       //if a token was created for scoping link status remove it
63
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 JavaDoc createFlowTokens(Token parent) {
76     Collection JavaDoc children = getNodes();
77     Map JavaDoc flowTokens = new HashMap JavaDoc(children.size());
78     Iterator JavaDoc 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 JavaDoc getTokenName(Token parent, String JavaDoc transitionName) {
88     String JavaDoc 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 { // no transition name
101
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 JavaDoc childTokenNameIterator = parentToken.getChildren().keySet().iterator();
110     while ( (childTokenNameIterator.hasNext())
111             && (reactivateParent) ){
112       String JavaDoc concurrentTokenName = (String JavaDoc) 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   // Link methods /////////////////////////////////////////////////////////////////////////////
128

129   public Link findLink(String JavaDoc 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 JavaDoc it = links.keySet().iterator(); it.hasNext();)
137         LinkInstance.create(token, (String JavaDoc)it.next());
138     }
139   }
140   
141   public void addLink(Link link) {
142     links.put(link.getName(), link);
143   }
144
145   public Collection JavaDoc getLinks() {
146     return links.values();
147   }
148   
149   public Link getLink(String JavaDoc linkName) {
150     return (Link) links.get(linkName);
151   }
152
153   /**{@inheritDoc}*/
154   public void accept(BpelVisitor visitor) {
155     visitor.visit(this);
156   }
157 }
158
Popular Tags