001: /*
002: * <copyright>
003: *
004: * Copyright 1999-2004 Honeywell Inc
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: // utils
030:
031: import org.cougaar.glm.ldm.Constants;
032: import org.cougaar.glm.ldm.asset.Container;
033: import org.cougaar.glm.ldm.asset.NewContentsPG;
034: import org.cougaar.glm.ldm.asset.NewMovabilityPG;
035: import org.cougaar.glm.ldm.asset.PropertyGroupFactory;
036: import org.cougaar.glm.ldm.plan.GeolocLocation;
037: import org.cougaar.planning.ldm.asset.Asset;
038: import org.cougaar.planning.ldm.asset.NewItemIdentificationPG;
039: import org.cougaar.planning.ldm.plan.NewMPTask;
040: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
041: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
042: import org.cougaar.planning.ldm.plan.Priority;
043: import org.cougaar.planning.ldm.plan.Task;
044:
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.Vector;
050:
051: public class AmmoTransport extends AggregationClosure {
052: public static final String AMMO_CATEGORY_CODE = "MBB";
053: public static final String MILVAN_NSN = "NSN/8115001682275";
054: public static final double PACKING_LIMIT = 13.9; /* short tons */
055:
056: private static Asset MILVAN_PROTOTYPE = null;
057: private static long COUNTER = 0;
058:
059: private GeolocLocation mySource;
060: private GeolocLocation myDestination;
061:
062: public static Collection getTransportGroups(Collection tasks) {
063: HashMap destMap = new HashMap();
064:
065: for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
066: Task task = (Task) iterator.next();
067: GeolocLocation destination = getDestination(task);
068: Collection destTasks = (Collection) destMap
069: .get(destination);
070: if (destTasks == null) {
071: destTasks = new ArrayList();
072: destMap.put(destination, destTasks);
073: }
074: destTasks.add(task);
075: }
076:
077: return destMap.values();
078: }
079:
080: public AmmoTransport() {
081: }
082:
083: // public AmmoTransport(ArrayList tasks) {
084: // setDestinations(tasks);
085: // }
086:
087: public void setDestinations(ArrayList tasks) {
088: for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
089: Task t = (Task) iterator.next();
090: GeolocLocation taskSource = getSource(t);
091: GeolocLocation taskDestination = getDestination(t);
092:
093: if ((taskSource == null) || (taskDestination == null)) {
094:
095: _gp
096: .getLoggingService()
097: .error(
098: "AmmoTransport(): task without a source/destination");
099: } else if ((mySource == null) || (myDestination == null)) {
100: mySource = taskSource;
101: myDestination = taskDestination;
102: } else if (!(mySource.getGeolocCode().equals(taskSource
103: .getGeolocCode()))) {
104: _gp.getLoggingService().error(
105: "AmmoTransport(): " + mySource
106: + " not equal to " + taskSource);
107: } else if (!(myDestination.getGeolocCode()
108: .equals(taskDestination.getGeolocCode()))) {
109: _gp.getLoggingService().error(
110: "AmmoTransport(): " + myDestination
111: + " not equal to " + taskDestination);
112: }
113: }
114: }
115:
116: /**
117: * returns max quanitity in short tons
118: *
119: * BOZO - unit picked because it agreed with the incoming supply requests
120: */
121: public double getQuantity() {
122: return PACKING_LIMIT;
123: }
124:
125: /**
126: * returns appropriate transport source location
127: *
128: * Currently hardcoded because incoming supply tasks don't have that
129: * info
130: */
131: public static GeolocLocation getSource(Task task) {
132: return Geolocs.blueGrass();
133: }
134:
135: /**
136: * returns appropriate transport destination location
137: *
138: */
139: public static GeolocLocation getDestination(Task task) {
140: PrepositionalPhrase phrase = task
141: .getPrepositionalPhrase(Constants.Preposition.TO);
142: GeolocLocation destination = null;
143: if (phrase != null) {
144: destination = (GeolocLocation) phrase.getIndirectObject();
145: }
146: return destination;
147: }
148:
149: public boolean validTask(Task t) {
150: return (mySource.getGeolocCode().equals(
151: getSource(t).getGeolocCode()) && myDestination
152: .getGeolocCode().equals(
153: getDestination(t).getGeolocCode()));
154: }
155:
156: /**
157: * Creates a Transport task, per the interface published by TOPS.
158: */
159: public NewMPTask newTask() {
160: if (_gp == null) {
161: _gp
162: .getLoggingService()
163: .error(
164: "AmmoTransport: Error! AmmoTransport not properly initialized: setGenericPlugin not called.");
165: return null;
166: }
167:
168: if ((mySource == null) || (myDestination == null)) {
169: _gp
170: .getLoggingService()
171: .error(
172: "AmmoTransport: Error! AmmoTransport not properly initialized: some parameter(s) are null.");
173: return null;
174: }
175:
176: Asset milvan = makeMilvan();
177: if (milvan == null) {
178: return null;
179: }
180:
181: NewMPTask task = _gp.getGPFactory().newMPTask();
182: task.setVerb(Constants.Verb.Transport);
183:
184: task.setPriority(Priority.UNDEFINED);
185:
186: task.setDirectObject(milvan);
187:
188: Vector preps = new Vector(2);
189:
190: NewPrepositionalPhrase fromPrepositionalPhrase = _gp
191: .getGPFactory().newPrepositionalPhrase();
192: fromPrepositionalPhrase
193: .setPreposition(Constants.Preposition.FROM);
194: fromPrepositionalPhrase.setIndirectObject(mySource);
195: preps.addElement(fromPrepositionalPhrase);
196:
197: NewPrepositionalPhrase toPrepositionalPhrase = _gp
198: .getGPFactory().newPrepositionalPhrase();
199: toPrepositionalPhrase = _gp.getGPFactory()
200: .newPrepositionalPhrase();
201: toPrepositionalPhrase.setPreposition(Constants.Preposition.TO);
202: toPrepositionalPhrase.setIndirectObject(myDestination);
203: preps.addElement(toPrepositionalPhrase);
204:
205: task.setPrepositionalPhrases(preps.elements());
206:
207: return task;
208: }
209:
210: /**
211: * An ancillary method that creates an asset that represents a MILVAN
212: * (military container) carrying ammunition
213: */
214: protected Asset makeMilvan() {
215:
216: if (MILVAN_PROTOTYPE == null) {
217: MILVAN_PROTOTYPE = _gp.getGPFactory().getPrototype(
218: MILVAN_NSN);
219:
220: if (MILVAN_PROTOTYPE == null) {
221: _gp.getLoggingService().error(
222: "AmmoTransport: Error! Unable to get prototype for"
223: + " milvan NSN -" + MILVAN_NSN);
224: return null;
225: }
226: }
227:
228: Container milvan = (Container) _gp.getGPFactory()
229: .createInstance(MILVAN_PROTOTYPE);
230:
231: // AMMO Cargo Code
232: NewMovabilityPG movabilityPG = PropertyGroupFactory
233: .newMovabilityPG(milvan.getMovabilityPG());
234: movabilityPG.setCargoCategoryCode(AMMO_CATEGORY_CODE);
235: milvan.setMovabilityPG(movabilityPG);
236:
237: // Milvan Contents
238: NewContentsPG contentsPG = PropertyGroupFactory.newContentsPG();
239: milvan.setContentsPG(contentsPG);
240:
241: // Unique Item Identification
242: NewItemIdentificationPG itemIdentificationPG = (NewItemIdentificationPG) milvan
243: .getItemIdentificationPG();
244: String itemID = makeMilvanID();
245: itemIdentificationPG.setItemIdentification(itemID);
246: itemIdentificationPG.setNomenclature("Milvan");
247: itemIdentificationPG.setAlternateItemIdentification(itemID);
248: milvan.setItemIdentificationPG(itemIdentificationPG);
249:
250: return milvan;
251: }
252:
253: protected String makeMilvanID() {
254: return new String("Milvan" + getCounter());
255: }
256:
257: private static synchronized long getCounter() {
258: return COUNTER++;
259: }
260:
261: }
|