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;
027:
028: import org.cougaar.glm.ldm.Constants;
029: import org.cougaar.glm.ldm.asset.Inventory;
030: import org.cougaar.glm.ldm.asset.Organization;
031: import org.cougaar.glm.ldm.asset.PhysicalPG;
032: import org.cougaar.glm.ldm.asset.ScheduledContentPG;
033: import org.cougaar.glm.ldm.plan.GeolocLocation;
034: import org.cougaar.planning.ldm.asset.AggregateAsset;
035: import org.cougaar.planning.ldm.asset.Asset;
036: import org.cougaar.planning.ldm.asset.TypeIdentificationPG;
037: import org.cougaar.planning.ldm.plan.Allocation;
038: import org.cougaar.planning.ldm.plan.AspectType;
039: import org.cougaar.planning.ldm.plan.Expansion;
040: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
041: import org.cougaar.planning.ldm.plan.Task;
042: import org.cougaar.util.log.Logger;
043: import org.cougaar.util.log.Logging;
044:
045: import java.util.Date;
046: import java.util.Enumeration;
047: import java.util.Vector;
048:
049: /** Provide methods to supply 'keys' uniquely describing published objects. */
050: public class PublicationKey {
051: private static Logger logger = Logging
052: .getLogger(PublicationKey.class);
053:
054: protected static String getKey(Object obj) {
055: if (obj instanceof Expansion)
056: return getKey((Expansion) obj);
057: if (obj instanceof Allocation)
058: return getKey((Allocation) obj);
059: if (obj instanceof Asset)
060: return getKey((Asset) obj);
061: if (obj instanceof Task)
062: return getKey((Task) obj);
063: else
064: return null;
065: }
066:
067: // Creates 'useful' string for Assets, AggregateAssets,
068: // Organizations, and Inventory objects
069: protected static String getKey(Asset a) {
070: String key = "Asset: TYPE_ID:"
071: + a.getTypeIdentificationPG().getTypeIdentification();
072:
073: TypeIdentificationPG ti = a.getTypeIdentificationPG();
074: if (ti != null) {
075: key += " NOMEN:" + ti.getTypeIdentification();
076: }
077:
078: if (a instanceof AggregateAsset) {
079: key += "aggregate QTY ="
080: + ((AggregateAsset) a).getQuantity();
081: a = ((AggregateAsset) a).getAsset();
082: } else if (a instanceof Organization) {
083: key += " ORG:"
084: + ((Organization) a).getMessageAddress()
085: .getAddress();
086: } else {
087: ScheduledContentPG prop = (ScheduledContentPG) a
088: .searchForPropertyGroup(ScheduledContentPG.class);
089: if ((a instanceof Inventory) && (prop != null)) {
090: key += " INVENTORY ASSET " + getKey(prop.getAsset());
091: }
092: PhysicalPG phys_prop = (PhysicalPG) a
093: .searchForPropertyGroup(PhysicalPG.class);
094: if (phys_prop != null) {
095: if (phys_prop.getVolume() != null) {
096: key += " VOL:"
097: + phys_prop.getVolume().getCubicFeet();
098: }
099: if (phys_prop.getMass() != null) {
100: key += " MASS:" + phys_prop.getMass().getTons();
101: }
102: }
103: }
104: return key;
105: }
106:
107: protected static String getKey(Task t) {
108: String key = "Task: " + getTotalTaskKey(t);
109: return key;
110: }
111:
112: protected static String getTotalTaskKey(Task task) {
113: String key = getTaskKey(task);
114: double quantity = TaskUtils.getPreferenceBestValue(task,
115: AspectType.QUANTITY);
116: if (!Double.isNaN(quantity))
117: key += " QTY:" + quantity;
118: double end = TaskUtils.getPreferenceBestValue(task,
119: AspectType.END_TIME);
120: if (!Double.isNaN(end))
121: key += " END:" + TimeUtils.dateString(new Date((long) end));
122: return key;
123: }
124:
125: protected static String getKey(Allocation alloc) {
126: String key = "Allocation of Task: ";
127: key += getTaskKey(alloc.getTask());
128: key += " To: " + getKey(alloc.getAsset());
129: return key;
130: }
131:
132: // from sourcing
133: protected static String getKey(Expansion exp) {
134: String key = "Expansion: ";
135: key += getTotalTaskKey(exp.getTask());
136: if (exp.getWorkflow() == null) {
137: key += " UNEXPANDED";
138: } else {
139: key += " EXPANDED";
140: }
141: return key;
142: }
143:
144: public static String getTaskKey(Task task) {
145: String key = "VERB:" + task.getVerb().toString();
146: Asset direct_obj = task.getDirectObject();
147: if (direct_obj != null) {
148: key += " " + getKey(direct_obj);
149: }
150:
151: // This whole prepositional phrase section should be replaced by the following
152: // if I knew an easy way to sort objects...
153: Enumeration pps = task.getPrepositionalPhrases();
154: Enumeration sorted_phrases = sortByPrepostion(pps);
155: while (sorted_phrases.hasMoreElements()) {
156: PrepositionalPhrase pp = (PrepositionalPhrase) sorted_phrases
157: .nextElement();
158: String preposition = pp.getPreposition();
159: Object indirect_obj = pp.getIndirectObject();
160: String description = "" + indirect_obj;
161: if (pp.equals(Constants.Preposition.TO)
162: || pp.equals(Constants.Preposition.FROM)) {
163: if (indirect_obj instanceof GeolocLocation) {
164: description = ((GeolocLocation) indirect_obj)
165: .getGeolocCode();
166: } else {
167: if (logger.isDebugEnabled()) {
168: logger.debug(preposition + " on "
169: + taskDesc(task) + " geoloc:"
170: + indirect_obj);
171: }
172: }
173: } else if (pp.equals(Constants.Preposition.FOR)) {
174: if (indirect_obj instanceof Organization) {
175: description = ((Organization) indirect_obj)
176: .getItemIdentificationPG()
177: .getItemIdentification();
178: } else if (indirect_obj instanceof String) {
179: description = (String) indirect_obj;
180: } else {
181: printError("getTaskKey() FOR prep phrase not an org or string id. key:"
182: + key + " TASK:" + taskDesc(task));
183: }
184: } else if (pp.equals(Constants.Preposition.MAINTAINING)) {
185: if (indirect_obj instanceof MaintainedItem) {
186: description = ((MaintainedItem) indirect_obj)
187: .getTypeIdentification();
188: String itemId = ((MaintainedItem) indirect_obj)
189: .getItemIdentification();
190: if (itemId != null) {
191: description = description + itemId;
192: }
193: } else {
194: printError("getTaskKey() Maintaining prep phrase not a MaintainedItem:"
195: + key + " TASK:" + taskDesc(task));
196: }
197: }
198: key += " " + preposition + " " + description;
199: }
200:
201: try {
202: long time = TaskUtils.getStartTime(task);
203: key += " START:" + TimeUtils.dateString(new Date(time));
204: } catch (RuntimeException re) {
205: try {
206: long time = TaskUtils.getEndTime(task);
207: key += " END:" + TimeUtils.dateString(new Date(time));
208: } catch (RuntimeException re2) {
209: }
210: }
211: return key;
212: }
213:
214: // PrepositionalPhrase pp0 = task.getPrepositionalPhrase("DemandSpec");
215: // if (pp0 != null) {
216: // key += " DEMAND SPEC:"+pp0.getIndirectObject();
217: // }
218:
219: // PrepositionalPhrase pp = task.getPrepositionalPhrase(Constants.Preposition.FOR);
220: // Object ind_obj;
221: // if (pp != null) {
222: // ind_obj = pp.getIndirectObject();
223: // String parent_string;
224: // if (ind_obj instanceof Organization) {
225: // parent_string = ((Organization)ind_obj).getItemIdentificationPG().getItemIdentification();
226: // } else if (ind_obj instanceof java.lang.String) {
227: // parent_string = (String)ind_obj;
228: // } else {
229: // printError("getTaskKey() FOR prep phrase not an org or string id. key:"+key+" TASK:"+taskDesc(task));
230: // return key;
231: // }
232: // key += " FOR:"+parent_string;
233: // }
234:
235: // pp = task.getPrepositionalPhrase(Constants.Preposition.USINGSUPPLYSOURCE);
236: // if (pp != null) {
237: // ind_obj = pp.getIndirectObject();
238: // if (ind_obj instanceof String) {
239: // key += " USINGSUPPLYSOURCE:"+ind_obj;
240: // } else if (ind_obj instanceof Asset) {
241: // key += " USINGSUPPLYSOURCE: "+getKey(ind_obj);
242: // } else {
243: // printError("getTaskKey() USINGSUPPLYSOURCE -- not an inventory object or a string"+ind_obj);
244: // }
245: // }
246:
247: // // if maintaining
248: // pp = task.getPrepositionalPhrase(Constants.Preposition.MAINTAINING);
249: // if (pp != null) {
250: // ind_obj = pp.getIndirectObject();
251: // if (!(ind_obj instanceof Asset)) {
252: // printError("getTaskKey() Maintaining prep phrase not an asset:"+key+" TASK:"+taskDesc(task));
253: // return key;
254: // }
255: // key += " MAINTAINING:"+((Asset)ind_obj).getTypeIdentificationPG().getTypeIdentification() +
256: // "/UID:" + ((Asset)ind_obj).getUID();
257: // }
258:
259: // // if to
260: // pp = task.getPrepositionalPhrase(Constants.Preposition.TO);
261: // if (pp != null) {
262: // GeolocLocation geo = (GeolocLocation)pp.getIndirectObject();
263: // if (geo != null) {
264: // key += " TO:"+geo.getGeolocCode();
265: // }
266: // else
267: // GLMDebug.DEBUG("PublicationKey", null, "TO task: "+taskDesc(task)+" geoloc:"+geo, 1);
268: // }
269:
270: // // if from
271: // pp = task.getPrepositionalPhrase(Constants.Preposition.FROM);
272: // if (pp != null) {
273: // GeolocLocation geo = (GeolocLocation)pp.getIndirectObject();
274: // if (geo != null) {
275: // key += " FROM:"+geo.getGeolocCode();
276: // }
277: // else
278: // GLMDebug.ERROR("PublicationKey","FROM task: "+taskDesc(task)+" geoloc:"+geo);
279: // }
280:
281: // // if oftype
282: // pp = task.getPrepositionalPhrase(Constants.Preposition.OFTYPE);
283: // if (pp != null) {
284: // Object io = pp.getIndirectObject();
285: // if (io != null) {
286: // key += " OFTYPE:"+io;
287: // } else
288: // GLMDebug.ERROR("PublicationKey","OFTYPE task: "+key+" OFTYPE w/ no io");
289: // }
290:
291: // try to get a time factor
292:
293: // there should be a much better sort available
294: private static Enumeration sortByPrepostion(Enumeration pps) {
295: Vector preps = new Vector();
296: while (pps.hasMoreElements()) {
297: preps.add(pps.nextElement());
298: }
299: Vector sorted = new Vector();
300: while (!preps.isEmpty()) {
301: pps = preps.elements();
302: PrepositionalPhrase best = (PrepositionalPhrase) pps
303: .nextElement();
304: if (pps.hasMoreElements()) {
305: PrepositionalPhrase next = (PrepositionalPhrase) pps
306: .nextElement();
307: if (next.getPreposition().compareTo(
308: best.getPreposition()) < 0) {
309: best = next;
310: }
311: }
312: sorted.add(best);
313: preps.remove(best);
314: }
315: return sorted.elements();
316: }
317:
318: private static void printError(String msg) {
319: if (logger.isErrorEnabled()) {
320: logger.error("PublicationKey" + msg);
321: }
322: }
323:
324: // utility functions
325: public static String taskDesc(Task task) {
326: return task.getUID() + ": " + task.getVerb() + "("
327: + TaskUtils.getQuantity(task) + " "
328: + TaskUtils.getTaskItemName(task) + ") "
329: + new Date(TaskUtils.getStartTime(task));
330: }
331:
332: }
|