KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > node > Join


1 package org.jbpm.graph.node;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
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   /**
21    * specifies if this joinhandler is a discriminator.
22    * a descriminator reactivates the parent when the first
23    * concurrent token enters the join.
24    */

25   private boolean isDiscriminator = false;
26
27   /**
28    * a fixed set of concurrent tokens.
29    */

30   private Collection JavaDoc tokenNames = null;
31
32   /**
33    * a script that calculates concurrent tokens at runtime.
34    */

35   private Script script = null;
36   
37   /**
38    * reactivate the parent if the n-th token arrives in the join.
39    */

40   private int nOutOfM = -1;
41   
42
43   public Join() {
44   }
45
46   public Join(String JavaDoc 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 this token is not able to reactivate the parent,
57
// we don't need to check anything
58
if ( token.isAbleToReactivateParent() ) {
59
60       // the token arrived in the join and can only reactivate
61
// the parent once
62
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 this is a discriminator
72
if ( isDiscriminator ) {
73           // reactivate the parent when the first token arrives in the
74
// join. this must be the first token arriving because otherwise
75
// the isAbleToReactivateParent() of this token should have been false
76
// above.
77
reactivateParent = true;
78
79         // if a fixed set of tokenNames is specified at design time...
80
} else if ( tokenNames != null ) {
81           // check reactivation on the basis of those tokenNames
82
reactivateParent = mustParentBeReactivated(parentToken, tokenNames.iterator() );
83
84         // if a script is specified
85
} else if ( script != null ) {
86
87           // check if the script returns a collection or a boolean
88
Object JavaDoc result = script.eval( token );
89           // if the result is a collection
90
if ( result instanceof Collection JavaDoc ) {
91             // it must be a collection of tokenNames
92
Collection JavaDoc runtimeTokenNames = (Collection JavaDoc) result;
93             reactivateParent = mustParentBeReactivated(parentToken, runtimeTokenNames.iterator() );
94
95
96           // if it's a boolean...
97
} else if ( result instanceof Boolean JavaDoc ) {
98             // the boolean specifies if the parent needs to be reactivated
99
reactivateParent = ((Boolean JavaDoc)result).booleanValue();
100           }
101
102         // if a nOutOfM is specified
103
} else if ( nOutOfM != -1 ) {
104
105           int n = 0;
106           // wheck how many tokens already arrived in the join
107
Iterator JavaDoc 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         // if no configuration is specified..
119
} else {
120           // the default behaviour is to check all concurrent tokens and reactivate
121
// the parent if the last token arrives in the join
122
reactivateParent = mustParentBeReactivated(parentToken, parentToken.getChildren().keySet().iterator() );
123         }
124
125         // if the parent token needs to be reactivated from this join node
126
if (reactivateParent) {
127
128           // write to all child tokens that the parent is already reactivated
129
Iterator JavaDoc iter = parentToken.getChildren().values().iterator();
130           while ( iter.hasNext() ) {
131             ((Token)iter.next()).setAbleToReactivateParent( false );
132           }
133
134           // write to all child tokens that the parent is already reactivated
135
ExecutionContext parentContext = new ExecutionContext(parentToken);
136           joinNode.leave(parentContext);
137         }
138       }
139     }
140   }
141
142   public boolean mustParentBeReactivated(Token parentToken, Iterator JavaDoc childTokenNameIterator) {
143     boolean reactivateParent = true;
144     while ( (childTokenNameIterator.hasNext())
145             && (reactivateParent) ){
146       String JavaDoc concurrentTokenName = (String JavaDoc) 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 JavaDoc getTokenNames() {
165     return tokenNames;
166   }
167   public void setTokenNames(Collection JavaDoc 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