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;
24 import com.intendico.data.Equals;
27 * The Plan class is a {@link Goal} that implements {@link Context}
28 * and {@link Precedence}.
30 * <p> The typical use of Plan object is as the top level {@link Goal}
31 * objects that get added to a {@link Capability}.
39 public class Plan extends SequenceGoal implements Context, Precedence {
42 * Holds a plan name. This name is not used for anything within
43 * GORITE, but may serve as an identifier for a plan for a
46 private String plan_name;
49 * Utility method that creates a context Query for defining a data
52 public static Query define(String name,Object value) {
53 Ref r = new Ref( name );
55 return new Equals( new Object [] { r } );
59 * Constructor for a given plan name, goal name and sub goals. The
60 * given sub goals comprise the plan body. The goal name is that
61 * which the plan is intended to achieve. The plan name is held in
64 public Plan(String pn,String n,Goal [] sg) {
70 * Constructor for a given plan name, goal name and without sub
71 * goals. The goal name is that which the plan is intended to
72 * achieve. The plan name is held in {@link #plan_name}.
74 public Plan(String pn,String n) {
79 * Constructor for a given goal name and sub goals. The given sub
80 * goals comprise the plan body. The goal name is that which the
81 * plan is intended to achieve.
83 public Plan(String n,Goal [] sg) {
88 * Constructor for a given goal name without sub goals. A body may
89 * be assigned to this plan at initialisation or later, or
90 * alternatively, this plan would be a <i>task</i> by virtue of an
91 * overriding {@link Goal#execute} implementation. The goal name
92 * is that which the plan is intended to achieve.
94 public Plan(String n) {
95 this( null, n, null );
99 * Construction for a goal name given as Class. Actual goal name
100 * is the name of the Class.
102 public Plan(Class goal) {
103 this( null, goal.getName(), null );
107 * Construction for a goal name given as Class, with plan
108 * name. Actual goal name is the name of the Class.
110 public Plan(String name,Class goal) {
111 this( name, goal.getName(), null );
115 * Construction for a goal name given as Class, with sub
116 * goals. Actual goal name is the name of the Class.
118 public Plan(Class goal,Goal [] sg) {
119 this( null, goal.getName(), sg );
123 * Construction for a goal name given as Class, with plan name and
124 * sub goals. Actual goal name is the name of the Class.
126 public Plan(String name,Class goal,Goal [] sg) {
127 this( name, goal.getName(), sg );
131 * The default precedence level for goals.
133 public final static int DEFAULT_PRECEDENCE = 5;
136 * Default context method.
138 public Query context(Data d) throws Exception {
143 * Default precedence method. Returns {@link
144 * #DEFAULT_PRECEDENCE}.
146 public int precedence(Data d) {
147 return DEFAULT_PRECEDENCE;
151 * Returns a {@link String} representation of this object.
153 public String toString() {
155 if ( plan_name != null )
156 n += " " + plan_name;
157 return n + "\n" + super.toString();
161 * Returns the {@link #plan_name} attribute.
163 public String getPlanName() {