001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.logistics.plugin.trans;
027:
028: import java.util.*;
029:
030: import org.cougaar.lib.vishnu.client.XMLizer;
031: import org.cougaar.lib.vishnu.client.custom.CustomVishnuAggregatorPlugin;
032:
033: import org.cougaar.planning.ldm.asset.AbstractAsset;
034: import org.cougaar.planning.ldm.asset.Asset;
035:
036: import org.cougaar.planning.ldm.plan.MPTask;
037: import org.cougaar.planning.ldm.plan.Task;
038:
039: import org.cougaar.logistics.ldm.Constants;
040:
041: import org.cougaar.glm.ldm.asset.GLMAsset;
042: import org.cougaar.glm.ldm.asset.TransportationRoute;
043:
044: import org.cougaar.logistics.plugin.trans.GLMTransConst;
045:
046: import org.cougaar.glm.util.GLMPrepPhrase;
047:
048: /**
049: * This class is necessary because the ShipPacker needs a DataXMLizer that <br>
050: * adds information specific to the ship packing problem.
051: */
052: public class SeaVishnuPlugin extends GenericVishnuPlugin {
053: protected int SUB_PROCESS_SIZE;
054:
055: public void localSetup() {
056: super .localSetup();
057:
058: try {
059: SUB_PROCESS_SIZE = (getMyParams()
060: .hasParam("SUB_PROCESS_SIZE")) ? getMyParams()
061: .getIntParam("SUB_PROCESS_SIZE") : 100;
062: } catch (Exception bogus) {
063: error("got bogus, never happen exception " + bogus);
064: }
065:
066: glmPrepHelper = new GLMPrepPhrase(logger);
067: }
068:
069: Comparator tasksComparator = new TaskSorter();
070:
071: /**
072: * Sort first by arrival time window width, then by UID.
073: *
074: * E.g. a task with ready at date of 9-10 and arrival at 10-10 will go
075: * before a task with ready at of 8-10 and arrival of 10-10, since it's
076: * more constrained.
077: *
078: * If don't sort by UID, throws away subsequently added tasks with same
079: * arrival times are previous ones...
080: */
081: class TaskSorter implements Comparator {
082: public int compare(Object obj1, Object obj2) {
083: Task t1 = (Task) obj1;
084: Task t2 = (Task) obj2;
085: long best1 = prefHelper.getBestDate(t1).getTime();
086: long ready1 = prefHelper.getReadyAt(t1).getTime();
087: long diff1 = best1 - ready1;
088: long best2 = prefHelper.getBestDate(t2).getTime();
089: long ready2 = prefHelper.getReadyAt(t2).getTime();
090: long diff2 = best2 - ready2;
091: if (diff1 < diff2)
092: return -1;
093: else if (diff1 > diff2)
094: return 1;
095:
096: return t1.getUID().compareTo(t2.getUID());
097: }
098:
099: public boolean equals(Object obj1, Object obj2) {
100: Task t1 = (Task) obj1;
101: Task t2 = (Task) obj2;
102: return (t1.equals(t2));
103: }
104: }
105:
106: /**
107: * This avoid n^2 problem with prerequisite calculations within Vishnu!
108: */
109: public void processTasks(List tasks) {
110: // make a sorted set of tasks, sorted by best arrival time
111: SortedSet sorted = new TreeSet(tasksComparator);
112: sorted.addAll(tasks);
113:
114: if (isInfoEnabled()) {
115: info("processTasks - got " + tasks.size() + " now has "
116: + sorted.size() + " sorted.");
117: }
118:
119: List toProcess = new ArrayList(SUB_PROCESS_SIZE);
120: for (Iterator iter = sorted.iterator(); iter.hasNext();) {
121: toProcess.add(iter.next());
122: if (toProcess.size() == SUB_PROCESS_SIZE) {
123: if (isInfoEnabled()) {
124: info("processing task sub set of size "
125: + toProcess.size());
126: }
127:
128: super .processTasks(toProcess);
129: toProcess.clear();
130: }
131: }
132:
133: if (!toProcess.isEmpty()) {
134: if (isInfoEnabled()) {
135: info("at end, processing task sub set of size "
136: + toProcess.size());
137: }
138:
139: super .processTasks(toProcess);
140: }
141: }
142:
143: /**
144: * override to use a different XMLizer <p>
145: *
146: * SeaDataXMLize adds fields for sea-specific info, like dealing with Ammunition
147: */
148: protected XMLizer createXMLizer(boolean direct) {
149: GenericDataXMLize xmlizer = new SeaDataXMLize(direct, logger);
150: setDataXMLizer(xmlizer);
151: return xmlizer;
152: }
153:
154: protected Task createMainTask(Task task, Asset asset, Date start,
155: Date end, Date setupStart, Date wrapupEnd) {
156: Task mainTask = super .createMainTask(task, asset, start, end,
157: setupStart, wrapupEnd);
158:
159: // attach route
160: TransportationRoute route = (TransportationRoute) glmPrepHelper
161: .getIndirectObject(task, GLMTransConst.SEAROUTE);
162:
163: glmPrepHelper.addPrepToTask(mainTask, glmPrepHelper
164: .makePrepositionalPhrase(ldmf,
165: Constants.Preposition.VIA, route));
166: glmPrepHelper.removePrepNamed(mainTask, GLMTransConst.SEAROUTE); // not necessary now that we put it on the VIA prep
167:
168: return mainTask;
169: }
170:
171: protected GLMPrepPhrase glmPrepHelper;
172: }
|