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.Process;
21  import nextgrid.api.pom.ProcessController;
22  import nextgrid.api.pom.ProcessException;
23  import nextgrid.api.pom.SequenceProcess;
24  
25  /**
26   * Control process that forces all its children to execute in a sequential
27   * basis. A child process must wait until its previous sibling finishes its
28   * execution to start its own.
29   *
30   * @author Rodrigo Ruiz
31   */
32  public class SequenceProcessImpl extends ControlProcessImpl
33    implements SequenceProcess {
34  
35    /**
36     * <code>serialVersionUID</code> attribute.
37     */
38    private static final long serialVersionUID = -8304168648136245702L;
39  
40    /**
41     * Current child process being enacted.
42     */
43    private int index;
44  
45    /**
46     * Creates a new instance.
47     */
48    public SequenceProcessImpl() {
49      super(-1);
50    }
51  
52    /**
53     * {@inheritDoc}
54     */
55    @Override
56    protected final void doValidate(ValidationType when) throws ProcessException {
57      // No extra validation required here
58    }
59  
60    /**
61     * {@inheritDoc}
62     */
63    @Override protected void doReset() {
64      this.index = 0;
65    }
66  
67    // ========================================================================
68    // Enactable implementation
69    // ========================================================================
70  
71    /**
72     * {@inheritDoc}
73     */
74    public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
75      // No candidate list exists for this process, so...
76      // If a child process throws a ProcessException, it must be passed along
77      // to the parent process.
78      fireProcessStarted();
79      waitForInputs();
80  
81      Process[] children = getChildren();
82      int count = (children == null) ? 0 : children.length;
83  
84      while (this.index < count) {
85        if (!ctx.isRunning()) {
86          return;
87        }
88        ProcessController controller = children[this.index].enact(ctx);
89        controller.run();
90  
91        if (!ctx.isRunning()) {
92          return;
93        }
94        this.index++;
95      }
96  
97      if (ctx.isRunning()) {
98        fireProcessFinished();
99      }
100   }
101 
102   /**
103    * {@inheritDoc}
104    */
105   @Override public String toString() {
106     return "SequenceProcess#" + this.getId();
107   }
108 }