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.trans;
028:
029: import java.util.Collection;
030: import java.util.ArrayList;
031: import java.util.Vector;
032: import java.util.Iterator;
033: import java.util.Enumeration;
034: import java.util.HashSet;
035:
036: import org.cougaar.planning.ldm.asset.Asset;
037: import org.cougaar.planning.ldm.measure.CountRate;
038: import org.cougaar.planning.ldm.plan.*;
039: import org.cougaar.planning.plugin.util.PluginHelper;
040: import org.cougaar.planning.plugin.util.AllocationResultHelper;
041: import org.cougaar.logistics.ldm.Constants;
042: import org.cougaar.glm.ldm.plan.AlpineAspectType;
043:
044: /**
045: * <pre>
046: * The default RequirementsExpander for the DemandForecastPlugin.
047: *
048: * This class expands a determine requirements task into generateProjections tasks for each
049: * MEI that has the passed in supply class PG on it.
050: *
051: *
052: **/
053:
054: public class Level2Disposer extends Level2TranslatorModule {
055:
056: public Level2Disposer(Level2TranslatorPlugin translatePlugin) {
057: super (translatePlugin);
058: }
059:
060: /**
061: * Remove the expansion which is the transport version of the Level2 task
062: * and then successfully dispose of each level 2 Task in the collection (should be 1)/
063: **/
064: public void disposeAndRemoveExpansion(Collection taskCollect) {
065: Iterator it = taskCollect.iterator();
066: while (it.hasNext()) {
067: disposeAndRemoveExpansion((Task) it.next());
068: }
069: }
070:
071: /**
072: * Remove the expansion which is the transport version of the Level2 task
073: * and then successfully dispose of this one.
074: **/
075: public void disposeAndRemoveExpansion(Task level2Task) {
076: boolean doDispose = true;
077: PlanElement pe = level2Task.getPlanElement();
078: if ((pe != null) && (pe instanceof Expansion)) {
079: removeExpansion(level2Task);
080: } else if ((pe != null) && (pe instanceof Disposition)) {
081: doDispose = removeDisposition(level2Task);
082: }
083: if (doDispose) {
084: disposeOfTask(level2Task);
085: }
086: }
087:
088: public void removeExpansion(Task level2Task) {
089: Expansion expansion = (Expansion) level2Task.getPlanElement();
090: NewWorkflow wf = (NewWorkflow) expansion.getWorkflow();
091: Enumeration subtasks = wf.getTasks();
092: while (subtasks.hasMoreElements()) {
093: Task task = (Task) subtasks.nextElement();
094: wf.removeTask(task);
095: translatorPlugin.publishRemove(task);
096: }
097: //Do you need to remove the workflow?
098: //translatorPlugin.publishRemove(wf);
099: translatorPlugin.publishRemove(expansion);
100: }
101:
102: public boolean removeDisposition(Task level2Task) {
103: Disposition disposition = (Disposition) level2Task
104: .getPlanElement();
105: AllocationResult ar = disposition.getEstimatedResult();
106: if (getTaskUtils().getDailyQuantity(level2Task) == getTaskUtils()
107: .getQuantity(level2Task, ar)) {
108: if (logger.isInfoEnabled()) {
109: logger
110: .info("Quantity on task is same as on disposition - not re-disposing (Bug 13424)");
111: }
112: return false;
113: } else {
114: if (logger.isInfoEnabled()) {
115: logger.info("Re-disposing: Quantity on task is "
116: + getTaskUtils().getDailyQuantity(level2Task)
117: + " while the estimate result says "
118: + getTaskUtils().getQuantity(level2Task, ar)
119: + " - (Bug 13424)");
120: }
121: translatorPlugin.publishRemove(disposition);
122: return true;
123: }
124: }
125:
126: protected void disposeOfTask(Task task) {
127: AllocationResultHelper helper = new AllocationResultHelper(
128: task, null);
129: AllocationResult dispAR = helper.getAllocationResult(
130: Constants.Confidence.OBSERVED, true);
131: Disposition disposition = getPlanningFactory()
132: .createDisposition(task.getPlan(), task, dispAR);
133: translatorPlugin.publishAdd(disposition);
134: }
135:
136: /**
137: *
138: * TODO: MWD remove
139: *
140:
141: protected long getStartTimePref(Task task) {
142: synchronized (task) {
143: Enumeration taskPrefs = task.getPreferences();
144: while (taskPrefs.hasMoreElements()) {
145: Preference aPref = (Preference) taskPrefs.nextElement();
146: if (aPref.getAspectType() == AspectType.START_TIME) {
147: ScoringFunction sf = aPref.getScoringFunction();
148: return (long) sf.getBest().getValue();
149: }
150: }
151: }
152: return 0L;
153: }
154:
155: protected Preference createStartTimePref(Task parentTask) {
156: long startTime = getStartTimePref(parentTask);
157: AspectValue av = AspectValue.newAspectValue(AspectType.START_TIME, startTime);
158: ScoringFunction score = ScoringFunction.createNearOrAbove(av, 0);
159: return getPlanningFactory().newPreference(AspectType.START_TIME, score);
160: }
161:
162: public PrepositionalPhrase newPrepositionalPhrase(String preposition,
163: Object io) {
164: NewPrepositionalPhrase pp = getPlanningFactory().newPrepositionalPhrase();
165: pp.setPreposition(preposition);
166: pp.setIndirectObject(io);
167: return pp;
168: }
169:
170: public void updateAllocationResults(Collection planElements) {
171: Iterator peIt = planElements.iterator();
172: while (peIt.hasNext()) {
173: PlanElement pe = (PlanElement) peIt.next();
174: if (PluginHelper.updatePlanElement(pe)) {
175: dfPlugin.publishChange(pe);
176: }
177: }
178: }
179:
180:
181:
182: public static NewTask addPrepositionalPhrase(NewTask task, PrepositionalPhrase pp) {
183: Enumeration enum = task.getPrepositionalPhrases();
184: Vector phrases = new Vector();
185: while (enum.hasMoreElements()) {
186: phrases.addElement(enum.nextElement());
187: }
188: phrases.addElement(pp);
189: task.setPrepositionalPhrases(phrases.elements());
190: return task;
191: }
192:
193: **/
194: }
|