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.RepeatUntilProcess;
29  
30  /**
31   * RepeatUntilProcess type.
32   *
33   * @author Rodrigo Ruiz
34   */
35  public final class RepeatUntilProcessImpl extends ControlProcessImpl
36    implements RepeatUntilProcess, WithExpression {
37  
38    /**
39     * <code>serialVersionUID</code> attribute.
40     */
41    private static final long serialVersionUID = -3099620746842223062L;
42  
43    /**
44     * Conditional expression to evaluate.
45     */
46    private Expression expr;
47  
48    /**
49     * Current child process being enacted.
50     */
51    private int index;
52  
53    /**
54     * Creates a new instance.
55     */
56    public RepeatUntilProcessImpl() {
57      super(1);
58    }
59  
60    /**
61     * Gets the expr value.
62     *
63     * @return The expr
64     */
65    public Expression getExpression() {
66      return this.expr;
67    }
68  
69    /**
70     * Sets the expr value.
71     *
72     * @param expression The expr to set
73     */
74    public void setExpression(Expression expression) {
75      this.expr = expression;
76    }
77  
78    /**
79     * {@inheritDoc}
80     */
81    @Override
82    protected void doValidate(ValidationType when) throws ProcessException {
83      if (when == ValidationType.BEFORE_CHILDREN) {
84        if (this.expr == null) {
85          throw new ExpressionException("Null expression");
86        } else {
87          this.expr.validate();
88        }
89      }
90    }
91  
92    /**
93     * {@inheritDoc}
94     */
95    public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
96      fireProcessStarted();
97  
98      // Very naive approach!
99      while (true) {
100 
101       // Reset all local variables used by this loop
102       for (Map.Entry<String, Reference<?>> entry : this.getLocalVars().entrySet()) {
103         entry.getValue().reset();
104       }
105 
106       waitForInputs();
107       Process[] children = getChildren();
108       int count = (children == null) ? 0 : children.length;
109 
110       while (this.index < count) {
111         if (!ctx.isRunning()) {
112           return;
113         }
114         ProcessController controller = children[this.index].enact(ctx);
115         controller.run();
116 
117         if (!ctx.isRunning()) {
118           return;
119         }
120         this.index++;
121       }
122       this.index = 0;
123 
124       if (this.getExpression().boolEval(ctx, this)) {
125         break;
126       }
127 
128     }
129 
130     if (ctx.isRunning()) {
131       fireProcessFinished();
132     }
133   }
134 
135   /**
136    * {@inheritDoc}
137    */
138   @Override protected void doReset() {
139   }
140 
141  /**
142    * {@inheritDoc}
143    */
144   @Override public String toString() {
145     return "RepeatUntilProcess#" + this.getId();
146   }
147 }