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