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: /**
030: *
031: **/
032:
033: /**
034: * An KeyRuleParameter is a RuleParameter with a list of
035: * key/value pairs and a default value. When the
036: * getValue is called with the Key argument, and some value is defined
037: * for that key, that value is returned. Otherwise, the default
038: * is returned.
039: */
040: public class KeyRuleParameter implements RuleParameter,
041: java.io.Serializable {
042:
043: protected String my_name;
044: protected KeyRuleParameterEntry[] my_keys;
045: protected String my_value;
046:
047: /**
048: * Constructor sets min/max values and establishes value as not set
049: */
050: public KeyRuleParameter(String param_name,
051: KeyRuleParameterEntry[] keys) {
052: my_keys = keys;
053: my_value = null;
054: my_name = param_name;
055: }
056:
057: public KeyRuleParameter(String param_name) {
058: my_name = param_name;
059: my_value = null;
060: }
061:
062: public KeyRuleParameter() {
063: }
064:
065: /**
066: * Parameter type is KEY
067: */
068: public int ParameterType() {
069: return RuleParameter.KEY_PARAMETER;
070: }
071:
072: public String getName() {
073: return my_name;
074: }
075:
076: public void setName(String name) {
077: my_name = name;
078: }
079:
080: public KeyRuleParameterEntry[] getKeys() {
081: return my_keys;
082: }
083:
084: public void setKeys(KeyRuleParameterEntry[] keys) {
085: my_keys = keys;
086: my_value = null;
087: }
088:
089: /**
090: * Get parameter value (String)
091: * @return Object parameter value (String). Note : could be null.
092: */
093: public Object getValue() {
094: return my_value;
095: }
096:
097: /**
098: * Get parameter value (String) keyed by int
099: * If key equals one of the defined keys, return associated
100: * value, otherwise return default value (String).
101: * @return Object parameter value (String). Note : could be null.
102: */
103: public Object getValue(String key) {
104: String value = my_value;
105: for (int i = 0; i < my_keys.length; i++) {
106: if (my_keys[i].getKey().equals(key)) {
107: value = my_keys[i].getValue();
108: break;
109: }
110: }
111:
112: return value;
113: }
114:
115: /**
116: * Set parameter value
117: * @param new_value : must be String within given list
118: * @throws RuleParameterIllegalValueException
119: */
120: public void setValue(Object new_value)
121: throws RuleParameterIllegalValueException {
122: if (new_value instanceof String) {
123: my_value = (String) new_value;
124: } else {
125: throw new RuleParameterIllegalValueException(
126: RuleParameter.KEY_PARAMETER,
127: "String must be specified");
128: }
129: }
130:
131: /**
132: * @param test_value : must be String
133: * @return true if Object is a String in the enumeration, false otherwise
134: */
135: public boolean inRange(Object test_value) {
136: return (test_value instanceof String);
137: }
138:
139: public Object clone() {
140: KeyRuleParameter krp = new KeyRuleParameter(my_name);
141: if (my_keys != null) {
142: krp.setKeys((KeyRuleParameterEntry[]) my_keys.clone());
143: }
144: try {
145: krp.setValue(my_value);
146: } catch (RuleParameterIllegalValueException rpive) {
147: }
148: return krp;
149: }
150:
151: public String toString() {
152: return "#<KEY_PARAMETER : " + my_value + " [" + Key_List()
153: + "] >";
154: }
155:
156: protected String Key_List() {
157: String list = "";
158: for (int i = 0; i < my_keys.length; i++) {
159: list += my_keys[i];
160: if (i != my_keys.length - 1)
161: list += "/";
162: }
163: return list;
164: }
165:
166: public static void main(String[] args) {
167: KeyRuleParameterEntry p1 = new KeyRuleParameterEntry("LOW",
168: "AAA");
169: KeyRuleParameterEntry p2 = new KeyRuleParameterEntry("MED",
170: "BBB");
171: KeyRuleParameterEntry p3 = new KeyRuleParameterEntry("HIGH",
172: "CCC");
173:
174: KeyRuleParameterEntry[] keys = { p1, p2, p3 };
175: KeyRuleParameter krp = new KeyRuleParameter("testKeyParam",
176: keys);
177:
178: if (krp.getValue() != null) {
179: System.out
180: .println("Error : Parameter not initialized to null");
181: }
182:
183: try {
184: krp.setValue("DFLT");
185: } catch (RuleParameterIllegalValueException rpive) {
186: System.out.println("Error detecting illegal set condition");
187: }
188:
189: String[] tests = { "NONE", "LOW", "HIGH", "MED", "DUMMY" };
190: for (int i = 0; i < tests.length; i++) {
191: System.out.println("Value for " + tests[i] + " = "
192: + krp.getValue(tests[i]));
193: }
194:
195: System.out.println("KRP = " + krp);
196: System.out.println("KeyRuleParameter test complete.");
197:
198: }
199:
200: }
|