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 SequenceGoal is achieved by means of achieving its sub goals in
31 * order. The goal fails when and if any sub goal fails.
33 public class SequenceGoal extends Goal {
38 public SequenceGoal(String n,Goal [] sg) {
43 * Convenience constructor without sub goals.
45 public SequenceGoal(String n) {
50 * Creates and returns an instance object for achieving
53 public Instance instantiate(String head,Data d) {
54 return new SequenceInstance( head );
58 * Implements sequential goal execution.
60 public class SequenceInstance extends Instance {
65 public SequenceInstance(String h) {
70 * Keeps the currently ongoing subgoal instance
72 public Instance ongoing = null;
75 * Index of the currently ongoing sub goal.
82 public void cancel() {
84 if ( ongoing != null )
89 * Instantiates and performs sub goals in sequence.
91 public States action(String head,Data d)
92 throws LoopEndException, ParallelEndException {
93 Goal [] subgoals = getGoalSubgoals();
94 if ( subgoals == null )
95 return super.action( head, d );
96 while ( index < subgoals.length ) {
97 if ( ongoing == null ) {
98 ongoing = subgoals[ index ].instantiate(
99 head + "." + index, d );
101 States s = ongoing.perform( d );
102 if ( s != States.PASSED )
107 return States.PASSED;