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 org.cougaar.planning.ldm.asset.Asset;
029:
030: import org.cougaar.planning.ldm.plan.Allocation;
031: import org.cougaar.planning.ldm.plan.MPTask;
032: import org.cougaar.planning.ldm.plan.PlanElement;
033: import org.cougaar.planning.ldm.plan.Task;
034:
035: import org.cougaar.planning.ldm.plan.AspectType;
036: import org.cougaar.planning.ldm.plan.AspectValue;
037:
038: import org.cougaar.lib.filter.UTILSingleTaskAllocatorPlugin;
039:
040: import java.util.Date;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import org.cougaar.logistics.ldm.Constants;
045: import org.cougaar.glm.ldm.asset.Organization;
046: import org.cougaar.glm.ldm.asset.GLMAsset;
047:
048: import org.cougaar.lib.callback.UTILExpandableTaskCallback;
049: import org.cougaar.lib.callback.UTILFilterCallback;
050: import org.cougaar.lib.callback.UTILGenericListener;
051:
052: /**
053: * <pre>
054: * Plugin that looks for TRANSIT or TRANSPORT tasks.
055: *
056: * Will just allocate the task to whatever asset
057: * is hanging off the WITH prep, since this plugin is meant to paired
058: * with and fire after a VishnuAggregatorPlugin. That plugin
059: * indicates the assignment of task to asset by attaching the WITH prep.
060: *
061: * </pre>
062: */
063: public class TransportAllocatorPlugin extends
064: UTILSingleTaskAllocatorPlugin {
065: /**
066: * <pre>
067: * This plugin is interested in tasks with verb TRANSIT or TRANSPORT
068: * The task must also have the VISHNU prep attached, indicating that the Vishnu
069: * plugin upstream has created the task.
070: * </pre>
071: * @param t task to check to see if it is an input task to this plugin
072: */
073: public boolean interestingTask(Task t) {
074: boolean hasTransitVerb = t.getVerb().equals(
075: Constants.Verb.TRANSIT);
076: boolean hasTransportVerb = t.getVerb().equals(
077: Constants.Verb.TRANSPORT);
078: boolean hasVishnu = prepHelper.hasPrepNamed(t, "VISHNU");
079:
080: if (hasTransitVerb && hasVishnu) {
081: if (isDebugEnabled())
082: debug(getName()
083: + ".interestingTask - interested in TRANSIT task "
084: + t.getUID());
085: return true;
086: }
087:
088: if (hasTransportVerb && hasVishnu) {
089: if (isDebugEnabled())
090: debug(getName()
091: + ".interestingTask - interested in TRANSPORT task "
092: + t.getUID());
093: return true;
094: }
095:
096: if (isDebugEnabled())
097: debug(getName() + ".interestingTask - NOT interested in "
098: + t.getUID());
099:
100: return false;
101: }
102:
103: /** determines the asset that is allocated to */
104: public Asset findAsset(Task t) {
105: Asset found = getAssetFromTask(t);
106: return found;
107: }
108:
109: /** looks at the task's WITH prep to get the asset -- was attached by the aggregator */
110: protected Asset getAssetFromTask(Task combinedTask) {
111: Object returnedObj = prepHelper.getIndirectObject(combinedTask,
112: Constants.Preposition.WITH);
113:
114: return (Asset) returnedObj;
115: }
116:
117: /**
118: * Deal with the tasks that we have accumulated.
119: * Find the asset that is attached to the task
120: * (in agents with this plugin the vishnu aggregator makes
121: * the task->asset assignment, encoded as a WITH preposition),
122: * make an allocation, and publish the allocation.
123: *
124: * @param List of tasks to handle
125: * @see #getAssetFromTask
126: */
127: public void processTasks(List tasks) {
128: for (Iterator iter = tasks.iterator(); iter.hasNext();) {
129: Task t = (Task) iter.next();
130: if (t.getPlanElement() == null) {
131: Asset a = findAsset(t);
132: PlanElement alloc = createAllocation(t, a);
133: publishAddingOfAllocation(alloc);
134: } else {
135: Object uid = ((Allocation) t.getPlanElement())
136: .getAsset().getUID();
137:
138: // this will happen when we become aware of changed tasks
139: if (isInfoEnabled()) {
140: info(getName() + " task " + t.getUID()
141: + " was already allocated to " + uid);
142: }
143:
144: if (!uid.equals(findAsset(t).getUID())) {
145: if (isWarnEnabled()) {
146: warn(getName()
147: + " task "
148: + t.getUID()
149: + " was already allocated to "
150: + uid
151: + " but trying to allocate to different asset "
152: + findAsset(t).getUID());
153: }
154: }
155: }
156: }
157:
158: tasks.clear();
159: }
160:
161: /**
162: * <pre>
163: * Do the actual allocation here
164: *
165: * If the asset is an organization, allocate with a MEDIUM confidence, otherwise,
166: * use a HIGH confidence.
167: *
168: * Uses two aspects : START and END time.
169: *
170: * </pre>
171: * @param t the task to allocate
172: * @param a the asset to allocate to
173: * @return the allocation
174: */
175: public PlanElement createAllocation(Task t, Asset a) {
176: Date from = prefHelper.getReadyAt(t);
177: Date to = prefHelper.getBestDate(t);
178:
179: double confidence = (((GLMAsset) a).hasOrganizationPG()) ? allocHelper.MEDIUM_CONFIDENCE
180: : allocHelper.HIGHEST_CONFIDENCE;
181:
182: if (isDebugEnabled())
183: debug(getName() + ".createAllocation - ready at " + from
184: + " - best " + to + " confidence " + confidence);
185:
186: AspectValue[] values = new AspectValue[2];
187: values[0] = AspectValue.newAspectValue(AspectType.START_TIME,
188: from.getTime());
189: values[1] = AspectValue.newAspectValue(AspectType.END_TIME, to
190: .getTime());
191:
192: PlanElement pe = allocHelper.makeAllocation(this, ldmf,
193: realityPlan, t, a, values, confidence,
194: Constants.Role.TRANSPORTER);
195: return pe;
196: }
197: }
|