capture
[rrq/gorite.git] / com / intendico / gorite / FailGoal.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 import com.intendico.gorite.Goal.States;
25
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Vector;
29
30 /**
31  * A FailGoal is achieved by means of failing any sub goal, which are
32  * attempted in sequence. The goal fails when and if all sub goals
33  * have been attempted and succeeded. In particular, a fail goal
34  * without sub goals always fail immediately, and is a way to induce
35  * failure. Failure may also be induced by tasks, which can succeed or
36  * fail.
37  *
38  * @see ConditionGoal
39  */
40 public class FailGoal extends SequenceGoal {
41
42     /**
43      * Constructor.
44      */
45     public FailGoal(String n,Goal [] sg) {
46         super( n, sg );
47     }
48
49     /**
50      * Convenience constructor without sub goals.
51      */
52     public FailGoal(String n) {
53         this( n, null );
54     }
55
56     /**
57      * Creates and returns an instance object for achieving
58      * a FailGoal.
59      */
60     public Instance instantiate(String head,Data d) {
61         return new FailInstance( head );
62     }
63
64     /**
65      * Represents the process of achieving a FailGoal
66      */
67     public class FailInstance extends SequenceInstance {
68
69         /**
70          * Constructor.
71          */
72         public FailInstance(String h) {
73             super( h );
74         }
75
76         /**
77          * Process all sub goals in sequence, then reverse {@link
78          * States#PASSED} and {@link States#FAILED}.
79          */
80         public States action(String head,Data d)
81             throws LoopEndException, ParallelEndException {
82             States s = super.action( head, d );
83             if ( s == States.PASSED )
84                 return States.FAILED;
85             if ( s == States.FAILED )
86                 return States.PASSED;
87             return s;
88         }
89     }
90 }