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 DoubleRuleParameter is a RuleParameter with specified/protected
035: * double bounds that returns a Double
036: */
037: public class DoubleRuleParameter implements RuleParameter,
038: java.io.Serializable {
039: protected String my_name;
040: protected double my_min;
041: protected double my_max;
042: protected Double my_value;
043:
044: /**
045: * Constructor sets min/max values and establishes value as not set
046: */
047: public DoubleRuleParameter(String param_name, double min, double max) {
048: my_min = min;
049: my_max = max;
050: my_value = null;
051: my_name = param_name;
052: }
053:
054: public DoubleRuleParameter(String param_name) {
055: my_name = param_name;
056: }
057:
058: public DoubleRuleParameter() {
059: }
060:
061: /**
062: * Parameter type is DOUBLE
063: */
064: public int ParameterType() {
065: return RuleParameter.DOUBLE_PARAMETER;
066: }
067:
068: public String getName() {
069: return my_name;
070: }
071:
072: public void setName(String name) {
073: my_name = name;
074: }
075:
076: public double getMin() {
077: return my_min;
078: }
079:
080: public void setMin(double min) {
081: my_min = min;
082: }
083:
084: public double getMax() {
085: return my_max;
086: }
087:
088: public void setMax(double max) {
089: my_max = max;
090: }
091:
092: public void setBounds(double min, double max) {
093: if (min > max) {
094: throw new java.lang.IllegalArgumentException("min - "
095: + min + " - must be greater than max - " + max);
096: }
097: my_min = min;
098: my_max = max;
099: }
100:
101: public double getLowerBound() {
102: return getMin();
103: }
104:
105: public double getUpperBound() {
106: return getMax();
107: }
108:
109: /**
110: * Get parameter value (Double)
111: * @return Object parameter value (Double). Note : could be null.
112: */
113: public Object getValue() {
114: return my_value;
115: }
116:
117: /**
118: * Set parameter value
119: * @param new_value : must be Double
120: * @throws RuleParameterIllegalValueException
121: */
122: public void setValue(Object new_value)
123: throws RuleParameterIllegalValueException {
124: boolean success = false;
125: if (new_value instanceof Double) {
126: Double new_double = (Double) new_value;
127: if ((new_double.intValue() >= my_min)
128: && (new_double.intValue() <= my_max)) {
129: my_value = new_double;
130: success = true;
131: }
132: }
133: if (!success)
134: throw new RuleParameterIllegalValueException(
135: RuleParameter.DOUBLE_PARAMETER,
136: "Double must be between " + my_min + " and "
137: + my_max);
138: }
139:
140: /**
141: *
142: * @param test_value : must be Double
143: * @return true if test_value is within the acceptable range
144: */
145: public boolean inRange(Object test_value) {
146: if (test_value instanceof Double) {
147: Double new_double = (Double) test_value;
148: if ((new_double.doubleValue() >= my_min)
149: && (new_double.doubleValue() <= my_max))
150: return true;
151: }
152: return false;
153:
154: }
155:
156: public static void Test() {
157: DoubleRuleParameter drp = new DoubleRuleParameter(
158: "testDoubleParam", 3.14, 10.73);
159:
160: if (drp.getValue() != null) {
161: System.out
162: .println("Error : Parameter not initialized to null");
163: }
164:
165: try {
166: drp.setValue(new Double(11.11));
167: System.out.println("Error detecting illegal set condition");
168: } catch (RuleParameterIllegalValueException rpive) {
169: }
170:
171: try {
172: drp.setValue(new Double(1.2));
173: System.out.println("Error detecting illegal set condition");
174: } catch (RuleParameterIllegalValueException rpive) {
175: }
176:
177: Double d4 = new Double(4.5);
178: try {
179: drp.setValue(d4);
180: } catch (RuleParameterIllegalValueException rpive) {
181: System.out.println("Error detecting legal set condition");
182: }
183:
184: if (drp.getValue() != d4) {
185: System.out.println("Error retrieving value of parameter");
186: }
187:
188: System.out.println("DRP = " + drp);
189: System.out.println("DoubleRuleParameter test complete.");
190:
191: }
192:
193: public String toString() {
194: return "#<DOUBLE_PARAMETER : " + my_value + " [" + my_min
195: + " , " + my_max + "] >";
196: }
197:
198: public Object clone() {
199: DoubleRuleParameter dp = new DoubleRuleParameter(my_name,
200: my_min, my_max);
201: try {
202: dp.setValue(my_value);
203: } catch (RuleParameterIllegalValueException rpive) {
204: }
205: return dp;
206: }
207:
208: }
|