001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.planning.ldm.policy;
028:
029: import java.beans.PropertyChangeEvent;
030: import java.beans.PropertyChangeListener;
031: import java.beans.PropertyChangeSupport;
032: import java.io.IOException;
033: import java.io.NotActiveException;
034: import java.io.ObjectInputStream;
035: import java.util.Enumeration;
036: import java.util.Hashtable;
037:
038: import org.cougaar.core.util.OwnedUniqueObject;
039: import org.cougaar.core.util.UniqueObject;
040: import org.cougaar.planning.ldm.plan.Context;
041: import org.cougaar.planning.ldm.plan.Transferable;
042:
043: /** Policy implementation
044: *
045: **/
046:
047: /**
048: * Policy is a class that contains RuleParameters
049: **/
050: public class Policy extends OwnedUniqueObject implements
051: java.io.Serializable, Transferable, UniqueObject {
052: protected Hashtable my_parameters;
053: protected String name;
054:
055: /**
056: * Default constructor. Creates a PolicyImpl with an empty Hashtable and
057: * no name.
058: **/
059: public Policy() {
060: my_parameters = new Hashtable(11);
061: }
062:
063: /**
064: * Creates a new policy with the name policyName
065: * @param policyName the name of the policy
066: **/
067: public Policy(String policyName) {
068: this ();
069: name = policyName;
070: }
071:
072: /**
073: * Adds a new RuleParameter to the set stored in the Policy. This
074: * should be the sole means for putting parameters into the policy
075: * and can be overridden if desired to take special action such as
076: * caching certain parameters for quicker access.
077: * @param rule_parameter the new param to be added to the Policy
078: **/
079: public void Add(RuleParameter rule_parameter) {
080: my_parameters.put(rule_parameter.getName(), rule_parameter);
081: }
082:
083: public void Remove(String name) {
084: my_parameters.remove(name);
085: }
086:
087: /**
088: * Lookup and return parameter by name
089: * Return null if no such parameter found.
090: */
091: public RuleParameter Lookup(String name) {
092: return (RuleParameter) my_parameters.get(name);
093: }
094:
095: /**
096: * Returns the policy name
097: * @return the name of the Policy
098: **/
099: public String getName() {
100: return name;
101: }
102:
103: /**
104: * Sets the name of the Policy
105: * @param policyName the name of the policy
106: **/
107: public void setName(String policyName) {
108: name = policyName;
109: }
110:
111: /**
112: * Replace a parameter with replacement_param
113: * @param replacement_param a RuleParameter to replace a parameter
114: * in the Policy. replacement_param must have the same getName() value
115: * as an existing parameter in the Policy.
116: * @return false if there is no matching param in the Policy, true
117: * otherwise
118: **/
119: public boolean Replace(RuleParameter replacement_param) {
120: if (my_parameters.get(replacement_param.getName()) != null) {
121: Add(replacement_param);
122: return true;
123: }
124: return false;
125: }
126:
127: /** Replaces existing set of RuleParameters with new set
128: * @param params the new rule parameters
129: **/
130: public void setRuleParameters(RuleParameter[] params) {
131: my_parameters = new Hashtable(11);
132: for (int i = 0; i < params.length; i++) {
133: Add(params[i]);
134: }
135: }
136:
137: /**
138: * @return this policy's rule parameters
139: **/
140: public RuleParameter[] getRuleParameters() {
141: java.util.Enumeration e = my_parameters.elements();
142: int i = 0;
143: RuleParameter[] rps = new RuleParameter[my_parameters.size()];
144: while (e.hasMoreElements())
145: rps[i++] = (RuleParameter) e.nextElement();
146: return rps;
147: }
148:
149: public Object clone() {
150:
151: Policy np = null;
152: try {
153: np = (Policy) getClass().newInstance();
154: } catch (InstantiationException ie) {
155: ie.printStackTrace();
156: } catch (IllegalAccessException iae) {
157: iae.printStackTrace();
158: }
159: if (np == null)
160: return null;
161:
162: np.setName(this .getName());
163: Enumeration e = my_parameters.elements();
164: while (e.hasMoreElements()) {
165: RuleParameter rp = (RuleParameter) e.nextElement();
166: np.Add((RuleParameter) rp.clone());
167: }
168:
169: // same or different UID?
170: np.setUID(this .getUID());
171: np.setOwner(this .getOwner());
172:
173: // clone context?
174: np.setContext(this .getContext());
175: return np;
176: }
177:
178: public boolean same(Transferable other) {
179: // name is good enough
180: if (other instanceof Policy)
181: if (this .getName().equals(((Policy) other).getName()))
182: return true;
183: return false;
184: }
185:
186: public void setAll(Transferable other) {
187: if (!(other instanceof Policy))
188: throw new IllegalArgumentException("Parameter not Policy");
189:
190: Policy oPolicy = (Policy) other;
191: setUID(oPolicy.getUID());
192: setOwner(oPolicy.getOwner());
193: setRuleParameters(oPolicy.getRuleParameters());
194: pcs.firePropertyChange(new PropertyChangeEvent(this ,
195: "RuleParameters", null, my_parameters));
196: }
197:
198: private Context myContext = null;
199:
200: /**
201: * Set the problem Context of this policy.
202: * @see org.cougaar.planning.ldm.plan.Context
203: **/
204: void setContext(Context context) {
205: myContext = context;
206: }
207:
208: /**
209: * Get the problem Context (if any) for this policy.
210: * @see org.cougaar.planning.ldm.plan.Context
211: **/
212: Context getContext() {
213: return myContext;
214: }
215:
216: /**
217: * Save the Policy object in some format in some file
218: **/
219: public void save() {
220:
221: // stub
222: }
223:
224: /**
225: * Restore a Policy object
226: **/
227: public void load() {
228:
229: // stub
230: }
231:
232: //dummy PropertyChangeSupport for the Jess Interpreter.
233: protected transient PropertyChangeSupport pcs = new PropertyChangeSupport(
234: this );
235:
236: public void addPropertyChangeListener(PropertyChangeListener pcl) {
237: pcs.addPropertyChangeListener(pcl);
238: }
239:
240: public void removePropertyChangeListener(PropertyChangeListener pcl) {
241: pcs.removePropertyChangeListener(pcl);
242: }
243:
244: private void readObject(ObjectInputStream is) throws IOException,
245: ClassNotFoundException, NotActiveException {
246: is.defaultReadObject();
247: pcs = new PropertyChangeSupport(this);
248: }
249: }
|