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;
028:
029: import org.cougaar.core.blackboard.IncrementalSubscription;
030: import org.cougaar.core.component.ServiceBroker;
031: import org.cougaar.core.service.BlackboardQueryService;
032: import org.cougaar.core.service.LoggingService;
033: import org.cougaar.glm.ldm.QueryLDMPlugin;
034: import org.cougaar.glm.ldm.asset.MilitaryPerson;
035: import org.cougaar.glm.ldm.asset.Organization;
036: import org.cougaar.glm.ldm.plan.Service;
037: import org.cougaar.logistics.ldm.asset.NewSubsistenceConsumerPG;
038: import org.cougaar.logistics.ldm.asset.SubsistenceConsumerBG;
039: import org.cougaar.logistics.ldm.asset.SubsistenceConsumerPG;
040: import org.cougaar.logistics.plugin.inventory.AssetUtils;
041: import org.cougaar.logistics.plugin.inventory.TaskUtils;
042: import org.cougaar.logistics.plugin.inventory.TimeUtils;
043: import org.cougaar.logistics.plugin.inventory.UtilsProvider;
044: import org.cougaar.logistics.plugin.utils.ScheduleUtils;
045: import org.cougaar.planning.ldm.PlanningFactory;
046: import org.cougaar.planning.ldm.asset.AggregateAsset;
047: import org.cougaar.planning.ldm.asset.Asset;
048: import org.cougaar.planning.ldm.asset.TypeIdentificationPG;
049: import org.cougaar.util.UnaryPredicate;
050:
051: import java.util.Collection;
052: import java.util.Enumeration;
053: import java.util.Iterator;
054: import java.util.Vector;
055:
056: import java.util.ArrayList;
057: import java.util.List;
058:
059: // Prototype Provider for Class I Subsistence Rations
060:
061: public class ClassIConsumerPrototypeProvider extends QueryLDMPlugin
062: implements UtilsProvider {
063: String service = null;
064: public static final String THEATER = "SWA";
065: boolean configured;
066: private IncrementalSubscription consumerSubscription;
067: private IncrementalSubscription myOrganizations;
068: private ServiceBroker serviceBroker;
069: private LoggingService logger;
070: private TaskUtils taskUtils;
071: private TimeUtils timeUtils;
072: private AssetUtils assetUtils;
073: private ScheduleUtils scheduleUtils;
074: private Organization myOrg;
075: private BlackboardQueryService queryService;
076:
077: private static UnaryPredicate orgsPredicate = new UnaryPredicate() {
078: public boolean execute(Object o) {
079: if (o instanceof Organization) {
080: return ((Organization) o).isSelf();
081: }
082: return false;
083: }
084: };
085:
086: // used for rehydration
087: private static UnaryPredicate personPredicate() {
088: return new UnaryPredicate() {
089: public boolean execute(Object o) {
090: if (o instanceof MilitaryPerson) {
091: return true;
092: }
093: if (o instanceof AggregateAsset) {
094: if (((AggregateAsset) o).getAsset() instanceof MilitaryPerson) {
095: return true;
096: }
097: }
098: return false;
099: }
100: };
101: }
102:
103: public TaskUtils getTaskUtils() {
104: return taskUtils;
105: }
106:
107: public TimeUtils getTimeUtils() {
108: return timeUtils;
109: }
110:
111: public AssetUtils getAssetUtils() {
112: return assetUtils;
113: }
114:
115: public ScheduleUtils getScheduleUtils() {
116: return scheduleUtils;
117: }
118:
119: public LoggingService getLoggingService(Object requestor) {
120: return (LoggingService) serviceBroker.getService(requestor,
121: LoggingService.class, null);
122: }
123:
124: public BlackboardQueryService getBlackboardQueryService(
125: Object requestor) {
126: return (BlackboardQueryService) serviceBroker.getService(
127: requestor, BlackboardQueryService.class, null);
128: }
129:
130: public void load() {
131: super .load();
132: serviceBroker = getBindingSite().getServiceBroker();
133: if (serviceBroker != null) {
134: logger = getLoggingService(this );
135: timeUtils = new TimeUtils(this );
136: assetUtils = new AssetUtils(this );
137: taskUtils = new TaskUtils(this );
138: scheduleUtils = new ScheduleUtils(this );
139: queryService = getBlackboardQueryService(this );
140: if (queryService == null) {
141: if (logger.isErrorEnabled()) {
142: logger.error("Unable to get query service");
143: }
144: }
145: }
146: }
147:
148: protected void setupSubscriptions() {
149: super .setupSubscriptions();
150: myOrganizations = (IncrementalSubscription) subscribe(orgsPredicate);
151: consumerSubscription = (IncrementalSubscription) subscribe(personPredicate());
152: if (didRehydrate()) {
153: rehydrate();
154: } // if
155: } // setupSubscriptions
156:
157: public Organization getMyOrg() {
158: return myOrg;
159: }
160:
161: protected void rehydrate() {
162: configure();
163: if (logger.isDebugEnabled()) {
164: logger.debug("Rehydrated - configured " + configured);
165: }
166: if (configured) {
167: // rehook handlers
168: Enumeration consumers = consumerSubscription.elements();
169: Asset asset, proto;
170: //boolean success;
171: Vector good_prototypes = new Vector();
172:
173: while (consumers.hasMoreElements()) {
174: asset = (Asset) consumers.nextElement();
175: if (asset instanceof AggregateAsset) {
176: proto = ((AggregateAsset) asset).getAsset();
177: } else {
178: proto = asset.getPrototype();
179: }
180: if (proto == null) {
181: if (logger.isErrorEnabled()) {
182: logger.error("no prototype for " + asset);
183: }
184: }
185: if ((proto != null)
186: && (!good_prototypes.contains(proto))) {
187: TypeIdentificationPG tip = asset
188: .getTypeIdentificationPG();
189: if (tip != null) {
190: String type_id = tip.getTypeIdentification();
191: if (type_id != null) {
192: getLDM().cachePrototype(type_id, proto);
193: good_prototypes.add(proto);
194: if (logger.isDebugEnabled()) {
195: logger.debug("Rehydrated asset "
196: + asset + " w/ proto " + proto);
197: }
198: } else {
199: if (logger.isErrorEnabled()) {
200: logger.error("cannot rehydrate "
201: + proto + " no typeId");
202: }
203: }
204: } else {
205: if (logger.isErrorEnabled()) {
206: logger.error("cannot rehydrate " + proto
207: + " no typeIdPG");
208: }
209: }
210: }
211: } // end while loop
212: addConsumerPGs(consumerSubscription);
213: }
214: }
215:
216: public void execute() {
217: if (!configured) {
218: configure();
219: }
220:
221: if (!consumerSubscription.isEmpty()) {
222: // System.out.println("Calling addConsumerPGs() for "+getAgentIdentifier());
223: addConsumerPGs(consumerSubscription);
224: }
225: }
226:
227: protected void configure() {
228: Iterator new_orgs = queryService.query(orgsPredicate)
229: .iterator();
230: if (new_orgs.hasNext()) {
231: myOrg = (Organization) new_orgs.next();
232: Service srvc = myOrg.getOrganizationPG().getService();
233: if (srvc != null) {
234: service = srvc.toString();
235: configured = true;
236: }
237: }
238: }
239:
240: // Don't want to do this. I am not creating prototypes
241: public boolean canHandle(String typeid, Class class_hint) {
242: return false;
243: }
244:
245: // I don't think this will ever be called since my can handle says no
246: // let's return null just in case -- llg
247: public Asset makePrototype(String type_name, Class class_hint) {
248: return null;
249: }
250:
251: public Asset getPrototype(String typeid) {
252: return getLDM().getFactory().getPrototype(typeid);
253: }
254:
255: public Collection generateRationList() {
256: List list = new ArrayList();
257: String query = (String) fileParameters_
258: .get("Class1ConsumedList");
259: if (query == null) { // if query not found, return null
260: if (logger.isDebugEnabled()) {
261: logger.debug("generaterationList(), query is null");
262: }
263: return null;
264: }
265: Vector holdsQueryResult;
266: try {
267: holdsQueryResult = executeQuery(query);
268: if (holdsQueryResult.isEmpty()) {
269: return null;
270: }
271: } catch (Exception ee) {
272: if (logger.isDebugEnabled()) {
273: String str = " DB query failed. query= " + query
274: + "\n ERROR " + ee.toString();
275: logger.debug(" getSupplementalList()," + str);
276: }
277: return null;
278: }
279: String typeIDPrefix = "NSN/";
280: PlanningFactory ldm = getLDM().getFactory();
281: for (int i = 0; i < holdsQueryResult.size(); i++) {
282: Object[] row = ((Object[]) holdsQueryResult.elementAt(i));
283: Asset ration = ldm.getPrototype(typeIDPrefix
284: + (String) row[0]);
285: if (ration != null) {
286: list.add(ration);
287: } else {
288: if (logger.isErrorEnabled()) {
289: logger.error(" Asset prototype is null");
290: }
291: } // if
292: } // for
293: return list;
294: }
295:
296: protected void addConsumerPGs(Collection consumers) {
297: if (logger.isDebugEnabled()) {
298: logger.debug(getAgentIdentifier() + ".addConsumerPGs with "
299: + consumers.size() + " people");
300: }
301: Iterator people = consumers.iterator();
302: Asset a, anAsset;
303:
304: // Loop over all eaters, adding the PG as necessary
305: while (people.hasNext()) {
306: a = (Asset) people.next();
307: if (a instanceof AggregateAsset) {
308: anAsset = ((AggregateAsset) a).getAsset();
309: } else {
310: anAsset = a;
311: }
312: if (anAsset instanceof MilitaryPerson) {
313: if (logger.isDebugEnabled()) {
314: logger.debug(getAgentIdentifier()
315: + ".addConsumerPG for MilitaryPerson: "
316: + anAsset);
317: }
318:
319: NewSubsistenceConsumerPG foodpg = (NewSubsistenceConsumerPG) anAsset
320: .searchForPropertyGroup(SubsistenceConsumerPG.class);
321:
322: // If have not added the PG yet, add it
323: if (foodpg == null) {
324: if (logger.isDebugEnabled()) {
325: logger
326: .debug(getAgentIdentifier()
327: + ".addConsumerPG CREATING SubConsumerPG for "
328: + anAsset);
329: }
330: foodpg = (NewSubsistenceConsumerPG) getLDM()
331: .getFactory().createPropertyGroup(
332: SubsistenceConsumerPG.class);
333: foodpg.setMei(a);
334: foodpg.setService(service);
335: foodpg.setTheater(THEATER);
336: foodpg
337: .setSubsistenceConsumerBG(new SubsistenceConsumerBG(
338: foodpg));
339: foodpg.initialize(this );
340: anAsset.setPropertyGroup(foodpg);
341: publishChange(a);
342: } else if (didRehydrate()) {
343: if (logger.isDebugEnabled()) {
344: logger
345: .debug(getAgentIdentifier()
346: + ".addConsumerPG on rehydrate - reinitializing PG for "
347: + anAsset);
348: }
349: // Otherwise, if it is there, and we rehydrated, then all the slots are filled in, but
350: // we must re-initialize (so the BG has a parentPlugin reference, etc)
351: foodpg.initialize(this );
352: } // end of didRehydrate
353: } // end of check for MilitaryPerson
354: } // end of loop over eaters
355: }
356:
357: // Associating a property group to the person asset
358: public void fillProperties(Asset anAsset) {
359: }
360: }
|