capture
[rrq/gorite.git] / com / intendico / gorite / addon / TodoGroupSkipBlocked.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 maintain
28  * focus to "the first" running intention, i.e., to avoid that the
29  * whole group is blocked when the top intention gets
30  * blocked. Instead, when the top intention gets blocked, the
31  * TodoGroup stack is scanned for its top-most non-blocked intention,
32  * which then is promoted.  The actual goal name for the meta goal is
33  * given at construction time, and unless it is the default {@link
34  * Performer#TODOGROUP_META_GOAL}, the actual {@link TodoGroup}
35  * instances to be managed by this goal need to be set up explicitly
36  * using {@link Performer#addTodoGroup}.
37  *
38  * <p> Usage example:
39  * <pre>
40  * new Performer( "example" ) {
41  *     addGoal( new TodoGroupSkipBlocked( "skip blocked todogroup meta goal" ) );
42  *     addTodoGroup( "example", "skip blocked todogroup meta goal" );
43  * }
44  * </pre>
45  *
46  * <p> Another usage example, to make it the default todogroup meta
47  * goal:
48  * <pre>
49  * new Performer( "example" ) {
50  *     addGoal( new TodoGroupSkipBlocked( TODOGROUP_META_GOAL ) );
51  * }
52  * </pre>
53  */
54 public class TodoGroupSkipBlocked extends Goal {
55
56     /**
57      * Constructor for providing {@link TodoGroup} management through
58      * the named meta goal.
59      */
60     public TodoGroupSkipBlocked(String name) {
61         super( name );
62     }
63
64     /**
65      * Overrides {@link Goal#execute} to provide the meta goal
66      * implementation. This expects a {@link Data} object with the
67      * {@link TodoGroup} concerned as the element named by {@link
68      * Performer#TODOGROUP}.  The implementation promotes the first
69      * non-blocked intention, if any. Returns PASSED if there is a
70      * non-blocked intention, an FAILED otherwise.
71      */
72     public States execute(Data d) {
73         TodoGroup tg = (TodoGroup) d.getValue( Performer.TODOGROUP );
74         Vector/*<Goal.Instance>*/ s = tg.stack;
75         for ( int i = 0; i < s.size(); i++ ) {
76             if ( ((Instance) s.get( i )).state == States.STOPPED ) {
77                 tg.promote( i );
78                 return States.PASSED;
79             }
80         }
81         return States.FAILED;
82     }
83
84 }