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.mlm.construction;
027:
028: import java.util.Enumeration;
029: import java.util.Vector;
030:
031: import org.cougaar.glm.ldm.asset.AssetConsumptionRatePG;
032: import org.cougaar.glm.ldm.asset.BulkPOL;
033: import org.cougaar.glm.ldm.plan.ObjectScheduleElement;
034: import org.cougaar.glm.ldm.plan.QuantityScheduleElement;
035: import org.cougaar.glm.ldm.plan.Service;
036: import org.cougaar.glm.plugins.projection.ConsumerSpec;
037: import org.cougaar.planning.ldm.PlanningFactory;
038: import org.cougaar.planning.ldm.asset.Asset;
039: import org.cougaar.planning.ldm.measure.CountRate;
040: import org.cougaar.planning.ldm.measure.FlowRate;
041: import org.cougaar.planning.ldm.measure.Rate;
042: import org.cougaar.planning.ldm.plan.Schedule;
043: import org.cougaar.planning.ldm.plan.ScheduleElement;
044:
045: /*
046: * Construction (ClassIV) Demand Spec for producing inventory demand
047: *
048: **/
049: public class ConstructionDemandSpec extends ConsumerSpec {
050:
051: private String className = "ConstructionDemandSpec";
052: Service service_;
053: String theater_;
054: Schedule quantitySchedule_;
055: transient Vector paramSchedules_ = null;
056:
057: Vector assetList;
058: Vector acrList;
059:
060: private Schedule consumer_sched;
061: private Schedule materiel_sched;
062:
063: private transient PlanningFactory theLDMFactory;
064:
065: // 86400000 msec/day = 1000msec/sec * 60sec/min *60min/hr * 24 hr/day
066: protected static final long MSEC_PER_DAY = 86400000;
067:
068: public ConstructionDemandSpec(Asset c, Schedule consumer_sched,
069: Schedule materiel_sched, PlanningFactory theLDMF) {
070:
071: super (c, "ClassIVConstructionMaterial");
072: this .theLDMFactory = theLDMF;
073: this .materiel_sched = materiel_sched;
074:
075: // convert QuantitySchedule (needed to use ScheduleUtils in core)
076: // to ObjectSchedule (so all param schedules are same type)
077: quantitySchedule_ = convertQuantitySchedule(consumer_sched);
078:
079: AssetConsumptionRatePG acrpg = (AssetConsumptionRatePG) ((Asset) consumer_)
080: .searchForPropertyGroup(AssetConsumptionRatePG.class);
081:
082: assetList = new Vector();
083: acrList = new Vector();
084:
085: //debug (" CONSTRUCTED"+ new Date());
086: } // constructor
087:
088: public void addAsset(Asset asset, Rate rate) {
089: //System.out.println("Asset class = " + asset.getClass());
090: //System.out.println("Asset typeID = " + asset.getTypeIdentificationPG().getTypeIdentification());
091: assetList.add(asset);
092: acrList.add(rate);
093: }
094:
095: public static String getAssetIdentifier(Asset asset) {
096: return (asset.getClass() + asset.getTypeIdentificationPG()
097: .getTypeIdentification());
098: }
099:
100: public Enumeration getConsumed() {
101: return assetList.elements();
102:
103: } // getConsumed
104:
105: public Vector getParameterSchedules() {
106: if (paramSchedules_ == null) {
107: paramSchedules_ = new Vector();
108: paramSchedules_.add(quantitySchedule_);
109: paramSchedules_
110: .add(convertQuantitySchedule(materiel_sched));
111: }
112: return paramSchedules_;
113: } // getParameterSchedules
114:
115: // checks all elements are ObjectScheduleElements
116: protected void checkSchedule(Schedule sched) {
117: Enumeration elements = sched.getAllScheduleElements();
118: ScheduleElement el;
119: while (elements.hasMoreElements()) {
120: el = (ScheduleElement) elements.nextElement();
121: if (!(el.getClass().getName()
122: .equals("org.cougaar.glm.ldm.plan.ObjectScheduleElement"))) {
123:
124: debug("checkSchedule wrong class " + el + " of type "
125: + el.getClass().getName());
126: } // if
127: } // while
128: } // checkSchedule
129:
130: public Schedule convertQuantitySchedule(Schedule qty_sched) {
131: ObjectScheduleElement element;
132: QuantityScheduleElement qty_el;
133: Vector sched_els = new Vector();
134: Enumeration qty_els = qty_sched.getAllScheduleElements();
135: while (qty_els.hasMoreElements()) {
136: qty_el = (QuantityScheduleElement) qty_els.nextElement();
137: element = new ObjectScheduleElement(qty_el.getStartDate(),
138: qty_el.getEndDate(), new Double(qty_el
139: .getQuantity()));
140: sched_els.addElement(element);
141: } // while
142: Schedule result_sched = newObjectSchedule(sched_els.elements());
143: // this is a paranoid check - can be removed
144: // checks all elements are ObjectScheduleElements
145: checkSchedule(result_sched);
146: return result_sched;
147: } // convertQuantity
148:
149: // The asset in this case is the Major End Item
150: // The params are:
151: // Element 0 : Number of consumers
152: // Element 1 : Org Activity (for Optempo)
153: public Rate getRate(Asset resource, Vector params) {
154: double quantity = 0;
155: Object obj = params.get(0);
156: if (obj instanceof Double) {
157: quantity = ((Double) obj).doubleValue();
158: } else {
159: if (obj != null) {
160: debug("Bad param - expected quantity got " + obj);
161: } // if
162: return null;
163: } // if
164:
165: obj = params.get(1);
166: if (obj == null)
167: return null;
168:
169: Rate result = null;
170: Rate rate = null;
171: //int index = assetList.indexOf(resource);
172: //int index = assetList.indexOf(getAssetIdentifier(resource));
173: for (int i = 0; i < assetList.size(); i++) {
174: Asset asset = (Asset) assetList.elementAt(i);
175: if (getAssetIdentifier(resource).equals(
176: getAssetIdentifier(asset))) {
177: //System.out.println("Matched asset with stored list, returning rate....");
178: rate = (Rate) acrList.elementAt(i);
179: }
180: }
181: if (rate == null)
182: System.err.println("ERROR - could not find acr for "
183: + resource);
184: /*
185: if (index > 0) {
186: rate = (Rate)acrList.elementAt(index);
187: }
188: else {
189: System.err.println("ERROR - could not find acr for " + resource);
190: } */
191: //acr_.getRate(resource, act);
192: //Deans' "fix" ==> Rate rate = null;
193: if (rate != null) {
194: double multiplier = getMultiplier(
195: resource.getTypeIdentificationPG()
196: .getTypeIdentification()).doubleValue();
197: if (resource instanceof BulkPOL) {
198: double flow = ((FlowRate) rate).getGallonsPerDay()
199: * quantity * multiplier;
200: if (flow > 0.0) {
201: result = FlowRate.newGallonsPerDay(flow);
202: } // if
203:
204: } else {
205: double count = ((CountRate) rate).getEachesPerDay()
206: * quantity * multiplier;
207: if (count > 0.0) {
208: // we CAN get fractional EACH demand
209: result = CountRate.newEachesPerDay(count);
210: } // if
211: } // if
212: } // if
213: return result;
214: } // getRate
215:
216: public String describe() {
217: return "ConstructionDemandSpec for " + consumer_ + " oftype "
218: + resourceType_;
219: }
220:
221: private void debug(String msg) {
222: System.out.println("[ConstructionDemandSpec] " + msg);
223: } // debug
224:
225: } // ConstructionDemandSpec
|