capture
[rrq/gorite.git] / com / intendico / gorite / Plan.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.data.Equals;
25
26 /**
27  * The Plan class is a {@link Goal} that implements {@link Context}
28  * and {@link Precedence}.
29  *
30  * <p> The typical use of Plan object is as the top level {@link Goal}
31  * objects that get added to a {@link Capability}.
32  *
33  * @see BDIGoal
34  * @see AnyGoal
35  * @see AllGoal
36  * @see DataGoal
37  * @see TeamGoal
38  */
39 public class Plan extends SequenceGoal implements Context, Precedence {
40
41     /**
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
44      * developer.
45      */
46     private String plan_name;
47
48     /**
49      * Utility method that creates a context Query for defining a data
50      * element value.
51      */
52     public static Query define(String name,Object value) {
53         Ref r = new Ref( name );
54         r.set( value );
55         return new Equals( new Object [] { r } );
56     }
57
58     /**
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
62      * {@link #plan_name}.
63      */
64     public Plan(String pn,String n,Goal [] sg) {
65         super( n, sg );
66         plan_name = pn;
67     }
68
69     /**
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}.
73      */
74     public Plan(String pn,String n) {
75         this( pn, n, null );
76     }
77
78     /**
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.
82      */
83     public Plan(String n,Goal [] sg) {
84         super( n, sg );
85     }
86
87     /**
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.
93      */
94     public Plan(String n) {
95         this( null, n, null );
96     }
97
98     /**
99      * Construction for a goal name given as Class. Actual goal name
100      * is the name of the Class.
101      */
102     public Plan(Class goal) {
103         this( null, goal.getName(), null );
104     }
105
106     /**
107      * Construction for a goal name given as Class, with plan
108      * name. Actual goal name is the name of the Class.
109      */
110     public Plan(String name,Class goal) {
111         this( name, goal.getName(), null );
112     }
113
114     /**
115      * Construction for a goal name given as Class, with sub
116      * goals. Actual goal name is the name of the Class.
117      */
118     public Plan(Class goal,Goal [] sg) {
119         this( null, goal.getName(), sg );
120     }
121
122     /**
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.
125      */
126     public Plan(String name,Class goal,Goal [] sg) {
127         this( name, goal.getName(), sg );
128     }
129
130     /**
131      * The default precedence level for goals.
132      */
133     public final static int DEFAULT_PRECEDENCE = 5;
134
135     /**
136      * Default context method.
137      */
138     public Query context(Data d) throws Exception {
139         return null;
140     }
141
142     /**
143      * Default precedence method. Returns {@link
144      * #DEFAULT_PRECEDENCE}.
145      */
146     public int precedence(Data d) {
147         return DEFAULT_PRECEDENCE;
148     }
149
150     /**
151      * Returns a {@link String} representation of this object.
152      */
153     public String toString() {
154         String n = "Plan:";
155         if ( plan_name != null )
156             n += " " + plan_name;
157         return n + "\n" + super.toString();
158     }
159
160     /**
161      * Returns the {@link #plan_name} attribute.
162      */
163     public String getPlanName() {
164         return plan_name;
165     }
166
167 }