capture
[rrq/gorite.git] / com / intendico / gorite / RepeatGoal.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 RepeatGoal is achieved by means of achieving all its
31  * instantiations in parallel, where each instantiation is achieved by
32  * achieving the given sub goals in sequence. The goal definition
33  * includes a context specification, which enumerates the
34  * instantiations of the goal arising. Each instantiation refers to
35  * the same sequence of sub goals, but with different focus in the
36  * data.
37  *
38  * <p> The following code snippet illustrates the use of a RepeatGoal
39  * to process a multi-valued data element (named "options" in the
40  * example).
41  *
42  * <pre>
43  * new RepeatGoal( "process options", new Goal [] {
44  *     // This goal sequence has its "options" data focussed
45  *     // on one of the (previosuly multi-valued) options.
46  *     new BDIGoal( "do this with options" ),
47  *     new BDIGoal( "do that with options" ),
48  *     new BDIGoal( "do more with options" ),
49  * } ) {{ control = "options"; }} // Replicate for all the values of "options"
50  * </pre>
51  *
52  * @see LoopGoal
53  * @see BDIGoal
54  */
55 public class RepeatGoal extends BranchingGoal {
56
57     /**
58      * Constructor with a given control name, goal name and sub goals.
59      */
60     public RepeatGoal(String c,String n,Goal [] sg) {
61         super( n, sg );
62         setGoalControl( c );
63     }
64
65     /**
66      * Constructor with a given goal name and sub goals.
67      */
68     public RepeatGoal(String n,Goal [] sg) {
69         super( n, sg );
70     }
71
72     /**
73      * Convenience constructor without sub goals.
74      */
75     public RepeatGoal(String n) {
76         this( n, null );
77     }
78
79     /**
80      * Creates and returns an instance object for achieving
81      * a RepeatGoal.
82      */
83     public Instance instantiate(String head,Data d) {
84         return new RepeatInstance( head );
85     }
86
87     /**
88      * Implements re-instatiation of this goal relative something.
89      */
90     public class RepeatInstance extends MultiInstance {
91
92         /**
93          * Constructor.
94          */
95         public RepeatInstance(String h) {
96             super( h );
97         }
98
99         /**
100          * To decide whether yet another branch should be made.
101          */
102         public boolean more(int i,Data d) {
103             return i < d.size( getGoalControl() );
104         }
105
106         /**
107          * The branch is created be instantiating this goal as a
108          * sequence goal.
109          */
110         public Instance getBranch(int i,String head,Data d) {
111             Goal g = new SequenceGoal( getGoalName(), getGoalSubgoals() );
112             g.setGoalControl( getGoalControl() );
113             g.setGoalGroup( getGoalGroup() );
114             return g.instantiate( head, d );
115         }
116     }
117
118 }