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 AmmoConsumerBG extends ConsumerBG {
051:
052: public static HashMap cachedDBValues = new HashMap();
053: protected AmmoConsumerPG myPG;
054: transient MEIPrototypeProvider parentPlugin;
055: String supplyType = "Ammunition";
056: HashMap consumptionRates = null;
057: private transient LoggingService logger;
058:
059: public AmmoConsumerBG(AmmoConsumerPG 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: } else {
106: if (logger.isErrorEnabled()) {
107: logger
108: .error("getParameterSchedule: unknown predicate");
109: }
110: }
111: }
112: paramSchedule = parentPlugin.getScheduleUtils()
113: .getMergedSchedule(params);
114: return paramSchedule;
115: }
116:
117: public Rate getRate(Asset asset, List params) {
118: Rate r = null;
119: // DEBUG
120: // String myOrgName = parentPlugin.getMyOrg().getItemIdentificationPG().getItemIdentification();
121: if (consumptionRates == null) {
122: return r;
123: }
124: if (params == null) {
125: if (logger.isErrorEnabled()) {
126: logger.error("getRate() params null for "
127: + asset.getTypeIdentificationPG()
128: .getNomenclature());
129: }
130: // if (myOrgName.indexOf("35-ARBN") >= 0) {
131: // System.out.println("getRate() params null for "+
132: // asset.getTypeIdentificationPG().getNomenclature());
133: // }
134: return r;
135: }
136: Double qty = (Double) params.get(0);
137: OrgActivity orgAct = (OrgActivity) params.get(1);
138: if (orgAct == null) {
139: if (logger.isDebugEnabled()) {
140: logger.debug("getRate() orgAct null for "
141: + asset.getTypeIdentificationPG()
142: .getNomenclature());
143: }
144:
145: return r;
146: }
147: HashMap map = (HashMap) consumptionRates.get(asset);
148: if (map == null) {
149: if (logger.isErrorEnabled()) {
150: logger.error("getRate() no Ammo consumption for "
151: + asset.getTypeIdentificationPG()
152: .getNomenclature());
153: }
154:
155: return r;
156: }
157:
158: Double d = (Double) map.get(orgAct.getOpTempo().toUpperCase());
159: if (d == null) {
160: if (logger.isErrorEnabled()) {
161: logger.error("getRate() consumption rate null for "
162: + asset.getTypeIdentificationPG()
163: .getNomenclature());
164: }
165:
166: return r;
167: }
168: r = CountRate.newEachesPerDay(d.doubleValue()
169: * qty.doubleValue());
170:
171: return r;
172: }
173:
174: public Collection getConsumed() {
175: if (consumptionRates == null) {
176: synchronized (cachedDBValues) {
177: Asset asset = myPG.getMei();
178: if (asset instanceof AggregateAsset) {
179: asset = ((AggregateAsset) asset).getAsset();
180: }
181: String typeId = asset.getTypeIdentificationPG()
182: .getTypeIdentification();
183:
184: String echelon = parentPlugin.getMyOrg()
185: .getMilitaryOrgPG().getEchelon();
186: String key = typeId + "-" + echelon;
187:
188: consumptionRates = (HashMap) cachedDBValues.get(key);
189: if (consumptionRates == null) {
190: Vector result = parentPlugin
191: .lookupAssetConsumptionRate(asset,
192: supplyType, myPG.getService(), myPG
193: .getTheater());
194: if (result == null) {
195: if (logger.isDebugEnabled()) {
196: logger
197: .debug("getConsumed(): Database query returned EMPTY result set for "
198: + myPG.getMei()
199: + ", "
200: + supplyType);
201: }
202: } else {
203: consumptionRates = parseResults(result);
204: cachedDBValues.put(key, consumptionRates);
205: }
206: }
207: }
208: }
209: if (consumptionRates == null) {
210: if (logger.isDebugEnabled()) {
211: logger.debug("No consumption rates for "
212: + myPG.getMei()
213: + " at "
214: + parentPlugin.getMyOrg()
215: .getItemIdentificationPG()
216: .getItemIdentification());
217: }
218: consumptionRates = new HashMap();
219: }
220: return consumptionRates.keySet();
221: }
222:
223: public Collection getConsumed(int x) {
224: return getConsumed();
225: }
226:
227: public Collection getConsumed(int x, int y) {
228: return getConsumed();
229: }
230:
231: protected HashMap parseResults(Vector result) {
232: String mei_nsn, typeid, optempo;
233: double dcr;
234: Asset newAsset;
235: HashMap map = null, ratesMap = new HashMap();
236: Enumeration results = result.elements();
237: Object row[];
238:
239: for (int i = 0; results.hasMoreElements(); i++) {
240: row = (Object[]) results.nextElement();
241: mei_nsn = (String) row[0];
242: if (logger.isDebugEnabled()) {
243: logger.debug("Ammo: parsing results for MEI nsn: "
244: + mei_nsn);
245: }
246: typeid = "DODIC/" + (String) row[1];
247: newAsset = parentPlugin.getPrototype(typeid);
248: if (newAsset != null) {
249: optempo = (String) row[2];
250: dcr = ((BigDecimal) row[3]).doubleValue();
251: map = (HashMap) ratesMap.get(newAsset);
252: if (map == null) {
253: map = new HashMap();
254: ratesMap.put(newAsset, map);
255: }
256: map.put(optempo.toUpperCase(), new Double(dcr));
257: if (logger.isDebugEnabled()) {
258: logger.debug("parseResult() for " + newAsset
259: + ", MEI " + mei_nsn + ", DCR " + dcr
260: + ", Optempo " + optempo);
261: }
262: } else {
263: if (logger.isErrorEnabled()) {
264: logger
265: .error("parseResults() Unable to get prototype for "
266: + typeid);
267: }
268: }
269: }
270: return ratesMap;
271: } // parseResults
272:
273: public PGDelegate copy(PropertyGroup pg) {
274: return null;
275: }
276:
277: }
|