capture
[rrq/gorite.git] / com / intendico / gorite / addon / TodoGroupRoundRobin.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.addon;
21
22 import com.intendico.gorite.*;
23 import com.intendico.gorite.Performer.TodoGroup;
24 import java.util.Vector;
25
26 /**
27  * This plan is a {@link TodoGroup} meta goal handler to cycle through
28  * the stacked intentions, by putting the top one last, except when it
29  * is newly added. The actual goal name for the meta goal is given at
30  * construction time, and unless it is the default {@link
31  * Performer#TODOGROUP_META_GOAL}, the actual {@link TodoGroup}
32  * instances to be managed by this goal need to be set up explicitly
33  * using {@link Performer#addTodoGroup}.
34  *
35  * <p> Usage example:
36  * <pre>
37  * new Performer( "example" ) {
38  *     addGoal( new TodoGroupRoundRobin( "round robin todogroup meta goal" ) );
39  *     addTodoGroup( "example", "round robin todogroup meta goal" );
40  * }
41  * </pre>
42  *
43  * <p> Another usage example, to make it the default todogroup meta goal:
44  * <pre>
45  * new Performer( "example" ) {
46  *     addGoal( new TodoGroupRoundRobin( TODOGROUP_META_GOAL ) );
47  * }
48  * </pre>
49  */
50 public class TodoGroupRoundRobin extends Goal {
51
52     /**
53      * Constructor for providing {@link TodoGroup} management through
54      * the named meta goal.
55      */
56     public TodoGroupRoundRobin(String name) {
57         super( name );
58     }
59
60     /**
61      * Overrides {@link Goal#execute} to provide the meta goal
62      * implementation. This expects a {@link Data} object with
63      * elements named by {@link Performer#TODOGROUP}, "coming" and
64      * "top". The implementation cycles the todogroup unless the "top"
65      * intention is in the "coming" vector.
66      */
67     public States execute(Data d) {
68         TodoGroup todogroup = (TodoGroup) d.getValue( Performer.TODOGROUP );
69         Vector coming = (Vector) d.getValue( "coming" );
70         Goal.Instance top = (Goal.Instance) d.getValue( "top" );
71         if ( ! coming.contains( top ) )
72             todogroup.rotate();
73         return States.PASSED;
74     }
75 }