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.lib.util;
028:
029: import java.util.ArrayList;
030: import java.util.Collections;
031: import java.util.Enumeration;
032: import java.util.HashMap;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Vector;
036:
037: import org.cougaar.planning.ldm.PlanningFactory;
038: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
039: import org.cougaar.planning.ldm.plan.NewTask;
040: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
041: import org.cougaar.planning.ldm.plan.Task;
042:
043: import org.cougaar.util.LRUCache;
044:
045: import org.cougaar.util.log.Logger;
046:
047: /**
048: * This class contains utility functions for creating
049: * and accessing PrepositionalPhrases.
050: */
051:
052: public class UTILPrepPhrase {
053: private static String myName = "UTILPrepPhrase";
054: protected Logger logger;
055:
056: // 0 = LRU
057: // 1 = hashmap
058: private static final int CACHE_STYLE_DEFAULT = 0;
059: private static final int CACHE_STYLE = Integer.getInteger(
060: "org.cougaar.lib.util.UTILPreference.cacheStyle",
061: CACHE_STYLE_DEFAULT).intValue();
062:
063: private static final int CACHE_SIZE_DEFAULT = 256;
064: private static final int CACHE_SIZE = Integer.getInteger(
065: "org.cougaar.lib.util.UTILPreference.cacheSize",
066: CACHE_SIZE_DEFAULT).intValue();
067:
068: /** maps prepositions to map of indirect objects to prep phrases */
069: protected Map prepToIndirectMap;
070:
071: public UTILPrepPhrase(Logger log) {
072: logger = log;
073:
074: if (CACHE_STYLE == 1) {
075: prepToIndirectMap = new HashMap(CACHE_SIZE);
076: } else {
077: prepToIndirectMap = new LRUCache(CACHE_SIZE);
078: }
079: }
080:
081: /**
082: * Utility method for extracting the indirect object from a
083: * prepositional phrase.
084: * @param t the task
085: * @param prepName the Preposition in String
086: * @return Object
087: */
088: public Object getIndirectObject(Task t, String prepName) {
089: Enumeration prepphrases = t.getPrepositionalPhrases();
090: PrepositionalPhrase pp = getPrepNamed(prepphrases, prepName);
091: if (pp == null)
092: throw new UTILRuntimeException("\nAsking for prep named <"
093: + prepName + "> which is not in task :\n" + t);
094:
095: return pp.getIndirectObject();
096: }
097:
098: /**
099: * @return true if Task <code>t</code> has <code>prepName</code> on list of
100: * its prepphrases.
101: */
102: public boolean hasPrepNamed(Task t, String prepName) {
103: Enumeration prepphrases = t.getPrepositionalPhrases();
104: return (getPrepNamed(prepphrases, prepName) != null);
105: }
106:
107: /**
108: * Utility method extracting a PrepositionalPhrase from a
109: * task
110: * @param t task with preps
111: * @param prepName the String name of the Preposition
112: * @return the preposition named prepName
113: */
114: public PrepositionalPhrase getPrepNamed(Task t, String prepName) {
115: return getPrepNamed(t.getPrepositionalPhrases(), prepName);
116: }
117:
118: /**
119: * Utility method extracting a PrepositionalPhrase from an
120: * Enumeration of PrepositionalPhrases.
121: * @param prepphrases an Enum of PrepositionalPhrases
122: * @param prepName the String name of the Preposition
123: * @return the preposition named prepName
124: */
125: public PrepositionalPhrase getPrepNamed(Enumeration prepphrases,
126: String prepName) {
127: while (prepphrases.hasMoreElements()) {
128: PrepositionalPhrase pp = (PrepositionalPhrase) prepphrases
129: .nextElement();
130: if (pp.getPreposition().equals(prepName))
131: return pp;
132: }
133: return null;
134: }
135:
136: /**
137: * Add a prepositionalPhrase to a task with replacement.
138: * Changes list of phrases on task.
139: * Does NOT require the existence of the prep on the task already, i.e. if
140: * can't replace, just adds.
141: *
142: */
143: public void replacePrepOnTask(Task taskToAddTo,
144: PrepositionalPhrase prepPhrase) {
145: if (hasPrepNamed(taskToAddTo, prepPhrase.getPreposition()))
146: removePrepNamed(taskToAddTo, prepPhrase.getPreposition());
147:
148: addPrepToTask(taskToAddTo, prepPhrase);
149: }
150:
151: /**
152: * Add a prepositionalPhrase to a task. Changes list of phrases on task.
153: *
154: * @param taskToAddTo task to add prep phrase to
155: * @param prepPhrase to add to list of prep phrases on task
156: */
157: public void addPrepToTask(Task taskToAddTo,
158: PrepositionalPhrase prepPhrase) {
159: // Save all of the old prep phrases of the task
160: List newPPs = getPrepListPlusPhrase(taskToAddTo, prepPhrase);
161: ((NewTask) taskToAddTo).setPrepositionalPhrases(Collections
162: .enumeration(newPPs));
163: }
164:
165: /**
166: * Adds prepositionalPhrases to a task. Changes list of phrases on task.
167: * Risky to use because it does not check whether the added Preps already exist.
168: *
169: * @param taskToAddTo task to add prep phrase to
170: * @param preps Vector of PrepositionalPhrases to add to list of prep phrases on task
171: */
172: public void addPreps(Task taskToAddTo, Vector preps) {
173: List newPPs = UTILAllocate.enumToList(taskToAddTo
174: .getPrepositionalPhrases());
175: for (int i = 0; i < preps.size(); i++)
176: newPPs.add(preps.elementAt(i));
177: ((NewTask) taskToAddTo).setPrepositionalPhrases(Collections
178: .enumeration(newPPs));
179: }
180:
181: /**
182: * Return list of phrases on task plus added one.
183: *
184: * Does NOT change original task.
185: *
186: * @param task to get prep phrases from
187: * @param prepPhrase to add to list of prep phrases
188: * @return List with given prep phrase added
189: */
190: public List getPrepListPlusPhrase(Task task,
191: PrepositionalPhrase prepPhrase) {
192: // Save all of the old prep phrases of the task
193: List newPPs = UTILAllocate.enumToList(task
194: .getPrepositionalPhrases());
195: // Then add the new one
196: newPPs.add(prepPhrase);
197: return newPPs;
198: }
199:
200: /**
201: * Removes a PrepositionalPhrase from a task's
202: * set of PrepositionalPhrases.
203: *
204: * @param taskToChange task to remove the prep from
205: * @param prepName the String name of the Preposition to remove
206: * @return the PrepositionalPhrase named prepName that was removed
207: */
208: public PrepositionalPhrase removePrepNamed(Task taskToChange,
209: String prepName) {
210: // Save all of the old prep phrases of the task
211: List newPrepPhrases = new ArrayList();
212: PrepositionalPhrase returnedPrep = null;
213:
214: Enumeration prepphrases = taskToChange
215: .getPrepositionalPhrases();
216: while (prepphrases.hasMoreElements()) {
217: PrepositionalPhrase pp = (PrepositionalPhrase) prepphrases
218: .nextElement();
219: if (pp.getPreposition().equals(prepName))
220: returnedPrep = pp;
221: else
222: newPrepPhrases.add(pp);
223: }
224:
225: ((NewTask) taskToChange).setPrepositionalPhrases(Collections
226: .enumeration(newPrepPhrases));
227:
228: return returnedPrep;
229: }
230:
231: /**
232: * Removes a series of PrepositionalPhrases from a task's
233: * set of PrepositionalPhrases.
234: *
235: * @param taskToChange task to remove the prep from
236: * @param preps a Vector of String names of Prepositions to remove
237: */
238: public void removePreps(Task taskToChange, Vector preps) {
239: // Save all of the old prep phrases of the task
240: List newPrepPhrases = new ArrayList();
241:
242: Enumeration prepphrases = taskToChange
243: .getPrepositionalPhrases();
244: while (prepphrases.hasMoreElements()) {
245: PrepositionalPhrase pp = (PrepositionalPhrase) prepphrases
246: .nextElement();
247: if (!preps.contains(pp.getPreposition()))
248: newPrepPhrases.add(pp);
249: }
250:
251: ((NewTask) taskToChange).setPrepositionalPhrases(Collections
252: .enumeration(newPrepPhrases));
253: }
254:
255: /**
256: * Utility methods for creating a PrepositionalPhrases
257: *
258: * Uses a cache - only makes unique prepositional phrases
259: *
260: * Uses the prepToIndirectMap cache so only distinct instances are created.
261: *
262: * @param ldmf the PlanningFactory
263: * @param prep the Preposition
264: * @param indirectObject the indirect object - any generic Object
265: * @return the new PrepositionalPhrase
266: */
267: public PrepositionalPhrase makePrepositionalPhrase(
268: PlanningFactory ldmf, String prep, Object indirectObject) {
269: Map indirectToPhrase = (Map) prepToIndirectMap.get(prep);
270: if (indirectToPhrase == null) {
271: if (CACHE_STYLE == 1) {
272: indirectToPhrase = new HashMap(CACHE_SIZE);
273: } else {
274: indirectToPhrase = new LRUCache(CACHE_SIZE);
275: }
276:
277: prepToIndirectMap.put(prep, indirectToPhrase);
278: }
279:
280: PrepositionalPhrase phrase = (PrepositionalPhrase) indirectToPhrase
281: .get(indirectObject);
282:
283: if (phrase == null) {
284: NewPrepositionalPhrase npp = ldmf.newPrepositionalPhrase();
285: npp.setPreposition(prep);
286: npp.setIndirectObject(indirectObject);
287: indirectToPhrase.put(indirectObject, npp);
288: phrase = npp;
289: }
290:
291: return phrase;
292: }
293: }
|