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.ldm.asset;
028:
029: import org.cougaar.core.service.LoggingService;
030: import org.cougaar.glm.ldm.oplan.OrgActivity;
031: import org.cougaar.logistics.ldm.MEIPrototypeProvider;
032: import org.cougaar.logistics.plugin.utils.OrgActivityPred;
033: import org.cougaar.planning.ldm.asset.AggregateAsset;
034: import org.cougaar.planning.ldm.asset.Asset;
035: import org.cougaar.planning.ldm.asset.PGDelegate;
036: import org.cougaar.planning.ldm.asset.PropertyGroup;
037: import org.cougaar.planning.ldm.measure.FlowRate;
038: import org.cougaar.planning.ldm.measure.Rate;
039: import org.cougaar.planning.ldm.plan.Schedule;
040: import org.cougaar.util.TimeSpan;
041: import org.cougaar.util.UnaryPredicate;
042:
043: import java.math.BigDecimal;
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Enumeration;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Vector;
051:
052: public class FuelConsumerBG extends ConsumerBG {
053:
054: public static HashMap cachedDBValues = new HashMap();
055: protected FuelConsumerPG myPG;
056: transient MEIPrototypeProvider parentPlugin;
057: String supplyType = "BulkPOL";
058: HashMap consumptionRates = null;
059: private transient LoggingService logger;
060:
061: public FuelConsumerBG(FuelConsumerPG pg) {
062: myPG = pg;
063: }
064:
065: public void initialize(MEIPrototypeProvider plugin) {
066: parentPlugin = plugin;
067: logger = parentPlugin.getLoggingService(this );
068: }
069:
070: public List getPredicates() {
071: ArrayList predList = new ArrayList();
072: predList.add(new OrgActivityPred());
073: return predList;
074: }
075:
076: public Schedule getParameterSchedule(Collection col, TimeSpan span) {
077: Schedule paramSchedule = null;
078: Vector params = new Vector();
079: Iterator predList = col.iterator();
080: UnaryPredicate predicate;
081: // DEBUG
082: // String myOrgName = parentPlugin.getMyOrg().getItemIdentificationPG().getItemIdentification();
083: // if (myOrgName.indexOf("35-ARBN") >= 0) {
084: // System.out.println("getParamSched() Asset is "+
085: // myPG.getMei().getTypeIdentificationPG().getTypeIdentification());
086: // }
087: ArrayList consumerlist = new ArrayList();
088: consumerlist.add(myPG.getMei());
089: Schedule consumerSched = parentPlugin.getScheduleUtils()
090: .createConsumerSchedule(consumerlist);
091: consumerSched = parentPlugin.getScheduleUtils()
092: .convertQuantitySchedule(consumerSched);
093: // DEBUG
094: // Schedule trimmedSched = parentPlugin.getScheduleUtils().trimObjectSchedule(consumerSched, span);
095: // if (myOrgName.indexOf("35-ARBN") >= 0) {
096: // System.out.println("TimeSpan "+parentPlugin.getTimeUtils().dateString(span.getStartTime())+
097: // " to "+parentPlugin.getTimeUtils().dateString(span.getEndTime()));
098: // System.out.println("CONSUMER Schedule: "+consumerSched);
099: // System.out.println("TRIMMED consumer: "+trimmedSched);
100: // }
101:
102: params.add(parentPlugin.getScheduleUtils().trimObjectSchedule(
103: consumerSched, span));
104: while (predList.hasNext()) {
105: Iterator list = ((Collection) predList.next()).iterator();
106: predicate = (UnaryPredicate) list.next();
107: if (predicate instanceof OrgActivityPred) {
108: Collection orgColl = (Collection) list.next();
109: if ((orgColl == null) || (orgColl.isEmpty())) {
110: return null;
111: }
112: Schedule orgActSched = parentPlugin.getScheduleUtils()
113: .createOrgActivitySchedule(orgColl);
114: // DEBUG
115: // trimmedSched = parentPlugin.getScheduleUtils().trimObjectSchedule(orgActSched, span);
116: // if (myOrgName.indexOf("35-ARBN") >= 0) {
117: // System.out.println("ORGACT Schedule: "+orgActSched);
118: // System.out.println("TRIMMED ORGACT: "+trimmedSched);
119: // }
120:
121: params.add(parentPlugin.getScheduleUtils()
122: .trimObjectSchedule(orgActSched, span));
123: } else {
124: if (logger.isErrorEnabled()) {
125: logger
126: .error("getParameterSchedule: unknown predicate");
127: }
128: }
129: }
130: paramSchedule = parentPlugin.getScheduleUtils()
131: .getMergedSchedule(params);
132:
133: // if (myOrgName.indexOf("35-ARBN") >= 0) {
134: // if (span != null)
135: // System.out.println("TimeSpan "+parentPlugin.getTimeUtils().dateString(span.getStartTime())+
136: // " to "+parentPlugin.getTimeUtils().dateString(span.getEndTime()));
137: // System.out.println(myPG.getMei().getTypeIdentificationPG().getTypeIdentification()+" PARAM SCHED: "+
138: // paramSchedule);
139: // }
140: return paramSchedule;
141: }
142:
143: public Rate getRate(Asset asset, List params) {
144: Rate r = null;
145:
146: if (consumptionRates == null) {
147: return r;
148: }
149: if (params == null) {
150: if (logger.isErrorEnabled()) {
151: logger.error("getRate() params null for "
152: + asset.getTypeIdentificationPG()
153: .getNomenclature());
154: }
155:
156: return r;
157: }
158: Double qty = (Double) params.get(0);
159: OrgActivity orgAct = (OrgActivity) params.get(1);
160: if (orgAct == null) {
161: if (logger.isDebugEnabled()) {
162: logger.debug("getRate() orgAct null for "
163: + asset.getTypeIdentificationPG()
164: .getNomenclature());
165: }
166:
167: return r;
168: }
169: HashMap map = (HashMap) consumptionRates.get(asset);
170: if (map == null) {
171: if (logger.isErrorEnabled()) {
172: logger.error("getRate() no bulkpol consumption for "
173: + asset.getTypeIdentificationPG()
174: .getNomenclature());
175: }
176:
177: return r;
178: }
179:
180: Double d = (Double) map.get(orgAct.getOpTempo().toUpperCase());
181: if (d == null) {
182: if (logger.isErrorEnabled()) {
183: logger.error("getRate() consumption rate null for "
184: + asset.getTypeIdentificationPG()
185: .getNomenclature());
186: }
187:
188: return r;
189: }
190: r = FlowRate.newGallonsPerDay(d.doubleValue()
191: * qty.doubleValue());
192:
193: return r;
194: }
195:
196: public Collection getConsumed() {
197: if (consumptionRates == null) {
198: synchronized (cachedDBValues) {
199: Asset asset = myPG.getMei();
200: if (asset instanceof AggregateAsset) {
201: asset = ((AggregateAsset) asset).getAsset();
202: }
203: String typeId = asset.getTypeIdentificationPG()
204: .getTypeIdentification();
205: consumptionRates = (HashMap) cachedDBValues.get(typeId);
206: if (consumptionRates == null) {
207: Vector result = parentPlugin
208: .lookupAssetConsumptionRate(asset,
209: supplyType, myPG.getService(), myPG
210: .getTheater());
211: if (result == null) {
212: if (logger.isDebugEnabled()) {
213: logger
214: .debug("getConsumed(): Database query returned EMPTY result set for "
215: + myPG.getMei()
216: + ", "
217: + supplyType);
218: }
219: } else {
220: consumptionRates = parseResults(result);
221: cachedDBValues.put(typeId, consumptionRates);
222: }
223: }
224: }
225: }
226: if (consumptionRates == null) {
227: if (logger.isDebugEnabled()) {
228: logger.debug("No consumption rates for "
229: + myPG.getMei()
230: + " at "
231: + parentPlugin.getMyOrg()
232: .getItemIdentificationPG()
233: .getItemIdentification());
234: }
235: consumptionRates = new HashMap();
236: }
237: return consumptionRates.keySet();
238: }
239:
240: public Collection getConsumed(int x) {
241: return getConsumed();
242: }
243:
244: public Collection getConsumed(int x, int y) {
245: return getConsumed();
246: }
247:
248: protected HashMap parseResults(Vector result) {
249: String mei_nsn, typeid, optempo;
250: double dcr;
251: Asset newAsset;
252: HashMap map = null, ratesMap = new HashMap();
253: Enumeration results = result.elements();
254: Object row[];
255:
256: for (int i = 0; results.hasMoreElements(); i++) {
257: row = (Object[]) results.nextElement();
258: mei_nsn = (String) row[0];
259: if (logger.isDebugEnabled()) {
260: logger.debug("FUEL: parsing results for MEI nsn: "
261: + mei_nsn);
262: }
263: typeid = "NSN/" + (String) row[1];
264: newAsset = parentPlugin.getPrototype(typeid);
265: if (newAsset != null) {
266: optempo = (String) row[2];
267: dcr = ((BigDecimal) row[3]).doubleValue();
268: map = (HashMap) ratesMap.get(newAsset);
269: if (map == null) {
270: map = new HashMap();
271: ratesMap.put(newAsset, map);
272: }
273: map.put(optempo.toUpperCase(), new Double(dcr));
274: if (logger.isDebugEnabled()) {
275: logger.debug("parseResult() for " + newAsset
276: + ", MEI " + mei_nsn + ", DCR " + dcr
277: + ", Optempo " + optempo);
278: }
279: }
280: }
281: return ratesMap;
282: } // parseResults
283:
284: public PGDelegate copy(PropertyGroup pg) {
285: return null;
286: }
287:
288: }
|