1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
4 This file is part of GORITE.
6 GORITE is free software: you can redistribute it and/or modify it
7 under the terms of the Lesser GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GORITE is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the Lesser GNU General Public
17 License along with GORITE. If not, see <http://www.gnu.org/licenses/>.
18 **********************************************************************/
20 package com.intendico.gorite;
22 import com.intendico.data.Query;
23 import com.intendico.data.Ref;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Vector;
30 * A LoopGoal is achieved by means of repeatedly achieving its sub
31 * goals in sequence, until eventually an embedded {@link EndGoal} is
32 * achieved. The usage pattern for this is as follows:
35 * new LoopGoal( "loop", new Goal [] {
36 * .. // steps in the loop
37 * .. new EndGoal( "break if", new Goal [] {
38 * // optional sub goals for ending the loop if they succeed
40 * .. // more steps in the loop
44 * There may be any number of {@link EndGoal} to end the loop although
45 * of course only one will take effect. The {@link EndGoal} may be at
46 * any level within the LoopGoal goal hierarchy, except nested within
52 public class LoopGoal extends SequenceGoal {
57 public LoopGoal(String n,Goal [] sg) {
62 * Convenience constructor without sub goals.
64 public LoopGoal(String n) {
69 * Creates and returns an instance object for achieving
72 public Instance instantiate(String head,Data d) {
73 return new LoopInstance( head );
77 * Implements repeated, sequential exception.
79 public class LoopInstance extends SequenceInstance {
84 public LoopInstance(String h) {
91 * Repeatedly process all subgoals in sequence, until a
92 * thrown LoopEndException is caught.
94 public States action(String head,Data d)
95 throws LoopEndException, ParallelEndException {
99 s = super.action( head + ":" + count, d );
100 } catch (LoopEndException e) {
104 if ( s != States.PASSED )
108 return States.PASSED;