001: /*
002: <copyright>
003: Copyright 2002-2003 BBNT Solutions, LLC
004: under sponsorship of the Defense Advanced Research Projects Agency (DARPA)
005: and the Defense Logistics Agency (DLA).
006:
007: This program is free software; you can redistribute it and/or modify
008: it under the terms of the Cougaar Open Source License as published by
009: DARPA on the Cougaar Open Source Website (www.cougaar.org).
010:
011: THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
012: PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
013: IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
014: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
015: ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
016: HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
017: DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
018: TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
019: PERFORMANCE OF THE COUGAAR SOFTWARE.
020: </copyright>
021: */
022: package org.cougaar.logistics.servlet;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026:
027: import java.util.ArrayList;
028: import java.util.Calendar;
029: import java.util.Date;
030: import java.util.Vector;
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: import javax.servlet.Servlet;
035:
036: import java.io.PrintWriter;
037:
038: import javax.servlet.http.HttpServlet;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041:
042: import org.cougaar.core.blackboard.BlackboardClient;
043:
044: import org.cougaar.core.service.BlackboardService;
045: import org.cougaar.core.service.DomainService;
046: import org.cougaar.core.service.AgentIdentificationService;
047: import org.cougaar.core.service.LoggingService;
048: import org.cougaar.core.service.AlarmService;
049:
050: import org.cougaar.core.servlet.BaseServletComponent;
051: import org.cougaar.core.mts.MessageAddress;
052: import org.cougaar.core.logging.LoggingServiceWithPrefix;
053:
054: import org.cougaar.glm.ldm.asset.ClassIXRepairPart;
055: import org.cougaar.glm.ldm.asset.Organization;
056:
057: import org.cougaar.planning.ldm.asset.Asset;
058: import org.cougaar.planning.ldm.asset.CommunityPGImpl;
059: import org.cougaar.planning.ldm.asset.NewClusterPG;
060: import org.cougaar.planning.ldm.asset.NewItemIdentificationPG;
061: import org.cougaar.planning.ldm.asset.NewTypeIdentificationPG;
062: import org.cougaar.planning.ldm.asset.PropertyGroupSchedule;
063:
064: import org.cougaar.planning.ldm.plan.*;
065: import org.cougaar.planning.ldm.PlanningFactory;
066: import org.cougaar.planning.plugin.util.PluginHelper;
067: import org.cougaar.util.UnaryPredicate;
068:
069: import org.cougaar.logistics.ldm.Constants;
070:
071: import org.cougaar.logistics.plugin.inventory.UtilsProvider;
072: import org.cougaar.logistics.plugin.inventory.TaskUtils;
073: import org.cougaar.logistics.plugin.inventory.TimeUtils;
074: import org.cougaar.logistics.plugin.inventory.AssetUtils;
075: import org.cougaar.logistics.plugin.utils.ScheduleUtils;
076:
077: /**
078: * Servlet allocates prints out demand totals
079: *
080: *@author MDavis
081: */
082: public class MapperServlet extends BaseServletComponent implements
083: BlackboardClient, UtilsProvider {
084:
085: private BlackboardService blackboard;
086: private AgentIdentificationService agentService;
087: private AlarmService alarmService;
088: private PrintWriter out;
089: private DomainService domainService = null;
090: private PlanningFactory planFactory;
091:
092: private LoggingService logger;
093:
094: protected TimeUtils timeUtils;
095: protected TaskUtils taskUtils;
096: protected ScheduleUtils scheduleUtils;
097: protected AssetUtils assetUtils;
098:
099: protected MapperPrinter printer;
100:
101: public void setDomainService(DomainService aDomainService) {
102: domainService = aDomainService;
103: }
104:
105: public DomainService getDomainService() {
106: return domainService;
107: }
108:
109: protected String getPath() {
110: return "/mapperServlet";
111: }
112:
113: protected Servlet createServlet() {
114: blackboard = (BlackboardService) serviceBroker.getService(this ,
115: BlackboardService.class, null);
116: if (blackboard == null) {
117: throw new RuntimeException(
118: "Unable to obtain blackboard service");
119: }
120:
121: agentService = (AgentIdentificationService) serviceBroker
122: .getService(this , AgentIdentificationService.class,
123: null);
124: if (agentService == null) {
125: throw new RuntimeException("Unable to obtain agent service");
126: }
127:
128: alarmService = (AlarmService) serviceBroker.getService(this ,
129: AlarmService.class, null);
130: if (alarmService == null) {
131: throw new RuntimeException("Unable to obtain alarm service");
132: }
133:
134: planFactory = (PlanningFactory) getDomainService().getFactory(
135: "planning");
136:
137: // get the logging service
138: logger = (LoggingService) serviceBroker.getService(this ,
139: LoggingService.class, null);
140: if (logger == null) {
141: throw new RuntimeException("Cannot find logging service!");
142: }
143:
144: timeUtils = new TimeUtils(this );
145: assetUtils = new AssetUtils(this );
146: taskUtils = new TaskUtils(this );
147: scheduleUtils = new ScheduleUtils(this );
148:
149: printer = new MapperPrinter(this );
150:
151: return new MyServlet();
152: }
153:
154: public LoggingService getLoggingService(Object requestor) {
155: LoggingService ls = (LoggingService) serviceBroker.getService(
156: requestor, LoggingService.class, null);
157: return LoggingServiceWithPrefix.add(ls, getAgentIdentifier()
158: + ": ");
159: }
160:
161: public String getAgentIdentifier() {
162: if (agentService != null) {
163: return agentService.getName();
164: }
165: return "Dont know Agent";
166: }
167:
168: public void setBlackboardService(BlackboardService blackboard) {
169: this .blackboard = blackboard;
170: }
171:
172: public String getBlackboardClientName() {
173: return toString();
174: }
175:
176: public long currentTimeMillis() {
177: return alarmService.currentTimeMillis();
178: }
179:
180: public boolean triggerEvent(Object event) {
181: return false;
182: }
183:
184: public void unload() {
185: super .unload();
186: //Release blackboard service
187: if (blackboard != null) {
188: serviceBroker.releaseService(this , BlackboardService.class,
189: blackboard);
190: blackboard = null;
191: }
192: }
193:
194: public TaskUtils getTaskUtils() {
195: return taskUtils;
196: }
197:
198: public TimeUtils getTimeUtils() {
199: return timeUtils;
200: }
201:
202: public AssetUtils getAssetUtils() {
203: return assetUtils;
204: }
205:
206: public ScheduleUtils getScheduleUtils() {
207: return scheduleUtils;
208: }
209:
210: private class MyServlet extends HttpServlet {
211:
212: String supplyType = "Ammunition";
213:
214: public String getSupplyType() {
215: return supplyType;
216: }
217:
218: /** More pirate garbage
219:
220: public void doGet(HttpServletRequest req, HttpServletResponse res)
221: throws IOException {
222:
223: out = res.getWriter();
224: out.println(headWithTitle("Publish Task") + "<BODY>");
225:
226: String action = "mapperServlet";
227:
228: out.println("<FORM ACTION=" + action + " METHOD=POST>\n");
229: out.println("<table><tr><td>NSN:</td><td>" + getChoices()
230: + "</td></tr>");
231: out.println("<tr><td>PROVIDER:</td><td><INPUT TYPE=TEXT NAME="
232: + "\"provider\"></td></tr></table>");
233: out.println("<CENTER><INPUT TYPE=SUBMIT VALUE=\"PUBLISH\"></CENTER>"
234: + "</FORM></BODY>");
235: out.flush();
236: }
237:
238: public void doPost(HttpServletRequest req, HttpServletResponse res)
239: throws IOException {
240:
241: String nsn = req.getParameter("nsn");
242: String provider = req.getParameter("provider");
243:
244: out = res.getWriter();
245: out.println(headWithTitle("Task published") + "<BODY>");
246:
247: try {
248: blackboard.openTransaction();
249: publishTask(nsn, provider);
250: } finally {
251: blackboard.closeTransactionDontReset();
252: out.println("<a href=taskServlet>back</a></body>");
253: out.flush();
254: }
255: } **/
256:
257: public void doGet(HttpServletRequest req,
258: HttpServletResponse res) throws IOException {
259:
260: Collection myOrgs = blackboard.query(orgsPredicate);
261: Iterator orgs = myOrgs.iterator();
262: Organization myOrg = null;
263: String orgName = "";
264: if (orgs.hasNext()) {
265: myOrg = (Organization) orgs.next();
266: orgName = myOrg.getItemIdentificationPG()
267: .getItemIdentification();
268: }
269:
270: Collection supplys = blackboard
271: .query(new SupplyTaskPredicate(getSupplyType(),
272: orgName, getTaskUtils()));
273: Collection level2s = blackboard
274: .query(new Level2TaskPredicate(getSupplyType(),
275: orgName, getTaskUtils()));
276: Collection level6s = blackboard
277: .query(new ProjectionTaskPredicate(getSupplyType(),
278: orgName, getTaskUtils()));
279:
280: Worker worker = new Worker(res, getSupplyType(), level2s,
281: level6s, supplys, orgName, printer);
282: worker.execute();
283: }
284:
285: }
286:
287: protected static class SupplyTaskPredicate implements
288: UnaryPredicate {
289: String supplyType;
290: TaskUtils taskUtils;
291: String orgName;
292:
293: public SupplyTaskPredicate(String type, String orgname,
294: TaskUtils aTaskUtils) {
295: supplyType = type;
296: orgName = orgname;
297: taskUtils = aTaskUtils;
298: }
299:
300: public boolean execute(Object o) {
301: if (o instanceof Task) {
302: Task task = (Task) o;
303: if (task.getVerb().equals(Constants.Verb.SUPPLY)) {
304: if (taskUtils
305: .isDirectObjectOfType(task, supplyType)) {
306: if (!taskUtils.isInternal(task)) {
307: if (!taskUtils
308: .isMyRefillTask(task, orgName)) {
309: //if (taskUtils.isMyDemandForecastProjection(task, orgName)){
310: return true;
311: }
312: }
313: }
314: }
315: }
316: return false;
317: }
318: }
319:
320: protected static class ProjectionTaskPredicate implements
321: UnaryPredicate {
322: String supplyType;
323: TaskUtils taskUtils;
324: String orgName;
325:
326: public ProjectionTaskPredicate(String type, String orgname,
327: TaskUtils aTaskUtils) {
328: supplyType = type;
329: orgName = orgname;
330: taskUtils = aTaskUtils;
331: }
332:
333: public boolean execute(Object o) {
334: if (o instanceof Task) {
335: Task task = (Task) o;
336: if (task.getVerb().equals(Constants.Verb.PROJECTSUPPLY)) {
337: if (!taskUtils.isLevel2(task)) {
338: if (taskUtils.isDirectObjectOfType(task,
339: supplyType)) {
340: //OSC only
341: //Should this ever be ready for Transport on level 6? - doesn't hurt but inneficient
342: if (!(taskUtils.isReadyForTransport(task))) {
343: //if (!taskUtils.isMyRefillTask(task, orgName)) {
344: // if (taskUtils.isMyDemandForecastProjection(task, orgName)) {
345: return true;
346: }
347: }
348: }
349: }
350: }
351: return false;
352: }
353: }
354:
355: protected static class Level2TaskPredicate implements
356: UnaryPredicate {
357: String supplyType;
358: TaskUtils taskUtils;
359: String orgName;
360:
361: public Level2TaskPredicate(String type, String orgname,
362: TaskUtils aTaskUtils) {
363: supplyType = type;
364: taskUtils = aTaskUtils;
365: orgName = orgname;
366: }
367:
368: public boolean execute(Object o) {
369: if (o instanceof Task) {
370: Task task = (Task) o;
371: if (task.getVerb().equals(Constants.Verb.PROJECTSUPPLY)) {
372: if (taskUtils.isLevel2(task)) {
373: if (taskUtils.isDirectObjectOfType(task,
374: supplyType)) {
375: // OSC only
376: if (!(taskUtils.isReadyForTransport(task))) {
377: // everything below me
378: if (!taskUtils.isMyRefillTask(task,
379: orgName)) {
380: // just the demand from my agent
381: //if (taskUtils.isMyDemandForecastProjection(task, orgName)) {
382: return true;
383: }
384: }
385: }
386: }
387: }
388: }
389: return false;
390: }
391: }
392:
393: private static UnaryPredicate orgsPredicate = new UnaryPredicate() {
394: public boolean execute(Object o) {
395: if (o instanceof Organization) {
396: return ((Organization) o).isSelf();
397: }
398: return false;
399: }
400: };
401:
402: private static class Worker {
403:
404: private HttpServletResponse response;
405: private String supplyType;
406: private String orgName;
407: private Collection level2Tasks;
408: private Collection level6Tasks;
409: private Collection supplyTasks;
410: private MapperPrinter printer;
411:
412: public Worker(HttpServletResponse response, String aSupplyType,
413: Collection level2, Collection level6,
414: Collection supply, String myOrg, MapperPrinter myPrinter) {
415: this .response = response;
416: supplyType = aSupplyType;
417: level2Tasks = level2;
418: level6Tasks = level6;
419: supplyTasks = supply;
420: orgName = myOrg;
421: printer = myPrinter;
422: }
423:
424: public void execute() throws IOException {
425: writeResponse();
426: }
427:
428: private void writeResponse() throws IOException {
429: response.setContentType("text/html");
430: PrintWriter out = response.getWriter();
431:
432: //printer call
433: //out.println(file);
434: printer.printDemandStats(level2Tasks, level6Tasks,
435: supplyTasks, supplyType, orgName, out);
436: }
437: }
438:
439: }
440:
441: /*** Remove Pirated software **
442:
443: private String headWithTitle(String title) {
444: return "<HEAD><TITLE>" + title + "</TITLE></HEAD>";
445: }
446:
447: private void publishTask(String nsnS, String providerName) {
448: Asset nsn = makePrototype(nsnS);
449:
450: NewTask task = planFactory.newTask();
451: task.setDirectObject(nsn);
452: task.setVerb(org.cougaar.glm.ldm.Constants.Verb.Supply);
453: Plan realityPlan = planFactory.getRealityPlan();
454: task.setPlan(realityPlan);
455:
456: AspectValue endTAV = TimeAspectValue.create(AspectType.END_TIME, DEFAULT_END_TIME);
457: ScoringFunction endScoreFunc =
458: ScoringFunction.createStrictlyAtValue(endTAV);
459: Preference endPreference = planFactory.newPreference(AspectType.END_TIME, endScoreFunc);
460:
461: Vector preferenceVector = new Vector(1);
462: preferenceVector.addElement(endPreference);
463: task.setPreferences(preferenceVector.elements());
464: blackboard.publishAdd(task);
465: out.println("Published task : " + true);
466: Organization org = createOrganization(providerName);
467:
468: boolean isSuccess = true;
469: AllocationResult estAR = PluginHelper.createEstimatedAllocationResult(task, planFactory, 0.5, isSuccess);
470: Allocation allocation = planFactory.createAllocation(task.getPlan(), task,
471: org, estAR, Role.ASSIGNED);
472:
473: out.println("Allocating task to: " + providerName + "<BR>");
474: out.println("Direct Object:" +
475: allocation.getTask().getDirectObject().getTypeIdentificationPG().getTypeIdentification() + "<BR>");
476: blackboard.publishAdd(allocation);
477: out.println("Published allocation: " + true + "<BR>");
478: }
479:
480: private Asset makePrototype(String itemID) {
481:
482: Asset cix = planFactory.getPrototype(itemID);
483: if(cix == null) {
484: cix = planFactory.createPrototype(ClassIXRepairPart.class, itemID);
485: }
486:
487: return cix;
488: }
489:
490: private Organization createOrganization(String orgStr) {
491: final String uic = orgStr;
492:
493: Organization org = (Organization) planFactory.createAsset("Organization");
494: org.initRelationshipSchedule();
495: org.setLocal(false);
496:
497: ((NewTypeIdentificationPG) org.getTypeIdentificationPG()).setTypeIdentification(UTC);
498: NewItemIdentificationPG itemIdProp =
499: (NewItemIdentificationPG) org.getItemIdentificationPG();
500: itemIdProp.setItemIdentification(uic);
501: itemIdProp.setNomenclature(orgStr);
502: itemIdProp.setAlternateItemIdentification(orgStr);
503:
504: NewClusterPG cpg = (NewClusterPG) org.getClusterPG();
505: cpg.setMessageAddress(MessageAddress.getMessageAddress(orgStr));
506:
507: CommunityPGImpl communityPG =
508: (CommunityPGImpl) planFactory.createPropertyGroup(CommunityPGImpl.class);
509: PropertyGroupSchedule schedule = new PropertyGroupSchedule();
510:
511: ArrayList communities = new ArrayList(1);
512: communities.add("COUGAAR");
513: communityPG.setCommunities(communities);
514: communityPG.setTimeSpan(DEFAULT_START_TIME, DEFAULT_END_TIME);
515: schedule.add(communityPG);
516: org.setCommunityPGSchedule(schedule);
517:
518: return org;
519: }
520:
521: private String getChoices() {
522: return "<select name=\"nsn\">" +
523: "<option value=\"NSN/4710007606205\" selected>"
524: + "WUC1:NSN1:4710007606205 Metal tube assembly" +
525: "<option value=\"NSN/4320012017527\">"
526: + "WUC1:NSN2:4320012017527 Rotary pump" +
527: "<option value=\"NSN/5930008432366\">"
528: + "WUC1:NSN3:5930008432366 Pressure switch" +
529: "<option value=\"NSN/1730007603370\">"
530: + "WUC2:NSN1:1730007603370 Ground safety pin" +
531: "<option value=\"NSN/5945002010273\">"
532: + "WUC2:NSN2:5945002010273 Solid state switch" +
533: "<option value=\"NSN/5930011951836\">"
534: + "WUC2:NSN3:5930011951836 Sensitive switch" +
535: "<option value=\"NSN/4310004145989\">"
536: + "WUC3:NSN1:4310004145989 Reciprocating compressor" +
537: "<option value=\"NSN/6105007262754\">"
538: + "WUC3:NSN2:6105007262754 AC Motor" +
539: "<option value=\"NSN/3110005656233\">"
540: + "WUC3:NSN3:3110005656233 Rod end roller bearing" +
541: "</select>";
542: }
543:
544:
545: ***/
|