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 nextgrid.api.pom.Expression;
21  import nextgrid.api.pom.ExpressionException;
22  import nextgrid.api.pom.IfThenElseProcess;
23  import nextgrid.api.pom.Process;
24  import nextgrid.api.pom.ProcessController;
25  import nextgrid.api.pom.ProcessException;
26  
27  /**
28   * Type description.
29   *
30   * @author Rodrigo Ruiz
31   */
32  public final class IfThenElseProcessImpl extends ControlProcessImpl
33    implements IfThenElseProcess, WithExpression {
34  
35    /**
36     * <code>serialVersionUID</code> attribute.
37     */
38    private static final long serialVersionUID = -5582636353281363056L;
39  
40    /**
41     * The conditional expression.
42     */
43    private Expression expr;
44  
45    /**
46     * Creates a new instance.
47     */
48    public IfThenElseProcessImpl() {
49      super(2);
50    }
51  
52    /**
53     * Gets the expr value.
54     *
55     * @return The expr
56     */
57    public Expression getExpression() {
58      return this.expr;
59    }
60  
61    /**
62     * Sets the expr value.
63     *
64     * @param expression The expr to set
65     */
66    public void setExpression(Expression expression) {
67      this.expr = expression;
68    }
69  
70    /**
71     * Gets the process in the "then" branch.
72     *
73     * @return The process in the "then" branch
74     */
75    public Process getThen() {
76      return this.getChildren(0);
77    }
78  
79    /**
80     * Gets the process in the "else" branch.
81     *
82     * @return The process in the "else" branch
83     */
84    public Process getElse() {
85      return this.getChildren(1);
86    }
87  
88    /**
89     * Sets the process in the "then" branch.
90     *
91     * @param process The process in the "then" branch
92     * @return The previous process in the "then" branch
93     */
94    public Process setThen(Process process) {
95      Process prev = this.getChildren(0);
96      this.setChildren(0, process);
97      return prev;
98    }
99  
100   /**
101    * Sets the process in the "else" branch.
102    *
103    * @param process The process in the "else" branch
104    * @return The previous process in the "else" branch
105    */
106   public Process setElse(Process process) {
107     Process prev = this.getChildren(1);
108     if (this.getChildCount() == 0) {
109       this.setChildren(null, process);
110     } else {
111       this.setChildren(1, process);
112     }
113     return prev;
114   }
115 
116   /**
117    * {@inheritDoc}
118    */
119   @Override protected void doValidate(ValidationType when)
120     throws ProcessException {
121     if (when == ValidationType.BEFORE_CHILDREN) {
122       if (this.expr == null) {
123         throw new ExpressionException("Null expression");
124       } else {
125         this.expr.validate();
126       }
127     }
128   }
129 
130   /**
131    * {@inheritDoc}
132    */
133   public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
134     fireProcessStarted();
135     waitForInputs();
136 
137     // Very naive approach!
138     Process p = (this.getExpression().boolEval(ctx, this)) ? getThen() : getElse();
139     if (p != null) {
140       ProcessController controller = p.enact(ctx);
141       controller.run();
142     }
143 
144     if (ctx.isRunning()) {
145       fireProcessFinished();
146     }
147 
148     // TODO Take into account pauses
149     // TODO Lock inputs during expression evaluation
150   }
151 
152   /**
153    * {@inheritDoc}
154    */
155   @Override protected void doReset() {
156   }
157 
158   /**
159    * {@inheritDoc}
160    */
161   @Override public String toString() {
162     return "IfThenElseProcess#" + this.getId();
163   }
164 }