001: /*--------------------------------------------------------------------------
002: * <copyright>
003: *
004: * Copyright 1999-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.glm.plugins.projection;
027:
028: import org.cougaar.core.mts.MessageAddress;
029: import org.cougaar.glm.ldm.Constants;
030: import org.cougaar.glm.ldm.asset.Organization;
031: import org.cougaar.glm.ldm.plan.GeolocLocation;
032: import org.cougaar.glm.plugins.AssetUtils;
033: import org.cougaar.glm.plugins.GLMDecorationPlugin;
034: import org.cougaar.glm.plugins.MaintainedItem;
035: import org.cougaar.glm.plugins.TaskUtils;
036: import org.cougaar.planning.ldm.asset.Asset;
037: import org.cougaar.planning.ldm.asset.ItemIdentificationPG;
038: import org.cougaar.planning.ldm.asset.TypeIdentificationPG;
039: import org.cougaar.planning.ldm.measure.Rate;
040: import org.cougaar.planning.ldm.plan.AspectType;
041: import org.cougaar.planning.ldm.plan.AspectValue;
042: import org.cougaar.planning.ldm.plan.NewTask;
043: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
044: import org.cougaar.planning.ldm.plan.ScoringFunction;
045: import org.cougaar.planning.ldm.plan.Task;
046: import org.cougaar.util.UnaryPredicate;
047: import org.cougaar.util.log.Logger;
048: import org.cougaar.util.log.Logging;
049:
050: import java.util.Date;
051: import java.util.Enumeration;
052: import java.util.Vector;
053:
054: /** Specifies how to create SUPPLY demand tasks for demand projection.
055: **/
056: public class GenerateSupplyDemandExpander extends
057: GenerateDemandExpander {
058: private static Logger logger = Logging
059: .getLogger(GenerateSupplyDemandExpander.class);
060:
061: /**
062: * @see ProjectionTasksPredicate
063: **/
064: public GenerateSupplyDemandExpander(GLMDecorationPlugin pi,
065: Organization org, Vector types) {
066: super (pi, org, types, new ProjectionTasksPredicate(pi, org,
067: types));
068: }
069:
070: /** Tasks that published by this processor **/
071: public static class ProjectionTasksPredicate implements
072: UnaryPredicate {
073: MessageAddress clusterId_;
074: String myOrgName_;
075: Vector resourceTypes_;
076:
077: public ProjectionTasksPredicate(GLMDecorationPlugin pi,
078: Organization org, Vector types) {
079: myOrgName_ = org.getItemIdentificationPG()
080: .getItemIdentification();
081: clusterId_ = pi.getMyDelegate().getMessageAddress();
082: resourceTypes_ = types;
083: }
084:
085: /** Looking for GenerateSupply tasks created in this cluster,
086: * with the correct type of DirectObject,
087: * that have a 'USINGSUPPLYSOURCE' for this organization. **/
088: public boolean execute(Object o) {
089: if (o instanceof Task) {
090: Task task = (Task) o;
091: if (task.getVerb().equals(Constants.Verb.SUPPLY)
092: || task.getVerb().equals(
093: Constants.Verb.PROJECTSUPPLY)) {
094: if (task.getParentTaskUID() != null) {
095: if (task.getSource().equals(clusterId_)) {
096: if (!TaskUtils.isMyRefillTask(task,
097: myOrgName_)) {
098: // OFTYPE check
099: PrepositionalPhrase pp = task
100: .getPrepositionalPhrase(Constants.Preposition.OFTYPE);
101: if (pp != null) {
102: Object obj = pp.getIndirectObject();
103: if (obj instanceof String) {
104: String type = (String) obj;
105: Enumeration en = resourceTypes_
106: .elements();
107: while (en.hasMoreElements()) {
108: if (type.equals((String) en
109: .nextElement())) {
110: return true;
111: }
112: }
113: } else {
114: if (logger.isErrorEnabled()) {
115: logger
116: .error("ProjectionTasksPredicate unrec OFTYPE :"
117: + obj);
118: }
119: }
120: }
121: }
122: }
123: }
124: }
125: }
126: return false;
127: }
128: }
129:
130: /** Create SUPPLY Task w/ prep phrases
131: * @param direct_object requested item (consumed part)
132: * @param end end time
133: * @param qty quantity of item requested
134: * @return supply task for given asset.
135: */
136: protected Task newDemandTask(Task parent_task, Asset direct_object,
137: Object consumer, long end, double qty) {
138: /* create prepositions for the new Supply task */
139: Vector pp_vector = demandTaskPrepPhrases(consumer,
140: direct_object, end, parent_task);
141: if (pp_vector == null)
142: return null; // Don't need a task
143:
144: Vector prefs = createSupplyPreferences(end, qty);
145:
146: NewTask t = (NewTask) buildTask(parent_task,
147: Constants.Verb.SUPPLY, direct_object, pp_vector, prefs
148: .elements());
149: //t.setCommitmentDate(end);
150: t.setCommitmentDate(new Date(end));
151: return t;
152: }
153:
154: protected Vector createSupplyPreferences(long end, double qty) {
155: Vector prefs = new Vector();
156: prefs.addElement(createDateAfterPreference(
157: AspectType.START_TIME, end - 3 * MSEC_PER_DAY));
158: prefs.addElement(createDateBeforePreference(
159: AspectType.END_TIME, end));
160: prefs.addElement(createQuantityPreference(AspectType.QUANTITY,
161: qty));
162: return prefs;
163: }
164:
165: /** Create PROJECTSUPPLY Task w/ prep phrases
166: * @param parent_task
167: * @param direct_object requested item (consumed part)
168: * @param consumer
169: * @param start start time
170: * @param end end time
171: * @param rate of consumption for item requested
172: * @return supply task for given asset.
173: */
174: protected Task newProjectionTask(Task parent_task,
175: Asset direct_object, Object consumer, long start, long end,
176: Rate rate, double multiplier) {
177: /* create prepositions for the new Supply task */
178: Vector pp_vector = demandTaskPrepPhrases(consumer,
179: direct_object, end, parent_task);
180: if (pp_vector == null)
181: return null; // Don't need a task
182:
183: Vector prefs = createProjectionPreferences(start, end, rate,
184: multiplier);
185:
186: NewTask t = (NewTask) buildTask(parent_task,
187: Constants.Verb.PROJECTSUPPLY, direct_object, pp_vector,
188: prefs.elements());
189: //t.setCommitmentDate(end);
190: t.setCommitmentDate(new Date(end));
191: return t;
192: }
193:
194: protected Vector createProjectionPreferences(long start, long end,
195: Rate rate, double mult) {
196: ScoringFunction score;
197: Vector prefs = new Vector();
198:
199: score = ScoringFunction.createStrictlyAtValue(AspectValue
200: .newAspectValue(AspectType.START_TIME, start));
201: prefs.addElement(ldmFactory_.newPreference(
202: AspectType.START_TIME, score));
203:
204: score = ScoringFunction.createStrictlyAtValue(AspectValue
205: .newAspectValue(AspectType.END_TIME, end));
206: prefs.addElement(ldmFactory_.newPreference(AspectType.END_TIME,
207: score));
208:
209: prefs.addElement(TaskUtils.createDemandRatePreference(
210: ldmFactory_, rate));
211: prefs.addElement(TaskUtils.createDemandMultiplierPreference(
212: ldmFactory_, mult));
213: return prefs;
214: }
215:
216: /** Create FOR, TO, MAINTAIN, and OFTYPE prepositional phrases
217: * for use by the subclasses.
218: * @param consumer the consumer the task support
219: * @param time - used to find the OPlan and the geoloc for the TO preposition
220: * @return Vector of PrepostionalPhrases
221: * @see #createDemandTasks
222: **/
223: protected Vector demandTaskPrepPhrases(Object consumer,
224: Asset resource, long time, Task parent_task) {
225: /* create prepositions for the new demand task */
226: Vector pp_vector = new Vector();
227: PrepositionalPhrase pp = parent_task
228: .getPrepositionalPhrase(Constants.Preposition.OFTYPE);
229: if (pp != null) {
230: Object obj = pp.getIndirectObject();
231: if (obj instanceof String) {
232: pp_vector.addElement(newPrepositionalPhrase(
233: Constants.Preposition.OFTYPE, (String) obj));
234: }
235: }
236:
237: pp_vector.addElement(newPrepositionalPhrase(
238: Constants.Preposition.FOR, myOrgName_));
239:
240: // when oplan id added to tasks....
241: // pp_vector.addElement(newPrepositionalPhrase(Constants.Preposition.FOROPLAN, oplan));
242:
243: GeolocLocation geoloc = getGeolocLocation(parent_task, time);
244: if (geoloc != null) {
245: pp_vector.addElement(newPrepositionalPhrase(
246: Constants.Preposition.TO, geoloc));
247: // printDebug("demandTaskPrepPhrases(), GeolocLocation is "+geoloc+" for "+TaskUtils.shortTaskDesc(parent_task));
248: } else { // Try to use HomeLocation
249: try {
250: // printDebug("demandTaskPrepPhrases(), Using HomeLocation for transport");
251: geoloc = (GeolocLocation) myOrganization_
252: .getMilitaryOrgPG().getHomeLocation();
253: pp_vector.addElement(newPrepositionalPhrase(
254: Constants.Preposition.TO, geoloc));
255: } catch (NullPointerException npe) {
256: if (logger.isErrorEnabled()) {
257: logger
258: .error("demandTaskPrepPhrases(), Unable to find Location for Transport");
259: }
260: }
261: }
262:
263: if (consumer != null) {
264: MaintainedItem itemID;
265: if (consumer instanceof Asset) {
266: TypeIdentificationPG tip = ((Asset) consumer)
267: .getTypeIdentificationPG();
268: ItemIdentificationPG iip = ((Asset) consumer)
269: .getItemIdentificationPG();
270: if (iip != null) {
271: itemID = MaintainedItem.findOrMakeMaintainedItem(
272: "Asset", tip.getTypeIdentification(), iip
273: .getItemIdentification(), tip
274: .getNomenclature());
275: } else {
276: itemID = MaintainedItem.findOrMakeMaintainedItem(
277: "Asset", tip.getTypeIdentification(), null,
278: tip.getNomenclature());
279: }
280: } else {
281: itemID = MaintainedItem.findOrMakeMaintainedItem(
282: "Other", consumer.toString(), null, null);
283: }
284: pp_vector.addElement(newPrepositionalPhrase(
285: Constants.Preposition.MAINTAINING, itemID));
286: }
287:
288: return pp_vector;
289: }
290:
291: protected GeolocLocation getGeolocLocation(Task parent_task,
292: long time) {
293: Enumeration geolocs = AssetUtils.getGeolocLocationAtTime(
294: myOrganization_, time);
295: if (geolocs.hasMoreElements()) {
296: GeolocLocation geoloc = (GeolocLocation) geolocs
297: .nextElement();
298: // GLMDebug.DEBUG("GenerateSupplyDemandExpander", clusterId_, "At "+TimeUtils.dateString(time)+ " the geoloc is "+geoloc);
299: return geoloc;
300: }
301: return null;
302: }
303: }
|