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 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
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.
42 * <p> If the TeamGoal is implemented by a {@link Team.Role} plan that
46 * @see Performer#fillRole
49 public class TeamGoal extends BranchingGoal {
52 * Constructor for TeamGoal.
54 public TeamGoal(String role,String n) {
56 setGoalControl( role );
60 * Cache of construction object (when not a String).
62 public Object specific;
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
70 * @see BDIGoal#BDIGoal(Object)
72 public TeamGoal(String role,Object n) {
73 super( ( n instanceof String )? (String) n : n.getClass().getName() );
75 setGoalControl( role );
79 * Make an Instance for executing this goal.
81 public Instance instantiate(String head,Data d) {
82 return new TeamInstance( head );
86 * Creates and returns an instance object for achieving
89 public class TeamInstance extends MultiInstance {
94 public TeamInstance(String h) {
99 * To decide whether yet another branch should be made.
101 public boolean more(int i,Data d) {
102 return i < Math.max( 1, d.size( getGoalControl() ) );
106 * The branch is created be instantiating a {@link BDIGoal}.
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 );