001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 SRA International
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:
028: package org.cougaar.logistics.ldm.policy;
029:
030: import org.cougaar.planning.ldm.policy.KeyRuleParameter;
031: import org.cougaar.planning.ldm.policy.KeyRuleParameterEntry;
032: import org.cougaar.planning.ldm.policy.Policy;
033: import org.cougaar.planning.ldm.policy.RangeRuleParameter;
034: import org.cougaar.planning.ldm.policy.RangeRuleParameterEntry;
035:
036: public class FeedingPolicy extends Policy {
037: public static final String MEAL_POLICY = "MealPolicy";
038: public static final String A_RATION_POLICY = "A-RationPolicy";
039: public static final String ENHANCEMENTS_POLICY = "EnhancementsPolicy";
040: public static final String BOTTLEDWATER_POLICY = "BottledWaterPolicy";
041: private final static RangeRuleParameterEntry[] EMPTY_RANGE_ENTRY = new RangeRuleParameterEntry[0];
042: private final static KeyRuleParameterEntry[] EMPTY_KEY_ENTRY = new KeyRuleParameterEntry[0];
043:
044: public FeedingPolicy() {
045: }
046:
047: public RangeRuleParameterEntry[] getARationPolicyRanges() {
048: RangeRuleParameterEntry[] entry = EMPTY_RANGE_ENTRY;
049: RangeRuleParameter param = (RangeRuleParameter) Lookup(A_RATION_POLICY);
050: if (param != null) {
051: entry = param.getRanges();
052: }
053: return entry;
054: }
055:
056: public RangeRuleParameterEntry[] getEnhancementsPolicyRanges() {
057: RangeRuleParameterEntry[] entry = EMPTY_RANGE_ENTRY;
058: RangeRuleParameter param = (RangeRuleParameter) Lookup(ENHANCEMENTS_POLICY);
059: if (param != null) {
060: entry = param.getRanges();
061: }
062: return entry;
063: }
064:
065: public RangeRuleParameterEntry[] getMealPolicyRanges() {
066: RangeRuleParameterEntry[] entry = EMPTY_RANGE_ENTRY;
067: RangeRuleParameter param = (RangeRuleParameter) Lookup(MEAL_POLICY);
068: if (param != null) {
069: entry = param.getRanges();
070: }
071: return entry;
072: }
073:
074: public RangeRuleParameterEntry[] getWaterPolicyRanges() {
075: RangeRuleParameterEntry[] entry = EMPTY_RANGE_ENTRY;
076: RangeRuleParameter param = (RangeRuleParameter) Lookup(BOTTLEDWATER_POLICY);
077: if (param != null) {
078: entry = param.getRanges();
079: }
080: return entry;
081: }
082:
083: public void setARationPolicyRanges(RangeRuleParameterEntry[] ranges) {
084: RangeRuleParameter param = (RangeRuleParameter) Lookup(A_RATION_POLICY);
085: param.setRanges(ranges);
086: }
087:
088: public void setEnhancementsPolicyRanges(
089: RangeRuleParameterEntry[] ranges) {
090: RangeRuleParameter param = (RangeRuleParameter) Lookup(ENHANCEMENTS_POLICY);
091: param.setRanges(ranges);
092: }
093:
094: public void setMealPolicyRanges(RangeRuleParameterEntry[] ranges) {
095: RangeRuleParameter param = (RangeRuleParameter) Lookup(MEAL_POLICY);
096: param.setRanges(ranges);
097: }
098:
099: public void setWaterPolicyRanges(RangeRuleParameterEntry[] ranges) {
100: RangeRuleParameter param = (RangeRuleParameter) Lookup(BOTTLEDWATER_POLICY);
101: param.setRanges(ranges);
102: }
103:
104: public static final String RULES = "Rules";
105:
106: public KeyRuleParameterEntry[] getActionKeys(
107: RangeRuleParameter theRule) {
108: if (theRule != null) {
109: RangeRuleParameterEntry[] rule = theRule.getRanges();
110: if (rule != null) {
111: KeyRuleParameter theAction = (KeyRuleParameter) rule[1]
112: .getValue();
113: if (theAction != null)
114: return theAction.getKeys();
115: }
116: }
117: return EMPTY_KEY_ENTRY;
118: }
119:
120: public KeyRuleParameterEntry[] getConditionKeys(
121: RangeRuleParameter theRule) {
122: if (theRule != null) {
123: RangeRuleParameterEntry[] rule = theRule.getRanges();
124: if (rule != null) {
125: KeyRuleParameter theCondition = (KeyRuleParameter) rule[0]
126: .getValue();
127: if (theCondition != null)
128: return theCondition.getKeys();
129: }
130: }
131: return EMPTY_KEY_ENTRY;
132: }
133:
134: public KeyRuleParameterEntry[] getEnhancementsKeys(int range) {
135: RangeRuleParameterEntry[] enhancements = getEnhancementsPolicyRanges();
136: if (enhancements != null) {
137: KeyRuleParameter krp = (KeyRuleParameter) enhancements[range]
138: .getValue();
139: if (krp != null)
140: return krp.getKeys();
141: }
142: return EMPTY_KEY_ENTRY;
143: }
144:
145: public KeyRuleParameterEntry[] getMealKeys(int range) {
146: RangeRuleParameterEntry[] meals = getMealPolicyRanges();
147: if (meals != null) {
148: KeyRuleParameter krp = (KeyRuleParameter) meals[range]
149: .getValue();
150: if (krp != null)
151: return krp.getKeys();
152: }
153: return EMPTY_KEY_ENTRY;
154: }
155:
156: public KeyRuleParameterEntry[] getRangeKeys(
157: RangeRuleParameterEntry range) {
158: if (range != null) {
159: KeyRuleParameter krp = (KeyRuleParameter) range.getValue();
160: if (krp != null)
161: return krp.getKeys();
162: }
163: return EMPTY_KEY_ENTRY;
164: }
165:
166: public RangeRuleParameterEntry[] getRules() {
167: RangeRuleParameterEntry[] entry = EMPTY_RANGE_ENTRY;
168: RangeRuleParameter param = (RangeRuleParameter) Lookup(RULES);
169: if (param != null) {
170: entry = param.getRanges();
171: }
172: return entry;
173: }
174:
175: public KeyRuleParameterEntry[] getWaterKeys(int range) {
176: RangeRuleParameterEntry[] water = getWaterPolicyRanges();
177: if (water != null) {
178: KeyRuleParameter krp = (KeyRuleParameter) water[range]
179: .getValue();
180: if (krp != null)
181: return krp.getKeys();
182: }
183: return EMPTY_KEY_ENTRY;
184: }
185:
186: public boolean equals(Object o) {
187: if (this == o) {
188: return true;
189: }
190: if (o instanceof FeedingPolicy) {
191: FeedingPolicy fp2 = (FeedingPolicy) o;
192: return isSamePolicy(this .getMealPolicyRanges(), fp2
193: .getMealPolicyRanges(), this , fp2)
194: && isSamePolicy(this .getEnhancementsPolicyRanges(),
195: fp2.getEnhancementsPolicyRanges(), this ,
196: fp2)
197: && isSamePolicy(this .getWaterPolicyRanges(), fp2
198: .getWaterPolicyRanges(), this , fp2)
199: && isSamePolicy(this .getARationPolicyRanges(), fp2
200: .getARationPolicyRanges(), this , fp2)
201: && isSamePolicy(this .getRules(), fp2.getRules(),
202: this , fp2);
203: }
204: return false;
205: }
206:
207: public boolean isSamePolicy(RangeRuleParameterEntry[] range1,
208: RangeRuleParameterEntry[] range2, FeedingPolicy fp1,
209: FeedingPolicy fp2) {
210:
211: if (range1.length != range2.length) {
212: return false;
213: }
214:
215: for (int i = 0; i < range1.length; i++) {
216: if (range1[i].getRangeMin() != range2[i].getRangeMin()) {
217: return false;
218: }
219: if (range1[i].getRangeMax() != range2[i].getRangeMax()) {
220: return false;
221: }
222:
223: KeyRuleParameterEntry[] keys1 = fp1.getRangeKeys(range1[i]);
224: KeyRuleParameterEntry[] keys2 = fp2.getRangeKeys(range2[i]);
225: for (int j = 0; j < keys1.length; j++) {
226: if (!keys1[j].getValue().equals(keys2[j].getValue())) {
227: return false;
228: }
229: }
230: }
231: return true;
232: }
233:
234: public static void main(String[] args) {
235: RangeRuleParameterEntry[] newRangeRule = new RangeRuleParameterEntry[1];
236: FeedingPolicy fp = new FeedingPolicy();
237: int end = 25;
238: int start = 0;
239:
240: KeyRuleParameter keyRule = new KeyRuleParameter(MEAL_POLICY);
241: KeyRuleParameterEntry[] keyEntries = new KeyRuleParameterEntry[3];
242: keyEntries[0] = new KeyRuleParameterEntry("Breakfast", "MRE");
243: keyEntries[1] = new KeyRuleParameterEntry("Lunch", "UGR");
244: keyEntries[2] = new KeyRuleParameterEntry("Dinner", "MRE");
245: keyRule.setKeys(keyEntries);
246: newRangeRule[0] = new RangeRuleParameterEntry(keyRule, start,
247: end);
248:
249: RangeRuleParameter rp = new RangeRuleParameter(MEAL_POLICY,
250: newRangeRule);
251: fp.Add(rp);
252:
253: RangeRuleParameterEntry rrpe[] = fp.getMealPolicyRanges();
254: KeyRuleParameterEntry[] keys = null;
255: System.out.println("what is the size " + rrpe.length);
256:
257: for (int i = 0; i < rrpe.length; i++) {
258: System.out.println("C+" + rrpe[i].getRangeMin() + " To C+"
259: + rrpe[i].getRangeMax());
260: KeyRuleParameter param = (KeyRuleParameter) rrpe[i]
261: .getValue();
262: keys = param.getKeys();
263:
264: for (int j = 0; j < keys.length; j++) {
265: System.out.println(keys[j].getValue());
266: }
267: }
268: /********************************/
269: newRangeRule = new RangeRuleParameterEntry[1];
270: FeedingPolicy fp2 = new FeedingPolicy();
271: end = 25;
272: start = 0;
273:
274: keyRule = new KeyRuleParameter(MEAL_POLICY);
275: keyEntries = new KeyRuleParameterEntry[3];
276: keyEntries[0] = new KeyRuleParameterEntry("Breakfast", "MRE");
277: keyEntries[1] = new KeyRuleParameterEntry("Lunch", "UGR");
278: keyEntries[2] = new KeyRuleParameterEntry("Dinner", "MRE");
279: keyRule.setKeys(keyEntries);
280: newRangeRule[0] = new RangeRuleParameterEntry(keyRule, start,
281: end);
282:
283: rp = new RangeRuleParameter(MEAL_POLICY, newRangeRule);
284: fp2.Add(rp);
285:
286: System.out
287: .println("---------------------------- are they equal "
288: + fp.equals(fp2));
289: System.out
290: .println("---------------------------- are they equal "
291: + (fp == fp2));
292: }
293: }
|