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.logistics.plugin.packer;
028:
029: import org.cougaar.core.blackboard.IncrementalSubscription;
030: import org.cougaar.planning.ldm.plan.AllocationResult;
031: import org.cougaar.planning.ldm.plan.AspectType;
032: import org.cougaar.planning.ldm.plan.AspectValue;
033: import org.cougaar.planning.ldm.plan.PlanElement;
034: import org.cougaar.planning.ldm.plan.Task;
035: import org.cougaar.planning.plugin.util.PluginHelper;
036: import org.cougaar.util.UnaryPredicate;
037:
038: import java.util.ArrayList;
039: import java.util.Collection;
040: import java.util.Enumeration;
041:
042: /**
043: * ALAmmoPacker - handles packing ammo supply requests
044: *
045: */
046: public class ALAmmoPacker extends ALPacker {
047:
048: /**
049: * ALAmmoPacker - constructor
050: */
051: public ALAmmoPacker() {
052: super ();
053: }
054:
055: protected void execute() {
056: super .execute();
057:
058: Collection unplannedInternal = getBlackboardService().query(
059: new UnaryPredicate() {
060: public boolean execute(Object obj) {
061: if (obj instanceof Task) {
062: Task task = (Task) obj;
063: return ((task
064: .getPrepositionalPhrase(GenericPlugin.INTERNAL) != null) && task
065: .getPlanElement() == null);
066: }
067: return false;
068: }
069: });
070: if (!unplannedInternal.isEmpty()) {
071: if (getLoggingService().isInfoEnabled()) {
072: getLoggingService().info(
073: "found " + unplannedInternal.size()
074: + " unplanned internal tasks.");
075: }
076: handleUnplanned(unplannedInternal);
077: }
078: }
079:
080: /**
081: * getTaskPredicate - returns predicate which screens for ammo supply tasks
082: *
083: * @return UnaryPredicate screens for incoming tasks which the packer should
084: * handle
085: */
086: public UnaryPredicate getTaskPredicate() {
087: return ALAmmoPackerPredicate.getInputTaskPredicate();
088: }
089:
090: /**
091: * getPlanElementPredicate - returns predicate which screens for plan
092: * elements which will need to have allocation results set. In this case,
093: * plan elements associated with Ammunition Supply tasks
094: *
095: * @return UnaryPredicate screens for plan elements which the packer is
096: * reponsible
097: */
098: public UnaryPredicate getPlanElementPredicate() {
099: return ALAmmoPackerPredicate.getPlanElementPredicate();
100: }
101:
102: /*
103: * getAggregationClosure - returns AggregationClosure for transporting ammo
104: */
105: public AggregationClosure getAggregationClosure(ArrayList tasks) {
106: // BOZO - source and destination should be taken from the tasks not
107: // hardcoded.
108: AmmoTransport ac = new AmmoTransport();
109:
110: ac.setGenericPlugin(this );
111: ac.setDestinations(tasks);
112:
113: return ac;
114: }
115:
116: protected Collection groupByAggregationClosure(Collection tasks) {
117: return AmmoTransport.getTransportGroups(tasks);
118: }
119:
120: protected void updateAllocationResult(
121: IncrementalSubscription planElements) {
122: // Make sure that quantity preferences get returned on the allocation
123: // results. Transport thread may not have filled them in.
124: Enumeration changedPEs = planElements.getChangedList();
125: while (changedPEs.hasMoreElements()) {
126: PlanElement pe = (PlanElement) changedPEs.nextElement();
127:
128: if (!PluginHelper.checkChangeReports(planElements
129: .getChangeReports(pe),
130: PlanElement.ReportedResultChangeReport.class))
131: continue;
132:
133: if (PluginHelper.updatePlanElement(pe)) {
134: boolean needToCorrectQuantity = false;
135:
136: AllocationResult estimatedAR = pe.getEstimatedResult();
137: double prefValue = pe.getTask().getPreference(
138: AspectType.QUANTITY).getScoringFunction()
139: .getBest().getAspectValue().getValue();
140:
141: AspectValue[] aspectValues = estimatedAR
142: .getAspectValueResults();
143:
144: boolean foundQuantity = false;
145: for (int i = 0; i < aspectValues.length; i++) {
146: if (aspectValues[i].getAspectType() == AspectType.QUANTITY) {
147: if (aspectValues[i].getValue() != prefValue) {
148: // set the quantity to be the preference quantity
149: aspectValues[i] = aspectValues[i]
150: .dupAspectValue(prefValue);
151: needToCorrectQuantity = true;
152: }
153: foundQuantity = true;
154: break;
155: }
156: }
157:
158: // BOZO -
159: // Gordon Vidaver 08/16/02 -
160: // someone should really fix this eventually
161: // some expansion is not setting the QUANTITY aspect on the aspect value, and Inventory Manager
162: // gets upset about this missing aspect in findCommittedRefill
163: if (!foundQuantity) {
164: AspectValue[] copy = new AspectValue[aspectValues.length + 1];
165: System.arraycopy(aspectValues, 0, copy, 0,
166: aspectValues.length);
167: copy[aspectValues.length] = AspectValue
168: .newAspectValue(AspectType.QUANTITY,
169: prefValue);
170: aspectValues = copy;
171: if (getLoggingService().isDebugEnabled()) {
172: getLoggingService().debug(
173: "Packer.updateAllocationResult - fixing quantity on estimated AR of pe "
174: + pe.getUID() + " task "
175: + pe.getTask().getUID());
176: }
177: }
178:
179: if (needToCorrectQuantity || !foundQuantity) {
180: if (getLoggingService().isDebugEnabled()) {
181: getLoggingService().debug(
182: "Packer.updateAllocationResult - fixing quantity on estimated AR of pe "
183: + pe.getUID());
184: }
185:
186: AllocationResult correctedAR = new AllocationResult(
187: estimatedAR.getConfidenceRating(),
188: estimatedAR.isSuccess(), aspectValues);
189:
190: pe.setEstimatedResult(correctedAR);
191: }
192:
193: publishChange(pe);
194: }
195: }
196: }
197: }
|