capture
[rrq/gorite.git] / com / intendico / gorite / SequenceGoal.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 SequenceGoal is achieved by means of achieving its sub goals in
31  * order. The goal fails when and if any sub goal fails.
32  */
33 public class SequenceGoal extends Goal {
34
35     /**
36      * Constructor.
37      */
38     public SequenceGoal(String n,Goal [] sg) {
39         super( n, sg );
40     }
41
42     /**
43      * Convenience constructor without sub goals.
44      */
45     public SequenceGoal(String n) {
46         this( n, null );
47     }
48
49     /**
50      * Creates and returns an instance object for achieving
51      * a SequenceGoal.
52      */
53     public Instance instantiate(String head,Data d) {
54         return new SequenceInstance( head );
55     }
56
57     /**
58      * Implements sequential goal execution.
59      */
60     public class SequenceInstance extends Instance {
61
62         /**
63          * Constructor.
64          */
65         public SequenceInstance(String h) {
66             super( h );
67         }
68
69         /**
70          * Keeps the currently ongoing subgoal instance
71          */
72         public Instance ongoing = null;
73
74         /**
75          * Index of the currently ongoing sub goal.
76          */
77         public int index = 0;
78
79         /**
80          * Cancels execution.
81          */
82         public void cancel() {
83             super.cancel();
84             if ( ongoing != null )
85                 ongoing.cancel();
86         }
87
88         /**
89          * Instantiates and performs sub goals in sequence.
90          */
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 );
100                 }
101                 States s = ongoing.perform( d );
102                 if ( s != States.PASSED )
103                     return s;
104                 index += 1;
105                 ongoing = null;
106             }
107             return States.PASSED;
108         }
109     }
110
111 }