001: /*--------------------------------------------------------------------------
002: * <copyright>
003: *
004: * Copyright 2000-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.plugins.inventory;
027:
028: import java.util.Enumeration;
029: import java.util.Hashtable;
030:
031: import org.cougaar.core.blackboard.IncrementalSubscription;
032: import org.cougaar.glm.ldm.Constants;
033: import org.cougaar.glm.ldm.asset.Ammunition;
034: import org.cougaar.glm.ldm.asset.BulkPOL;
035: import org.cougaar.glm.ldm.asset.Consumable;
036: import org.cougaar.glm.ldm.asset.Inventory;
037: import org.cougaar.glm.ldm.asset.Organization;
038: import org.cougaar.glm.ldm.asset.PackagedPOL;
039: import org.cougaar.glm.plugins.TaskUtils;
040: import org.cougaar.glm.plugins.TimeUtils;
041: import org.cougaar.planning.ldm.asset.Asset;
042: import org.cougaar.planning.ldm.plan.AllocationResult;
043: import org.cougaar.planning.ldm.plan.Role;
044: import org.cougaar.planning.ldm.plan.Task;
045: import org.cougaar.planning.plugin.util.AllocationResultHelper;
046: import org.cougaar.util.log.Logger;
047: import org.cougaar.util.log.Logging;
048:
049: public class WithdrawAllocator extends InventoryProcessor {
050:
051: protected IncrementalSubscription withdrawTasks_;
052: Hashtable bins_ = new Hashtable();
053: Role role_;
054: private static Logger logger = Logging
055: .getLogger(WithdrawAllocator.class);
056:
057: public WithdrawAllocator(InventoryPlugin plugin, Organization org,
058: String type, Role role) {
059: super (plugin, org, type);
060: role_ = role;
061: initialize();
062: }
063:
064: /**
065: * Set up subscriptions,
066: * get the this plugin's organization UIC, and
067: * initialize the OPLAN object.
068: */
069: private void initialize() {
070: // Subscribe to SupplyInventory Task
071: withdrawTasks_ = subscribe(inventoryPlugin_
072: .getDueOutPredicate(supplyType_));
073: }
074:
075: public void update() {
076: super .update(); // set up dates
077: if (inventoryPlugin_.getDetermineRequirementsTask() != null) {
078: allocateWithdrawTasks(withdrawTasks_.getAddedList());
079: allocateWithdrawTasks(withdrawTasks_.getChangedList());
080: }
081: }
082:
083: protected void allocateWithdrawTasks(Enumeration tasks) {
084: Task wdrawTask;
085: Inventory inventory;
086: Asset proto;
087: AllocationResult ar;
088: int num_tasks = 0;
089: while (tasks.hasMoreElements()) {
090: wdrawTask = (Task) tasks.nextElement();
091: proto = (Asset) wdrawTask.getDirectObject();
092: inventory = inventoryPlugin_.findOrMakeInventory(
093: supplyType_, proto);
094: // A withdraw task is not created unless findOrMakeInventory returns an
095: // inventory object, so this should never happen.
096: if (inventory == null) {
097: String typeID = proto.getTypeIdentificationPG()
098: .getTypeIdentification();
099: if (logger.isErrorEnabled()) {
100: logger.error("Inventory NOT found for " + typeID);
101: }
102: continue;
103: }
104: boolean isNew = wdrawTask.getPlanElement() == null;
105: if (logger.isDebugEnabled()) {
106: logger
107: .debug("allocateWithdrawTasks() "
108: + (isNew ? "new " : "change ")
109: + wdrawTask.getUID()
110: + " "
111: + TaskUtils.getDailyQuantity(wdrawTask)
112: + (TaskUtils.isProjection(wdrawTask) ? (" from "
113: + TimeUtils
114: .dateString(TaskUtils
115: .getStartTime(wdrawTask))
116: + " to " + TimeUtils
117: .dateString(TaskUtils
118: .getEndTime(wdrawTask)))
119: : (" on " + TimeUtils
120: .dateString(TaskUtils
121: .getEndTime(wdrawTask)))));
122: }
123: if (isNew) {
124: ar = new AllocationResultHelper(wdrawTask, null)
125: .getAllocationResult(1.0);
126: if (publishAllocation(wdrawTask, inventory, role_, ar)) {
127: // GLMDebug.DEBUG("WithdrawAllocator", clusterId_,
128: // "Allocating "+TaskUtils.taskDesc(wdrawTask)+" to inventory, with PlanElement:"+
129: // wdrawTask.getPlanElement().getUID()+ ", DESCRIPTION: "+inventoryDesc(inventory));
130: num_tasks++;
131: }
132: }
133: delegate_.publishChange(inventory);
134: }
135: if (num_tasks > 0) {
136: if (logger.isDebugEnabled()) {
137: logger.debug("allocateWithdrawTasks() allocated "
138: + num_tasks + " tasks to Inventory objects");
139: }
140: }
141:
142: }
143:
144: public Role roleForTask(Task t) {
145: Asset item = t.getDirectObject();
146: if (item instanceof Consumable) {
147: return Constants.Role.SPAREPARTSPROVIDER;
148: } else if (item instanceof BulkPOL) {
149: return Constants.Role.FUELSUPPLYPROVIDER;
150: } else if (item instanceof PackagedPOL) {
151: return Constants.Role.PACKAGEDPOLSUPPLYPROVIDER;
152: } else if (item instanceof Ammunition) {
153: return Constants.Role.AMMUNITIONPROVIDER;
154: }
155: return null;
156: }
157:
158: }
|