001: //
002: /*
003: * <copyright>
004: *
005: * Copyright 1997-2004 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026: */
027:
028: package org.cougaar.glm.xml.parser;
029:
030: import java.util.Date;
031: import java.util.Vector;
032:
033: import org.cougaar.core.mts.MessageAddress;
034: import org.cougaar.glm.ldm.Constants;
035: import org.cougaar.glm.plugins.MaintainedItem;
036: import org.cougaar.lib.xml.parser.DateParser;
037: import org.cougaar.lib.xml.parser.DirectObjectParser;
038: import org.cougaar.lib.xml.parser.PreferencesParser;
039: import org.cougaar.lib.xml.parser.VerbParser;
040: import org.cougaar.planning.ldm.LDMServesPlugin;
041: import org.cougaar.planning.ldm.PlanningFactory;
042: import org.cougaar.planning.ldm.asset.Asset;
043: import org.cougaar.planning.ldm.asset.NewTypeIdentificationPG;
044: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
045: import org.cougaar.planning.ldm.plan.NewTask;
046: import org.cougaar.planning.ldm.plan.Preference;
047: import org.cougaar.planning.ldm.plan.Schedule;
048: import org.cougaar.planning.ldm.plan.Task;
049: import org.cougaar.planning.ldm.plan.Verb;
050: import org.cougaar.util.log.Logger;
051: import org.w3c.dom.Node;
052: import org.w3c.dom.NodeList;
053:
054: /**
055: * Copyright (c) 1999-2002 BBN Technologies
056: */
057: public class TaskParser {
058: private static String PREPO = "PREPO";
059:
060: public TaskParser(Logger log) {
061: logger = log;
062: verbParser = new VerbParser();
063: dateParser = new DateParser();
064: preferencesParser = new PreferencesParser(log);
065: directObjectParser = new DirectObjectParser(log);
066: locationParser = new LocationParser();
067: itineraryParser = new ItineraryParser();
068:
069: if (logger.isInfoEnabled())
070: logger.info("TaskParser created.");
071: }
072:
073: public Task getTask(LDMServesPlugin ldm,
074: MessageAddress clusterIdentifier, PlanningFactory ldmf,
075: Node node) {
076: NewTask task = null;
077:
078: if (node.getNodeName().equals("task")) {
079: NodeList nlist = node.getChildNodes();
080: int nlength = nlist.getLength();
081:
082: task = ldmf.newTask();
083: task.setPlan(ldmf.getRealityPlan());
084: task.setSource(clusterIdentifier);
085: task.setDestination(clusterIdentifier);
086:
087: Vector prep_phrases = new Vector();
088: for (int i = 0; i < nlength; i++) {
089: Node child = nlist.item(i);
090: String childname = child.getNodeName();
091:
092: if (child.getNodeType() == Node.TEXT_NODE) {
093: continue;
094: }
095: if (child.getNodeType() == Node.ELEMENT_NODE) {
096:
097: if (handleTag(task, prep_phrases, ldm, ldmf, child)) {
098: } else if (childname.equals("verb")) {
099: Verb verb = verbParser.getVerb(child);
100: task.setVerb(verb);
101: if (logger.isDebugEnabled())
102: logger.debug("TaskParser - on task "
103: + task.getUID() + " set verb to "
104: + verb);
105: } else if (childname.equals("directobject")) {
106: task.setDirectObject(directObjectParser
107: .getDirectObject(ldm, child));
108: if (logger.isDebugEnabled())
109: logger.debug("TaskParser - on task "
110: + task.getUID() + " set d.o. to "
111: + task.getDirectObject());
112: } else if (childname.equals("from")) {
113: NewPrepositionalPhrase newpp = ldmf
114: .newPrepositionalPhrase();
115: newpp
116: .setPreposition(Constants.Preposition.FROM);
117: newpp.setIndirectObject(locationParser
118: .getLocation(ldm, child));
119: prep_phrases.addElement(newpp);
120: } else if (childname.equals("to")) {
121: NewPrepositionalPhrase newpp = ldmf
122: .newPrepositionalPhrase();
123: newpp.setPreposition(Constants.Preposition.TO);
124: newpp.setIndirectObject(locationParser
125: .getLocation(ldm, child));
126: prep_phrases.addElement(newpp);
127: } else if (childname.equals("with")) {
128: NewPrepositionalPhrase newpp = ldmf
129: .newPrepositionalPhrase();
130: newpp
131: .setPreposition(Constants.Preposition.WITH);
132: newpp.setIndirectObject(getStuff(ldm, child));
133: prep_phrases.addElement(newpp);
134: } else if (childname.equals("using")) {
135: // same as with
136: NewPrepositionalPhrase newpp = ldmf
137: .newPrepositionalPhrase();
138: newpp
139: .setPreposition(Constants.Preposition.USING);
140: newpp.setIndirectObject(getStuff(ldm, child));
141: prep_phrases.addElement(newpp);
142: }
143: /*
144: else if(childname.equals("maintaining")){
145: // same as with
146: NewPrepositionalPhrase newpp = ldmf.newPrepositionalPhrase();
147: newpp.setPreposition(Constants.Preposition.MAINTAINING);
148: newpp.setIndirectObject(getMaintainingObject(ldm, child));
149: prep_phrases.addElement(newpp);
150: }
151: */
152: else if (childname.equals("for")) {
153: NewPrepositionalPhrase newpp = ldmf
154: .newPrepositionalPhrase();
155: newpp.setPreposition(Constants.Preposition.FOR);
156: String forunit = getTagContents(child);
157: newpp.setIndirectObject(forunit);
158: prep_phrases.addElement(newpp);
159: } else if (childname.equals("oftype")) {
160: NewPrepositionalPhrase newpp = ldmf
161: .newPrepositionalPhrase();
162: newpp
163: .setPreposition(Constants.Preposition.OFTYPE);
164: newpp.setIndirectObject(getStuff(ldm, child));
165: prep_phrases.addElement(newpp);
166: } else if (childname.toUpperCase().equals(PREPO)) {
167: NewPrepositionalPhrase newpp = ldmf
168: .newPrepositionalPhrase();
169: newpp.setPreposition(PREPO);
170: newpp.setIndirectObject(PREPO);
171: prep_phrases.addElement(newpp);
172: } else if (childname.equals("readyat")) {
173: NewPrepositionalPhrase newpp = ldmf
174: .newPrepositionalPhrase();
175: newpp
176: .setPreposition(Constants.Preposition.READYAT);
177: Date readyat = dateParser.getDate(child);
178: Schedule sched = ldmf.newSimpleSchedule(
179: readyat, readyat);
180: newpp.setIndirectObject(sched);
181: prep_phrases.addElement(newpp);
182: } else if (childname.equals("startdate")) {
183: Preference p = preferencesParser.getStartDate(
184: ldmf, child);
185: task.addPreference(p);
186: } else if (childname.equals("enddate")) {
187: Preference p = preferencesParser.getEndDate(
188: ldmf, child);
189: task.addPreference(p);
190: } else if (childname.equals("cost")) {
191: Preference p = preferencesParser.getCost(ldmf,
192: child);
193: task.addPreference(p);
194: } else if (childname.equals("quantity")) {
195: Preference p = preferencesParser.getQuantity(
196: ldmf, child);
197: task.addPreference(p);
198: } else if (childname.equals("ItineraryOf")) {
199: NewPrepositionalPhrase newpp = ldmf
200: .newPrepositionalPhrase();
201: newpp
202: .setPreposition(Constants.Preposition.ITINERARYOF);
203: newpp.setIndirectObject(itineraryParser
204: .getItinerary(ldm, child));
205: //task.setPrepositionalPhrase(newpp);
206: prep_phrases.addElement(newpp);
207: } else if (childname.equals("refill")) {
208: NewPrepositionalPhrase newpp = ldmf
209: .newPrepositionalPhrase();
210: newpp
211: .setPreposition(Constants.Preposition.REFILL);
212: prep_phrases.addElement(newpp);
213: } else if (childname.equals("RespondTo")) {
214: NewPrepositionalPhrase newpp = ldmf
215: .newPrepositionalPhrase();
216: newpp
217: .setPreposition(Constants.Preposition.RESPONDTO);
218: String AEForg = child.getFirstChild()
219: .getNodeValue();
220: newpp.setIndirectObject(AEForg);
221: prep_phrases.addElement(newpp);
222:
223: // LMC: 11/22/99 AEF now sets this as a String.......
224: // newpp.setPreposition(AirforceGrammar.RESPONDTO);
225: // Asset AFOrg = new AbstractAsset();
226: // try {
227: // NewTypeIdentificationPG typeId = (NewTypeIdentificationPG)AFOrg.getTypeIdentificationPG();
228: // typeId.setTypeIdentification(child.getFirstChild().getNodeValue());
229: // typeId.setNomenclature(child.getFirstChild().getNodeValue());
230: // } catch (Exception exc) {
231: // System.err.println("problem creating the AbstractAsset for RespondTo");
232: // }
233: // newpp.setIndirectObject(AFOrg);
234: // //task.setPrepositionalPhrase(newpp);
235: // prep_phrases.addElement(newpp);
236: } else if (childname.equals("FromTask")) {
237: NewPrepositionalPhrase newpp = ldmf
238: .newPrepositionalPhrase();
239: newpp
240: .setPreposition(Constants.Preposition.FROMTASK);
241: Asset AEF = ldmf.createInstance(task
242: .getDirectObject());
243: try {
244: NewTypeIdentificationPG typeId = (NewTypeIdentificationPG) AEF
245: .getTypeIdentificationPG();
246: typeId.setTypeIdentification(child
247: .getFirstChild().getNodeValue());
248: typeId.setNomenclature(child
249: .getFirstChild().getNodeValue());
250: } catch (Exception exc) {
251: logger
252: .error(
253: "problem creating the AbstractAsset for FromTask",
254: exc);
255: }
256: newpp.setIndirectObject(AEF);
257: prep_phrases.addElement(newpp);
258: } else {
259: if (logger.isDebugEnabled())
260: logger
261: .debug("TaskParser - found non-specific prep : creating marker prep for tag "
262: + childname);
263: NewPrepositionalPhrase newpp = ldmf
264: .newPrepositionalPhrase();
265: newpp.setPreposition(childname);
266: newpp.setIndirectObject(childname);
267: prep_phrases.addElement(newpp);
268: }
269: }
270: task.setPrepositionalPhrases(prep_phrases.elements());
271: }
272: }
273: return task;
274: }
275:
276: /**
277: * Does nothing by default - override to handle your own new tags
278: *
279: * @return false by default
280: */
281: protected boolean handleTag(NewTask task, Vector prep_phrases,
282: LDMServesPlugin ldm, PlanningFactory ldmf, Node child) {
283: return false;
284: }
285:
286: private Object getStuff(LDMServesPlugin ldm, Node node) {
287: Object object = null;
288:
289: NodeList nlist = node.getChildNodes();
290: int nlength = nlist.getLength();
291:
292: for (int i = 0; i < nlength; i++) {
293: Node child = nlist.item(i);
294: String childname = child.getNodeName();
295:
296: if (child.getNodeType() == Node.ELEMENT_NODE) {
297: if (childname.equals("asset")) {
298: // object = AssetParser.getAsset(ldm, child);
299: object = directObjectParser.getDirectObject(ldm,
300: node);
301: break;
302: }
303: } else if (child.getNodeType() == Node.TEXT_NODE) {
304: String data = child.getNodeValue().trim();
305: if (data.length() != 0) {
306: object = data;
307: break;
308: }
309: }
310: }
311:
312: return object;
313: }
314:
315: /*
316: private static Object getMaintainingObject(LDMServesPlugin ldm, Node node){
317: String type = null, typeID = null, itemID = "maintainedItem", nomen = null;
318:
319: NodeList nlist = node.getChildNodes();
320: int nlength = nlist.getLength();
321:
322: for(int i = 0; i < nlength; i++){
323: Node child = nlist.item(i);
324: String childname = child.getNodeName();
325: if(child.getNodeType() == Node.ELEMENT_NODE){
326: if(childname.equals("type"))
327: type = getTagContents (child);
328: else if (childname.equals("typeID"))
329: typeID = getTagContents (child);
330: else if (childname.equals("nomen"))
331: nomen = getTagContents (child);
332: else if (childname.equals("itemID"))
333: itemID = getTagContents (child);
334: }
335: }
336:
337: return new MaintainedItem (type, typeID, itemID, nomen);
338: }
339: */
340:
341: protected String getTagContents(Node node) {
342: Asset asset = null;
343: Node data = node.getFirstChild();
344: return data.getNodeValue();
345: }
346:
347: private Logger logger;
348: VerbParser verbParser;
349: DateParser dateParser;
350: PreferencesParser preferencesParser;
351: DirectObjectParser directObjectParser;
352: LocationParser locationParser;
353: ItineraryParser itineraryParser;
354: }
|