View Javadoc

1   /*
2    Copyright (C) 2006 Grid Systems, S.A.
3   
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8   
9    This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13  
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18  package com.gridsystems.nextgrid.api.pom;
19  
20  import java.util.Map;
21  
22  import nextgrid.api.pom.Expression;
23  import nextgrid.api.pom.ExpressionException;
24  import nextgrid.api.pom.Process;
25  import nextgrid.api.pom.ProcessController;
26  import nextgrid.api.pom.ProcessException;
27  import nextgrid.api.pom.Reference;
28  import nextgrid.api.pom.WhileDoProcess;
29  
30  /**
31   * WhileDoProcess type.
32   *
33   * @author Rodrigo Ruiz
34   */
35  public final class WhileDoProcessImpl extends ControlProcessImpl
36    implements WhileDoProcess, WithExpression {
37  
38    /**
39     * <code>serialVersionUID</code> attribute.
40     */
41    private static final long serialVersionUID = -1324090284824130863L;
42  
43    /**
44     * Conditional expression to evaluate.
45     */
46    private Expression expr;
47  
48    /**
49     * Current loop count.
50     */
51    private int loop;
52  
53    /** Current child process being enacted. */
54    private int index;
55  
56    /**
57     * Creates a new instance.
58     */
59    public WhileDoProcessImpl() {
60      super(1);
61    }
62  
63    /**
64     * Gets the expression value.
65     *
66     * @return The expression
67     */
68    public Expression getExpression() {
69      return this.expr;
70    }
71  
72    /**
73     * Sets the expression value.
74     *
75     * @param expression The expression to set
76     */
77    public void setExpression(Expression expression) {
78      this.expr = expression;
79    }
80  
81    /**
82     * {@inheritDoc}
83     */
84    @Override
85    protected void doValidate(ValidationType when) throws ProcessException {
86      if (when == ValidationType.BEFORE_CHILDREN) {
87        if (this.expr == null) {
88          throw new ExpressionException("Null expression");
89        } else {
90          this.expr.validate();
91        }
92      }
93    }
94  
95    /**
96     * {@inheritDoc}
97     */
98    @Override protected void doReset() {
99      resetChildren();
100     this.loop = 0;
101     this.index = 0;
102   }
103 
104   // ========================================================================
105   // Enactable implementation
106   // ========================================================================
107 
108   /**
109    * {@inheritDoc}
110    */
111   public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
112     fireProcessStarted();
113 
114     // Very naive approach!
115     while (true) {
116       this.loop += 1;
117 
118       // Reset all local variables used by this loop
119       for (Map.Entry<String, Reference<?>> entry : this.getLocalVars().entrySet()) {
120         entry.getValue().reset();
121       }
122 
123       waitForInputs();
124       if (!this.getExpression().boolEval(ctx, this)) {
125         break;
126       }
127 
128       Process[] children = getChildren();
129       int count = (children == null) ? 0 : children.length;
130 
131       while (this.index < count) {
132         if (!ctx.isRunning()) {
133           return;
134         }
135         ProcessController controller = children[this.index].enact(ctx);
136         controller.run();
137 
138         this.index++;
139       }
140       this.index = 0;
141     }
142 
143     if (ctx.isRunning()) {
144       fireProcessFinished();
145     }
146   }
147 
148   /**
149    * {@inheritDoc}
150    */
151   @Override public String toString() {
152     return "WhileDoProcess#" + this.getId();
153   }
154 }