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: package org.cougaar.glm.packer;
027:
028: import java.util.ArrayList;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Vector;
033:
034: import org.cougaar.glm.ldm.Constants;
035: import org.cougaar.glm.ldm.asset.GLMAsset;
036: import org.cougaar.glm.ldm.asset.NewContentsPG;
037: import org.cougaar.glm.ldm.asset.PropertyGroupFactory;
038: import org.cougaar.planning.ldm.asset.Asset;
039: import org.cougaar.planning.ldm.asset.ItemIdentificationPG;
040: import org.cougaar.planning.ldm.asset.TypeIdentificationPG;
041: import org.cougaar.planning.ldm.measure.Mass;
042: import org.cougaar.planning.ldm.plan.AllocationResultDistributor;
043: import org.cougaar.planning.ldm.plan.AspectType;
044: import org.cougaar.planning.ldm.plan.ContextOfOplanIds;
045: import org.cougaar.planning.ldm.plan.NewMPTask;
046: import org.cougaar.planning.ldm.plan.Plan;
047: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
048: import org.cougaar.planning.ldm.plan.Task;
049:
050: class Filler {
051: private static final String UNKNOWN = "UNKNOWN";
052: private Sizer _sz;
053:
054: private GenericPlugin _gp;
055:
056: /**
057: * This is the generator for the Multi-parent tasks that
058: * are the "product" of the aggregation. Currently assumes that
059: * we're creating Transport tasks.
060: */
061: private AggregationClosure _ac;
062:
063: private PreferenceAggregator _pa;
064:
065: /**
066: * The AllocationResultDistributor that should be used on
067: * any Containers created by this Filler
068: */
069: private AllocationResultDistributor _ard;
070:
071: public static double TRANSPORT_TONS;
072:
073: Filler(Sizer sz, GenericPlugin gp, AggregationClosure ac,
074: AllocationResultDistributor ard, PreferenceAggregator pa) {
075: _sz = sz;
076: _gp = gp;
077: _ac = ac;
078: _ard = ard;
079: _pa = pa;
080: }
081:
082: /**
083: * This is the driving function in the whole packing process.
084: */
085: public double execute() {
086: boolean finished = false;
087: double tonsPacked = 0;
088:
089: if (_gp.getLoggingService().isInfoEnabled())
090: _gp.getLoggingService().info("Filler.execute - entered.");
091: int numTasks = 0;
092: int numParents = 0;
093: while (!finished) {
094: // initialize the aggregation
095: ArrayList agglist = new ArrayList();
096: double amount = 0.0;
097: while (_ac.getQuantity() - amount > 0.0) {
098: Task t = _sz.provide(_ac.getQuantity() - amount);
099: if (t == null) {
100: finished = true;
101: break;
102: }
103: numTasks++;
104:
105: // if we reach here, t is a Task that provides
106: // some amount towards our overall amount
107: double provided = t
108: .getPreferredValue(AspectType.QUANTITY);
109:
110: if (!_ac.validTask(t)) {
111: _gp.getLoggingService().error(
112: "Filler.execute: AggregationClosure rejected "
113: + " task - " + t);
114: continue;
115: }
116:
117: amount += provided;
118: agglist.add(t);
119: }
120:
121: if (!agglist.isEmpty()) {
122: double loadedQuantity = createMPTask(agglist);
123: numParents += agglist.size();
124: TRANSPORT_TONS += loadedQuantity;
125: tonsPacked += loadedQuantity;
126: }
127: }
128:
129: if (numTasks != numParents)
130: _gp.getLoggingService().error(
131: "Filler.execute - num tasks created " + numTasks
132: + " != parents of MPTask " + numParents);
133:
134: if (numParents != _sz.sizedMade)
135: _gp.getLoggingService().error(
136: "Filler.execute - sizer num tasks made "
137: + _sz.sizedMade
138: + " != total parents of MPTask "
139: + numParents);
140:
141: if (_gp.getLoggingService().isInfoEnabled())
142: _gp.getLoggingService().info(
143: "Packer - current aggregated requested transport: "
144: + TRANSPORT_TONS + " tons.");
145:
146: if (_gp.getLoggingService().isInfoEnabled())
147: _gp.getLoggingService().info("Filler.execute - exited.");
148:
149: return tonsPacked;
150: }
151:
152: public double handleUnplanned(Task unplanned) {
153: if (_gp.getLoggingService().isDebugEnabled())
154: _gp.getLoggingService().debug(
155: "Filler.handleUnplanned - replanning "
156: + unplanned.getUID());
157: List agglist = new ArrayList();
158: agglist.add(unplanned);
159: double loadedQuantity = createMPTask(agglist);
160: return loadedQuantity;
161: }
162:
163: /** agglist is the list of parent tasks */
164: protected double createMPTask(List agglist) {
165: // now we do the aggregation
166: NewMPTask mpt = _ac.newTask();
167:
168: HashSet set = new HashSet();
169: Iterator taskIt = agglist.iterator();
170: Task parentTask;
171: while (taskIt.hasNext()) {
172: parentTask = (Task) taskIt.next();
173: if (parentTask.getContext() != null) {
174: set.addAll((ContextOfOplanIds) parentTask.getContext());
175: }
176: }
177: mpt.setContext(new ContextOfOplanIds(set));
178:
179: // Set ContentsPG on container
180: addContentsInfo((GLMAsset) mpt.getDirectObject(), agglist);
181:
182: //BOZO
183: mpt.setPreferences(new Vector(_pa.aggregatePreferences(agglist
184: .iterator(), _gp.getGPFactory())).elements());
185: double loadedQuantity = mpt
186: .getPreferredValue(AspectType.QUANTITY);
187: Plan plan = ((Task) agglist.get(0)).getPlan();
188:
189: _gp.createAggregation(agglist.iterator(), mpt, plan, _ard);
190:
191: if (mpt.getComposition().getParentTasks().size() != agglist
192: .size())
193: _gp.getLoggingService().error(
194: "Filler.createMPTask - received "
195: + agglist.size()
196: + " tasks to be agggregated, but only "
197: + mpt.getComposition().getParentTasks()
198: .size() + " tasks as parents of "
199: + mpt.getUID());
200: return loadedQuantity;
201: }
202:
203: protected void addContentsInfo(GLMAsset container, List agglist) {
204: ArrayList typeIDs = new ArrayList();
205: ArrayList nomenclatures = new ArrayList();
206: ArrayList weights = new ArrayList();
207: ArrayList receivers = new ArrayList();
208:
209: for (Iterator iterator = agglist.iterator(); iterator.hasNext();) {
210: Task task = (Task) iterator.next();
211: TypeIdentificationPG typeIdentificationPG = task
212: .getDirectObject().getTypeIdentificationPG();
213: String typeID;
214: String nomenclature;
215: if (typeIdentificationPG != null) {
216: typeID = typeIdentificationPG.getTypeIdentification();
217: if ((typeID == null) || (typeID.equals(""))) {
218: typeID = UNKNOWN;
219: }
220:
221: nomenclature = typeIdentificationPG.getNomenclature();
222: if ((nomenclature == null) || (nomenclature.equals(""))) {
223: nomenclature = UNKNOWN;
224: }
225: } else {
226: typeID = UNKNOWN;
227: nomenclature = UNKNOWN;
228: }
229: typeIDs.add(typeID);
230: nomenclatures.add(nomenclature);
231:
232: double quantity = task
233: .getPreferredValue(AspectType.QUANTITY);
234: Mass mass = Mass.newMass(quantity, Mass.SHORT_TONS);
235: weights.add(mass);
236:
237: Object receiver = task
238: .getPrepositionalPhrase(Constants.Preposition.FOR);
239:
240: if (receiver != null)
241: receiver = ((PrepositionalPhrase) receiver)
242: .getIndirectObject();
243:
244: String receiverID;
245:
246: // Add field with recipient
247: if (receiver == null) {
248: receiverID = UNKNOWN;
249: _gp.getLoggingService().error(
250: "Filler.addContentsInfo - Task "
251: + task.getUID() + " had no FOR prep.");
252: } else if (receiver instanceof String) {
253: receiverID = (String) receiver;
254: } else if (!(receiver instanceof Asset)) {
255: receiverID = UNKNOWN;
256: } else {
257: ItemIdentificationPG itemIdentificationPG = ((Asset) receiver)
258: .getItemIdentificationPG();
259: if ((itemIdentificationPG == null)
260: || (itemIdentificationPG
261: .getItemIdentification() == null)
262: || (itemIdentificationPG
263: .getItemIdentification().equals(""))) {
264: receiverID = UNKNOWN;
265: } else {
266: receiverID = itemIdentificationPG
267: .getItemIdentification();
268: }
269: }
270: receivers.add(receiverID);
271: }
272:
273: // Contents
274: NewContentsPG contentsPG = PropertyGroupFactory.newContentsPG();
275: contentsPG.setNomenclatures(nomenclatures);
276: contentsPG.setTypeIdentifications(typeIDs);
277: contentsPG.setWeights(weights);
278: contentsPG.setReceivers(receivers);
279: container.setContentsPG(contentsPG);
280: }
281:
282: }
|