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 ConditionGoal is achieved by achieving any sub goal, which are
31 * attempted in sequence. The goal fails when and if all sub goals
32 * have been attempted and failed.
34 public class ConditionGoal extends Goal {
39 public ConditionGoal(String n,Goal [] sg) {
44 * Convenience constructor without sub goals.
46 public ConditionGoal(String n) {
51 * Creates and returns an instance object for achieving
54 public Instance instantiate(String head,Data d) {
55 return new ConditionInstance( head );
59 * Implements sequential, conditional sub goal execution
60 * attempt. I.e., if a sub goal fails, then try next, otherwise
63 public class ConditionInstance extends Instance {
68 public ConditionInstance(String h) {
73 * Next sub goal index.
78 * Current ongoing Instance.
80 public Instance ongoing = null;
83 * Cancels this execution by propagating the cancel call to
86 public void cancel() {
88 if ( ongoing != null )
93 * Instantiates and performs sub goals in sequence, until the
94 * first one that does not fail. If a sub goal fails, then
95 * that is caught, and the next sub goal in sequence is
96 * instantiated and performed.
98 public States action(String head,Data d)
99 throws LoopEndException, ParallelEndException {
100 Goal [] subgoals = getGoalSubgoals();
101 if ( subgoals == null )
102 return States.FAILED;
103 while ( index < subgoals.length ) {
104 if ( ongoing == null ) {
105 ongoing = subgoals[ index ].instantiate(
106 head + "." + index, d );
108 States s = ongoing.perform( d );
109 if ( s != States.FAILED )
114 return States.FAILED;