001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.lib.vishnu.client;
027:
028: import java.text.ParseException;
029: import java.text.SimpleDateFormat;
030:
031: import java.util.Collection;
032: import java.util.Date;
033: import java.util.ArrayList;
034: import java.util.List;
035: import java.util.Map;
036: import java.util.Vector;
037:
038: import org.cougaar.planning.ldm.asset.Asset;
039: import org.cougaar.planning.ldm.plan.Task;
040: import org.cougaar.lib.param.ParamMap;
041: import org.cougaar.util.StringKey;
042: import org.cougaar.util.log.Logger;
043:
044: import org.w3c.dom.Document;
045:
046: import org.xml.sax.helpers.DefaultHandler;
047: import org.xml.sax.Attributes;
048: import org.xml.sax.SAXException;
049:
050: /**
051: * Takes XML results returned from the scheduler and calls methods on the result listener
052: * to create plan elements
053: */
054: public class XMLResultHandler extends PluginHelper implements
055: ResultHandler {
056: public XMLResultHandler(ModeListener parent, VishnuComm comm,
057: XMLProcessor xmlProcessor, VishnuDomUtil domUtil,
058: VishnuConfig config, ParamMap myParamTable, Logger logger) {
059: super (parent, comm, xmlProcessor, domUtil, config,
060: myParamTable, logger);
061: resultListener = (ResultListener) parent;
062: }
063:
064: protected void localSetup() {
065: super .localSetup();
066:
067: try {
068: debugParseAnswer = getMyParams().getBooleanParam(
069: "debugParseAnswer");
070: } catch (Exception e) {
071: debugParseAnswer = false;
072: }
073: }
074:
075: /**
076: * <pre>
077: * Reads XML from the URL to get the assignments. Uses AssignmentHandler
078: * (SAX) to parse XML.
079: *
080: * Uses myTaskUIDtoObject to figure out if there were any impossible tasks.
081: * (If there were any, they will be in the myTaskUIDtoObject Map.)
082: *
083: * The AssignmentHandler removes any assigned tasks from myTaskUIDtoObject.
084: *
085: * </pre>
086: */
087: protected void parseAnswer() {
088: if (logger.isInfoEnabled())
089: logger
090: .info(parent.getName()
091: + ".waitTillFinished - Vishnu scheduler result returned!");
092: int unhandledTasks = parent.getNumTasks();
093:
094: comm.getAnswer(new AssignmentHandler());
095:
096: if (logger.isInfoEnabled())
097: logger
098: .info(parent.getName()
099: + ".parseAnswer - created successful plan elements for "
100: + (unhandledTasks - parent.getNumTasks())
101: + " tasks.");
102: }
103:
104: public DefaultHandler getAssignmentHandler() {
105: return new AssignmentHandler();
106: }
107:
108: /**
109: * this is where we look up unique ids
110: */
111: public class AssignmentHandler extends DefaultHandler {
112: /**
113: * just calls parseStartElement in plugin
114: */
115: public void startElement(String uri, String local, String name,
116: Attributes atts) throws SAXException {
117: parseStartElement(name, atts);
118: }
119:
120: /**
121: * just calls parseEndElement in plugin
122: */
123: public void endElement(String uri, String local, String name)
124: throws SAXException {
125: parseEndElement(name);
126: }
127: }
128:
129: protected Asset assignedAsset = null;
130: protected Date start, end, setup, wrapup;
131: String multicontribs;
132: String multitext;
133: protected Vector alpTasks = new Vector();
134:
135: /**
136: * Given the XML that indicates assignments, parse it
137: *
138: * Uses the <code>myTaskUIDtoObject</code> and <code>myAssetUIDtoObject</code> maps
139: * to lookup the keys returned in the xml to figure out which task was assigned to
140: * which asset. These were set in processTasks using setUIDToObjectMap.
141: *
142: * @param name the tag name
143: * @param atts the tag's attributes
144: * @see org.cougaar.lib.vishnu.client.VishnuPlugin#processTasks
145: * @see org.cougaar.lib.vishnu.client.VishnuPlugin#setUIDToObjectMap
146: */
147: protected void parseStartElement(String name, Attributes atts) {
148: try {
149: if (logger.isDebugEnabled())
150: logger.debug(parent.getName()
151: + ".parseStartElement got " + name);
152:
153: if (name.equals("ASSIGNMENT")) {
154: if (logger.isInfoEnabled()) {
155: logger
156: .info(parent.getName()
157: + ".parseStartElement -\nAssignment: task = "
158: + atts.getValue("task")
159: + " resource = "
160: + atts.getValue("resource")
161: + " start = "
162: + atts.getValue("start")
163: + " end = " + atts.getValue("end"));
164: }
165: String taskUID = atts.getValue("task");
166: String resourceUID = atts.getValue("resource");
167: String startTime = atts.getValue("start");
168: String endTime = atts.getValue("end");
169: String setupTime = atts.getValue("setup");
170: String wrapupTime = atts.getValue("wrapup");
171: String contribs = atts.getValue("contribs");
172: Date start = format.parse(startTime);
173: Date end = format.parse(endTime);
174: Date setup = format.parse(setupTime);
175: Date wrapup = format.parse(wrapupTime);
176:
177: StringKey taskKey = new StringKey(taskUID);
178: Task handledTask = resultListener
179: .getTaskForKey(taskKey);
180: if (handledTask == null) {
181: // Ignore this assignment since it has already been handled previously
182: return;
183:
184: // logger.debug ("VishnuPlugin - AssignmentHandler.startElement no task found with " + taskUID);
185: // logger.debug ("\tmap was " + myTaskUIDtoObject);
186: } else {
187: resultListener.removeTask(taskKey);
188: }
189:
190: Asset assignedAsset = resultListener
191: .getAssetForKey(new StringKey(resourceUID));
192: if (assignedAsset == null)
193: logger
194: .debug("VishnuPlugin - AssignmentHandler.startElement no asset found with "
195: + resourceUID);
196:
197: resultListener.handleAssignment(handledTask,
198: assignedAsset, start, end, setup, wrapup,
199: contribs, atts.getValue("text"));
200: } else if (name.equals("MULTITASK")) {
201: if (logger.isInfoEnabled() || debugParseAnswer) {
202: logger.info(getName()
203: + ".parseStartElement -\nAssignment: "
204: + " resource = "
205: + atts.getValue("resource") + " start = "
206: + atts.getValue("start") + " end = "
207: + atts.getValue("end") + " setup = "
208: + atts.getValue("setup") + " wrapup = "
209: + atts.getValue("wrapup"));
210: }
211: String resourceUID = atts.getValue("resource");
212: String startTime = atts.getValue("start");
213: String endTime = atts.getValue("end");
214: String setupTime = atts.getValue("setup");
215: String wrapupTime = atts.getValue("wrapup");
216: multicontribs = atts.getValue("contribs");
217: start = format.parse(startTime);
218: end = format.parse(endTime);
219: setup = format.parse(setupTime);
220: wrapup = format.parse(wrapupTime);
221:
222: multitext = atts.getValue("text");
223:
224: assignedAsset = resultListener
225: .getAssetForKey(new StringKey(resourceUID));
226: if (assignedAsset == null)
227: logger
228: .debug(getName()
229: + ".parseStartElement - no asset found with "
230: + resourceUID);
231: } else if (name.equals("TASK")) {
232: if (logger.isInfoEnabled() || debugParseAnswer) {
233: logger.info(getName()
234: + ".parseStartElement -\nTask: "
235: + " task = " + atts.getValue("task"));
236: }
237: String taskUID = atts.getValue("task");
238:
239: StringKey taskKey = new StringKey(taskUID);
240: Task handledTask = resultListener
241: .getTaskForKey(taskKey);
242: if (handledTask == null) {
243: // Ignore since task has already been handled
244: return;
245:
246: // debug (getName () + ".parseStartElement - no task found with " + taskUID + " uid.");
247: } else
248: alpTasks.add(handledTask);
249:
250: // this is absolutely critical, otherwise VishnuPlugin will make a failed disposition
251: resultListener.removeTask(taskKey);
252: }
253: // else if (logger.isDebugEnabled()) {
254: // debug (getName () + ".parseStartElement - ignoring tag " + name);
255: // }
256: } catch (NullPointerException npe) {
257: npe.printStackTrace();
258: logger.error(getName()
259: + ".parseStartElement - got bogus assignment", npe);
260: } catch (ParseException pe) {
261: logger
262: .error(getName()
263: + ".parseStartElement - start or end time is in bad format "
264: + pe + "\ndates were : " + " start = "
265: + atts.getValue("start") + " end = "
266: + atts.getValue("end") + " setup = "
267: + atts.getValue("setup") + " wrapup = "
268: + atts.getValue("wrapup"));
269: }
270: }
271:
272: protected void parseEndElement(String name) {
273: if (name.equals("MULTITASK")) {
274: if (debugParseAnswer) {
275: logger.debug(getName()
276: + ".parseEndElement - got ending MULTITASK.");
277: }
278: for (int i = 0; i < alpTasks.size(); i++)
279: resultListener.handleAssignment((Task) alpTasks.get(i),
280: assignedAsset, start, end, setup, wrapup,
281: multicontribs, multitext);
282: alpTasks.clear();
283: } else if (name.equals("TASK")) {
284: } else if (debugParseAnswer) {
285: logger.debug(getName() + ".parseEndElement - ignoring tag "
286: + name);
287: }
288: }
289:
290: public void setNameToDescrip(Map map) {
291: myNameToDescrip = map;
292: }
293:
294: public void setSingleAssetClassName(String name) {
295: singleAssetClassName = name;
296: }
297:
298: protected String getName() {
299: return "XMLResultHandler";
300: }
301:
302: Map myNameToDescrip;
303: String singleAssetClassName;
304: protected boolean debugParseAnswer = false;
305: ResultListener resultListener;
306:
307: private final SimpleDateFormat format = new SimpleDateFormat(
308: "yyyy-MM-dd HH:mm:ss");
309: }
|