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 org.cougaar.lib.param.ParamMap;
029: import com.bbn.vishnu.scheduling.Assignment;
030: import com.bbn.vishnu.scheduling.MultitaskAssignment;
031: import com.bbn.vishnu.scheduling.Resource;
032: import com.bbn.vishnu.scheduling.Scheduler;
033: import com.bbn.vishnu.scheduling.SchedulingData;
034: import com.bbn.vishnu.scheduling.TimeOps;
035: import org.cougaar.util.StringKey;
036: import org.cougaar.util.log.Logger;
037:
038: import org.cougaar.planning.ldm.plan.Task;
039: import org.cougaar.planning.ldm.asset.Asset;
040:
041: import java.util.Date;
042: import java.util.ArrayList;
043: import java.util.List;
044: import java.util.Vector;
045: import org.w3c.dom.Document;
046:
047: /**
048: * Direct mode.
049: * <p>
050: * Creates an internal instance of the scheduler and talks to it, instead
051: * of to a web server. Uses direct translation of Cougaar to Vishnu objects,
052: * and reads the assignments directly. <b>No XML involved.</b>
053: *
054: * Needs the plugin to be a DirectResultListener. Mainly this means implementing
055: * the prepareVishnuObjects method.
056: * @see DirectResultListener
057: */
058: public class DirectMode extends InternalMode {
059: public DirectMode(ModeListener parent, VishnuComm comm,
060: XMLProcessor xmlProcessor, VishnuDomUtil domUtil,
061: VishnuConfig config, ResultHandler resultHandler,
062: ParamMap myParamTable, Logger logger) {
063: super (parent, comm, xmlProcessor, domUtil, config,
064: resultHandler, myParamTable, logger);
065: localSetup();
066: }
067:
068: protected void localSetup() {
069: super .localSetup();
070:
071: try {
072: debugParseAnswer = getMyParams().getBooleanParam(
073: "debugParseAnswer");
074: } catch (Exception e) {
075: debugParseAnswer = false;
076: }
077: }
078:
079: /**
080: * Handles direct mode differently from external/internal.
081: * <p>
082: * Calls prepareVishnuObjects on listener to populate <tt>vishnuTasks</tt> and
083: * <tt>vishnuResources</tt> lists with Vishnu objects.
084: * <p>
085: * Also sends header with the time window information, and any other
086: * data to scheduler via the internal buffer (in VishnuComm).
087: * @param stuffToSend Cougaar tasks and assets
088: * @param objectFormatDoc used to figure out info about fields
089: */
090: public void prepareData(List stuffToSend, Document objectFormatDoc) {
091: vishnuTasks = new ArrayList();
092: vishnuResources = new ArrayList();
093: changedVishnuResources = new ArrayList();
094:
095: Date start = new Date();
096:
097: ((DirectModeListener) parent).prepareVishnuObjects(stuffToSend,
098: parent.getChangedAssets(), vishnuTasks,
099: vishnuResources, changedVishnuResources,
100: objectFormatDoc, sched.getData());
101: parent.clearChangedAssets();
102:
103: if (logger.isDebugEnabled())
104: logger.debug(getName()
105: + ".prepareData - clearing changed assets!");
106:
107: if (showTiming)
108: domUtil
109: .reportTime(
110: ".prepareData - prepared vishnu objects in ",
111: start);
112:
113: // think it's OK to send this more than once
114: serializeAndPostDoc(xmlProcessor.getVanillaHeader());
115:
116: sendOtherData();
117: }
118:
119: /**
120: * Run directly. Give Vishnu tasks and resources to scheduler and get assignments
121: * back, <i>without</i> using XML. <p>
122: *
123: * Uses <tt>internalBuffer</tt> to send the specs, the object format and other data.<br>
124: * Adds the task and resources (<tt>addTasksDirectly</tt>), and then runs the scheduler
125: * (<tt>Scheduler.scheduleInternal</tt>). Reads the assignments with
126: * <tt>directlyHandleAssignments</tt>.
127: * <p>
128: * <tt>vishnuTasks</tt> and <tt>vishnuResources</tt> are created in prepareData and populated
129: * in #prepareVishnuObjects. <p>
130: *
131: * @see #addTasksDirectly
132: * @see #prepareData
133: * @see org.cougaar.lib.vishnu.client.VishnuPlugin#prepareVishnuObjects
134: * @see com.bbn.vishnu.scheduling.Scheduler#scheduleInternal
135: */
136: public void run() {
137: Date start = new Date();
138:
139: int unhandledTasks = prepareScheduler();
140:
141: // These are things created by the scheduler during setup that
142: // are needed for direct object writing.
143: TimeOps timeOps = sched.getTimeOps();
144: SchedulingData data = sched.getData();
145: // sched.clearCaches ();
146:
147: // add tasks and resources to data
148: // vishnuTasks & Resources created in prepareData and set in prepareVishnuObjects
149: addTasksDirectly(data, vishnuTasks, vishnuResources,
150: changedVishnuResources);
151:
152: // Call scheduleInternal after adding all the data
153: Date start2 = new Date();
154:
155: try {
156: sched.scheduleInternal(null, false);
157:
158: dumpPostProcess();
159: if (showTiming)
160: domUtil.reportTime(
161: ".runDirectly - scheduler processed "
162: + parent.getNumTasks() + " tasks in ",
163: start2);
164:
165: // get the assignments the scheduler made, make plan elements
166: ((DirectResultHandler) resultHandler)
167: .directlyHandleAssignments(sched, data, timeOps);
168: } catch (Exception e) {
169: logger.error(getName()
170: + ".runDirectly - Got error running scheduler : "
171: + e.getMessage(), e);
172: } finally {
173: cleanUpAfterScheduling(unhandledTasks);
174: }
175: }
176:
177: /** calls methods on SchedulingData to add tasks, resources, and changed resources */
178: protected void addTasksDirectly(SchedulingData data,
179: List vishnuTasks, List vishnuResources,
180: List changedVishnuResources) {
181: Date start = new Date();
182:
183: if (logger.isInfoEnabled())
184: logger.info(getName() + ".runDirectly - adding "
185: + vishnuTasks.size() + " tasks, "
186: + vishnuResources.size() + " resources, "
187: + changedVishnuResources.size()
188: + " changed resources to scheduler data.");
189:
190: for (int i = 0; i < vishnuTasks.size(); i++) {
191: Object vishnuObject = vishnuTasks.get(i);
192: data.addTask((com.bbn.vishnu.scheduling.Task) vishnuObject);
193: }
194: for (int i = 0; i < vishnuResources.size(); i++) {
195: Object vishnuObject = vishnuResources.get(i);
196: data.addResource((Resource) vishnuObject);
197: }
198: for (int i = 0; i < changedVishnuResources.size(); i++) {
199: Object vishnuObject = changedVishnuResources.get(i);
200: data.replaceResource((Resource) vishnuObject);
201: }
202: if (logger.isDebugEnabled())
203: for (int i = 0; i < data.getResources().length; i++) {
204: logger.debug(getName()
205: + ".addTasksDirectly - Known Resource #" + i
206: + " : \n" + data.getResources()[i]);
207: }
208: }
209:
210: protected String getName() {
211: return parent.getName() + "-DirectMode";
212: }
213:
214: protected List vishnuTasks, vishnuResources,
215: changedVishnuResources;
216: protected boolean debugParseAnswer = false;
217: }
|