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 ControlGoal is achieved by performing all its sub goals in
31 * sequence, which results in a control effect on an enclosing
32 * parallel execution by terminating all other branches when it
33 * succeeds. Thus, the execution either fails, or continues after the
34 * enclosing parallel goal.
36 * <p> The following code snippet illustrates an optional interrupt,
37 * i.e., how to either interrupt or succeed (rather than either
40 * new FailGoal( "not interrupting is fine", new Goal [] {
41 * new ControlGoal( "maybe interrupt", new Goal {
42 * // Goal sequence for causing interrupt if achieved successfully
47 * <p> Note that the ControlGoal effect propagates up to the nearest
48 * parallel type goal through {@link BDIGoal} execution, and it
49 * therefore is not necessary for the ControlGoal to be explicitly
50 * within the sub goal hiearachy of the parallel type goal of
53 * <p> In particular, a {@link Plan} of a {@link Team.Role} may
54 * include a ControlGoal to express that a {@link TeamGoal} is
55 * achieved by any one filler rather than requiring all of them.
63 public class ControlGoal extends SequenceGoal {
68 public ControlGoal(String n,Goal [] sg) {
73 * Convenience constructor without sub goals.
75 public ControlGoal(String n) {
80 * Creates and returns an instance object for achieving
83 public Instance instantiate(String head,Data d) {
84 return new ControlInstance( head );
88 * Implements a control action for parallel execution.
90 public class ControlInstance extends SequenceInstance {
95 public ControlInstance(String h) {
100 * Process all subgoals in sequence, then throw a
101 * LoopEndException if success. If a subgoals fail, then fail
102 * without throwing exception.
104 public States action(String head,Data d)
105 throws LoopEndException, ParallelEndException {
106 States s = super.action( head, d );
107 if ( s == States.PASSED ) {
108 throw new ParallelEndException( head );