KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.def;
2
3 import java.util.Iterator JavaDoc;
4
5 import org.jbpm.graph.def.Transition;
6 import org.jbpm.graph.exe.ExecutionContext;
7
8 import org.jbpm.bpel.data.def.Snippet;
9 import org.jbpm.bpel.xml.util.DatatypeUtil;
10
11 /**
12  * The while activity supports repeated performance of a specified iterative activity.
13  * @see "WS-BPEL 2.0 §12.3"
14  * @author Juan Cantú
15  * @version $Revision: 1.6 $ $Date: 2005/06/23 02:22:46 $
16  */

17 public class While extends StructuredActivity {
18
19   private static final long serialVersionUID = 1L;
20
21   private Snippet condition;
22   private LoopNode loopNode;
23   
24   public While() {
25     super();
26     loopNode = new LoopNode(this);
27     setEvaluateFirst(true);
28     loopNode.connect(end);
29   }
30   
31   public While(String JavaDoc name) {
32     super(name);
33     loopNode = new LoopNode(this);
34     setEvaluateFirst(true);
35     loopNode.connect(end);
36   }
37   
38   // children management /////////////////////////////////////////////////////////////////////////////
39

40   protected void attachChild(Activity activity) {
41     if(nodes != null && nodes.size() > 0) {
42       detachChild((Activity) nodes.get(0));
43     }
44     super.attachChild(activity);
45   }
46   
47   protected void addImplicitTransitions(Activity activity) {
48     if(!getEvaluateFirst()) start.connect(activity);
49     
50     // Connect loop node to the activity
51
loopNode.connect(activity);
52     activity.connect(loopNode);
53   }
54   
55   protected void removeImplicitTransitions(Activity activity) {
56     if(!getEvaluateFirst()) start.disconnect(activity);
57     
58     // Disconnect loop node to the activity
59
loopNode.disconnect(activity);
60     activity.disconnect(loopNode);
61   }
62   
63   // while properties /////////////////////////////////////////////////////////////////////////////
64

65   public boolean getEvaluateFirst() {
66     return start.isConnected(loopNode);
67   }
68   
69   public void setEvaluateFirst(boolean evaluateFirst) {
70     if(evaluateFirst == getEvaluateFirst()) return;
71     
72     if(evaluateFirst) {
73       start.connect(loopNode);
74     }
75     else {
76       start.disconnect(loopNode);
77     }
78   }
79   
80   public Snippet getCondition() {
81     return condition;
82   }
83   
84   public void setCondition(Snippet condition) {
85     this.condition = condition;
86   }
87   
88   public LoopNode getLoopNode() {
89     return loopNode;
90   }
91
92   /**{@inheritDoc}*/
93   public void accept(BpelVisitor visitor) {
94     visitor.visit(this);
95   }
96   
97   /**
98    * Implements the loop behavior for the while activity
99    */

100   public static class LoopNode extends Activity {
101     private static final long serialVersionUID = 1L;
102     While whileBlock;
103     
104     LoopNode() {
105     }
106     
107     public LoopNode(While activity) {
108       this.whileBlock = activity;
109     }
110
111     public void execute(ExecutionContext context) {
112       Iterator JavaDoc transIt = getLeavingTransitions().iterator();
113       Transition transition = (Transition) transIt.next();
114       Snippet condition = whileBlock.getCondition();
115       
116       if( DatatypeUtil.toBoolean(condition.getScript().evaluate(context.getToken())).booleanValue() ) {
117         //first transition is successful
118
transition = (Transition) transIt.next();
119       }
120       
121       leave(context, transition);
122     }
123     
124     public CompositeActivity getCompositeActivity() {
125       return whileBlock;
126     }
127     
128     public void setCompositeActivity(CompositeActivity activity) {
129       whileBlock = (While) activity;
130     }
131     
132     public String JavaDoc getName() {
133       return whileBlock.getName();
134     }
135   }
136 }
Popular Tags