capture
[rrq/gorite.git] / com / intendico / gorite / TeamGoal.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 TeamGoal is a {@link Goal} that is achieved as a {@link BDIGoal}
31  * by a given {@link Team.Role}. The TeamGoal looks up {@link
32  * Team.Role} fillers in the {@link Data} under the given role name,
33  * and presents itself as a {@link BDIGoal} to be performed by the
34  * fillers.
35  *
36  * <p> Note that the TeamGoal distributes like a {@link RepeatGoal}
37  * over the role name data, requesting all fillers to achieve the goal
38  * in parallel. The TeamGoal succeeds when all the fillers succeed,
39  * and fails if any role filler fails. In the latter case, the
40  * branches for the fillers in progress are cancelled.
41  *
42  * <p> If the TeamGoal is implemented by a {@link Team.Role} plan that
43  *
44  * @see Team
45  * @see Team.TaskTeam
46  * @see Performer#fillRole
47  * @see ControlGoal
48  */
49 public class TeamGoal extends BranchingGoal {
50
51     /**
52      * Constructor for TeamGoal.
53      */
54     public TeamGoal(String role,String n) {
55         super( n, null );
56         setGoalControl( role );
57     }
58
59     /**
60      * Cache of construction object (when not a String).
61      */
62     public Object specific;
63
64     /**
65      * Constructor using an object other than String. Then the class
66      * name of the object is used as goal name, and the object is held
67      * as {@link #specific}. However, if the given object is a {@link
68      * String}, then its value (rather than its type) is used as goal
69      * name anyhow.
70      * @see BDIGoal#BDIGoal(Object)
71      */
72     public TeamGoal(String role,Object n) {
73         super( ( n instanceof String )? (String) n : n.getClass().getName() );
74         specific = n;
75         setGoalControl( role );
76     }
77
78     /**
79      * Make an Instance for executing this goal.
80      */
81     public Instance instantiate(String head,Data d) {
82         return new TeamInstance( head );
83     }
84
85     /**
86      * Creates and returns an instance object for achieving
87      * a TeamGoal.
88      */
89     public class TeamInstance extends MultiInstance {
90
91         /**
92          * Constructor.
93          */
94         public TeamInstance(String h) {
95             super( h );
96         }
97
98         /**
99          * To decide whether yet another branch should be made.
100          */
101         public boolean more(int i,Data d) {
102             return i < Math.max( 1, d.size( getGoalControl() ) );
103         }
104
105         /**
106          * The branch is created be instantiating a {@link BDIGoal}.
107          */
108         public Instance getBranch(int i,String head,Data d) {
109             Goal g = specific != null?
110                 new BDIGoal( specific ) : new BDIGoal( getGoalName() );
111             g.setGoalGroup( getGoalGroup() );
112             g.setGoalControl( getGoalControl() );
113             return g.instantiate( head, d );
114         }
115
116     }
117
118 }