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.logistics.servlet;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import java.util.*;
032:
033: import javax.servlet.Servlet;
034: import javax.servlet.http.HttpServlet;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import org.cougaar.core.mts.MessageAddress;
039: import org.cougaar.core.blackboard.BlackboardClient;
040: import org.cougaar.core.service.AgentIdentificationService;
041: import org.cougaar.core.service.BlackboardService;
042: import org.cougaar.core.servlet.BaseServletComponent;
043: import org.cougaar.core.util.UID;
044: import org.cougaar.util.UnaryPredicate;
045:
046: import org.cougaar.planning.ldm.plan.Task;
047: import org.cougaar.planning.ldm.plan.AllocationResult;
048: import org.cougaar.planning.ldm.plan.PlanElement;
049: import org.cougaar.planning.ldm.plan.AspectValue;
050: import org.cougaar.planning.ldm.plan.Verb;
051:
052: public class AllocResultsServlet extends BaseServletComponent implements
053: BlackboardClient {
054:
055: private ArrayList supplyTasks = new ArrayList();
056: private ArrayList projectSupplyTasks = new ArrayList();
057: protected MessageAddress agentId;
058: protected BlackboardService blackboard;
059:
060: public static final String SUPPLY = "SUPPLY Tasks";
061: public static final String PROJECTSUPPLY = "Project Supply Tasks";
062: public static final String PHASES = "Phases";
063: public static final String Results = "Results";
064:
065: /** A zero-argument constructor is required for dynamically loaded PSPs,
066: required by Class.newInstance()
067: **/
068: public AllocResultsServlet() {
069: super ();
070: }
071:
072: // odd BlackboardClient method:
073: public String getBlackboardClientName() {
074: return toString();
075: }
076:
077: // odd BlackboardClient method:
078: public long currentTimeMillis() {
079: throw new UnsupportedOperationException(this
080: + " asked for the current time???");
081: }
082:
083: // unused BlackboardClient method:
084: public boolean triggerEvent(Object event) {
085: // if we had Subscriptions we'd need to implement this.
086: //
087: // see "ComponentPlugin" for details.
088: throw new UnsupportedOperationException(this
089: + " only supports Blackboard queries, but received "
090: + "a \"trigger\" event: " + event);
091: }
092:
093: public void setBlackboardService(BlackboardService blackboard) {
094: this .blackboard = blackboard;
095: }
096:
097: public final void setAgentIdentificationService(
098: AgentIdentificationService ais) {
099: MessageAddress an;
100: if ((ais != null)
101: && ((an = ais.getMessageAddress()) instanceof MessageAddress)) {
102: this .agentId = (MessageAddress) an;
103: } else {
104: // FIXME: Log something?
105: }
106: }
107:
108: // release services:
109: public void unload() {
110: super .unload();
111: if (blackboard != null) {
112: serviceBroker.releaseService(this , BlackboardService.class,
113: blackboard);
114: blackboard = null;
115: }
116: }
117:
118: protected String getPath() {
119: return "/AllocationResults";
120: }
121:
122: protected MessageAddress getAgentID() {
123: return agentId;
124: }
125:
126: protected Servlet createServlet() {
127: // create inner class
128: return new MyServlet();
129: }
130:
131: protected void displayExceptionFailure(PrintWriter out, Exception e) {
132: printHtmlBegin(out);
133: out.println("<font size=+1>Failed due to Exception</font><p>");
134: out.println(e);
135: out.println("<p><pre>");
136: e.printStackTrace(out);
137: out.println("</pre>");
138: printHtmlEnd(out);
139: }
140:
141: protected void displayResults(PrintWriter out) {
142: try {
143: blackboard.openTransaction();
144: supplyTasks = new ArrayList(blackboard
145: .query(taskPred("Supply")));
146: projectSupplyTasks = new ArrayList(blackboard
147: .query(taskPred("ProjectSupply")));
148: blackboard.closeTransaction();
149:
150: printHtmlBegin(out);
151:
152: out
153: .println("<p align=\"center\"><b><font size=\"5\">ALLOCATION RESULTS: SUPPLY TASKS</font></b></p>");
154: out.println("<table width=\"100%\" border=\"1\">");
155: //table headers
156: out.println("<tr>");
157: out.println("<td>" + "Task UID" + "</td>");
158: out.println("<td>" + "Phased" + "</td>");
159: //out.println("<td>" + "# of Phases" + "</td>");
160: out.println("<td>" + "Results" + "</td>");
161: out.println("<td>" + "Confidence" + "</td>");
162: out.println("<td>" + "IsSuccess" + "</td>");
163: out.println("</tr>");
164:
165: for (Iterator iterator = supplyTasks.iterator(); iterator
166: .hasNext();) {
167: Task sTask = (Task) iterator.next();
168: PlanElement pe = sTask.getPlanElement();
169: if (pe != null) {
170: AllocationResult estResult = pe
171: .getEstimatedResult();
172: AllocationResult repResult = pe.getReportedResult();
173: if (estResult != null) {
174: out.println("<tr>");
175: out.println("<td>" + sTask.getUID() + "</td>");
176: out.println("<td>" + estResult.isPhased()
177: + "</td>");
178: // out.println("<td>" + phasedResults.size() + "</td>");
179: out.println("<td>" + "EstimatedResults="
180: + parseResult(estResult) + "</td>");
181: out.println("<td>"
182: + estResult.getConfidenceRating()
183: + "</td>");
184: out.println("<td>" + estResult.isSuccess()
185: + "</td>");
186: out.println("</tr>");
187: }
188:
189: if (repResult != null) {
190: out.println("<tr>");
191: out.println("<td>" + sTask.getUID() + "</td>");
192: out.println("<td>" + repResult.isPhased()
193: + "</td>");
194: //out.println("<td>" + repPhasedResults.size() + "</td>");
195: out.println("<td>" + "ReportedResults="
196: + parseResult(repResult) + "</td>");
197: out.println("<td>"
198: + repResult.getConfidenceRating()
199: + "</td>");
200: out.println("<td>" + repResult.isSuccess()
201: + "</td>");
202: out.println("</tr>");
203: }
204:
205: }
206: }
207:
208: out.println("</table>");
209: printHtmlEnd(out);
210: out.flush();
211:
212: //BEGIN PROJECTSUPPLY
213:
214: out
215: .println("<p align=\"center\"><b><font size=\"5\">ALLOCATION RESULTS: PROJECT SUPPLY TASKS</font></b></p>");
216: out.println("<table width=\"100%\" border=\"1\">");
217: //table headers
218: out.println("<tr>");
219: out.println("<td>" + "Task UID" + "</td>");
220: out.println("<td>" + "Phased" + "</td>");
221: //out.println("<td>" + "# of Phases" + "</td>");
222: out.println("<td>" + "Results" + "</td>");
223: out.println("<td>" + "Confidence" + "</td>");
224: out.println("<td>" + "IsSuccess" + "</td>");
225: out.println("</tr>");
226:
227: for (Iterator iterator = projectSupplyTasks.iterator(); iterator
228: .hasNext();) {
229: Task psTask = (Task) iterator.next();
230: PlanElement pspe = psTask.getPlanElement();
231: if (pspe != null) {
232: AllocationResult estResult = pspe
233: .getEstimatedResult();
234: AllocationResult repResult = pspe
235: .getReportedResult();
236:
237: if (estResult != null) {
238: out.println("<tr>");
239: out.println("<td>" + psTask.getUID() + "</td>");
240: out.println("<td>" + estResult.isPhased()
241: + "</td>");
242: // out.println("<td>" + phasedResults.size() + "</td>");
243: out.println("<td>" + "EstimatedResults="
244: + parseResult(estResult) + "</td>");
245: out.println("<td>"
246: + estResult.getConfidenceRating()
247: + "</td>");
248: out.println("<td>" + estResult.isSuccess()
249: + "</td>");
250: out.println("</tr>");
251: }
252:
253: if (repResult != null) {
254: out.println("<tr>");
255: out.println("<td>" + psTask.getUID() + "</td>");
256: out.println("<td>" + repResult.isPhased()
257: + "</td>");
258: //out.println("<td>" + repPhasedResults.size() + "</td>");
259: out.println("<td>" + "ReportedResults="
260: + parseResult(repResult) + "</td>");
261: out.println("<td>"
262: + repResult.getConfidenceRating()
263: + "</td>");
264: out.println("<td>" + repResult.isSuccess()
265: + "</td>");
266: out.println("</tr>");
267: }
268:
269: }
270: }
271:
272: out.println("</table>");
273: printHtmlEnd(out);
274: out.flush();
275:
276: } catch (Exception e) {
277: out.println("</table>");
278: printHtmlEnd(out);
279: e.printStackTrace();
280: }
281: }
282:
283: protected void printHtmlBegin(PrintWriter out) {
284: out.println("<html>");
285: out.println("<head>");
286: out.println("<title>Allocation Results Servlet</title>");
287: out
288: .println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
289: out.println("</head>");
290: out.println("<body bgcolor=\"#FFFFFF\">");
291: }
292:
293: protected void printHtmlEnd(PrintWriter out) {
294: out.println("</body>");
295: out.println("</html>");
296: }
297:
298: private String parseResult(AllocationResult ar) {
299: ArrayList phasedResults;
300: String printableResults = "[";
301: if (ar.isPhased()) {
302: phasedResults = (ArrayList) ar
303: .getPhasedAspectValueResults();
304: } else {
305: phasedResults = new ArrayList(1);
306: phasedResults.add(ar.getAspectValueResults());
307: }
308: Iterator it = phasedResults.iterator();
309: while (it.hasNext()) {
310: Object obj = it.next();
311: if (obj instanceof AspectValue) {
312: AspectValue av = (AspectValue) obj;
313: printableResults = printableResults + "{"
314: + av.getAspectType() + " , " + av.getValue()
315: + "} ";
316: } else if (obj instanceof AspectValue[]) {
317: AspectValue[] avs = (AspectValue[]) obj;
318: for (int i = 0; i < avs.length; i++) {
319: AspectValue anAV = (AspectValue) avs[i];
320: printableResults = printableResults + "{"
321: + anAV.getAspectType() + " , "
322: + anAV.getValue() + "} ";
323: }
324: } else if (obj instanceof Collection) {
325: ArrayList alist = (ArrayList) obj;
326: Iterator listIt = alist.iterator();
327: while (listIt.hasNext()) {
328: AspectValue av = (AspectValue) listIt.next();
329: printableResults = printableResults + "{"
330: + av.getAspectType() + " , "
331: + av.getValue() + "} ";
332: }
333: } else {
334: System.out
335: .println("AllocResults servlet got unexpected object: "
336: + obj);
337: }
338: }
339: printableResults = printableResults + "]";
340: return printableResults;
341: }
342:
343: protected UnaryPredicate taskPred(String verbString) {
344: final String vString = verbString;
345: return new UnaryPredicate() {
346: public boolean execute(Object o) {
347: if (o instanceof Task) {
348: Task task = (Task) o;
349: if (task.getVerb().equals(Verb.get(vString))) {
350: return true;
351: }
352: }
353: return false;
354: }
355: };
356: }
357:
358: private class MyServlet extends HttpServlet {
359:
360: public void doGet(HttpServletRequest req,
361: HttpServletResponse resp) throws IOException {
362: MyWorker mw = new MyWorker(req, resp);
363: mw.execute();
364: }
365:
366: private class MyWorker {
367:
368: // from the "doGet(..)":
369: private HttpServletRequest request;
370: private HttpServletResponse response;
371:
372: public MyWorker(HttpServletRequest req,
373: HttpServletResponse resp) {
374: request = req;
375: response = resp;
376: }
377:
378: public void execute() throws IOException {
379: PrintWriter out = response.getWriter();
380:
381: try {
382: displayResults(out);
383: } catch (Exception topLevelException) {
384: displayExceptionFailure(out, topLevelException);
385: }
386: }
387:
388: }
389: }
390:
391: } // end of class
|