capture
[rrq/gorite.git] / com / intendico / gorite / ConditionGoal.java
1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
3
4 This file is part of GORITE.
5
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.
10
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.
15
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 **********************************************************************/
19
20 package com.intendico.gorite;
21
22 import com.intendico.data.Query;
23 import com.intendico.data.Ref;
24
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Vector;
28
29 /**
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.
33  */
34 public class ConditionGoal extends Goal {
35
36     /**
37      * Constructor.
38      */
39     public ConditionGoal(String n,Goal [] sg) {
40         super( n, sg );
41     }
42
43     /**
44      * Convenience constructor without sub goals.
45      */
46     public ConditionGoal(String n) {
47         this( n, null );
48     }
49
50     /**
51      * Creates and returns an instance object for achieving
52      * a ConditionGoal.
53      */
54     public Instance instantiate(String head,Data d) {
55         return new ConditionInstance( head );
56     }
57
58     /**
59      * Implements sequential, conditional sub goal execution
60      * attempt. I.e., if a sub goal fails, then try next, otherwise
61      * succeed.
62      */
63     public class ConditionInstance extends Instance {
64
65         /**
66          * Constructor.
67          */
68         public ConditionInstance(String h) {
69             super( h );
70         }
71
72         /**
73          * Next sub goal index.
74          */
75         public int index = 0;
76
77         /**
78          * Current ongoing Instance.
79          */
80         public Instance ongoing = null;
81
82         /**
83          * Cancels this execution by propagating the cancel call to
84          * all sub goals.
85          */
86         public void cancel() {
87             super.cancel();
88             if ( ongoing != null )
89                 ongoing.cancel();
90         }
91
92         /**
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.
97          */
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 );
107                 }
108                 States s = ongoing.perform( d );
109                 if ( s != States.FAILED )
110                     return s;
111                 index += 1;
112                 ongoing = null;
113             }
114             return States.FAILED;
115         }
116     }
117 }