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.glm.plugins.tools;
028:
029: import java.awt.BorderLayout;
030: import java.awt.event.ActionEvent;
031: import java.util.Collection;
032: import java.util.Date;
033: import java.util.HashSet;
034: import java.util.Vector;
035:
036: import javax.swing.JButton;
037: import javax.swing.JFrame;
038: import javax.swing.JLabel;
039: import javax.swing.JPanel;
040: import javax.swing.JTextField;
041:
042: import org.cougaar.glm.ldm.asset.Organization;
043: import org.cougaar.lib.callback.UTILAllocationCallback;
044: import org.cougaar.lib.callback.UTILAllocationListener;
045: import org.cougaar.lib.util.UTILAllocate;
046: import org.cougaar.planning.ldm.plan.Allocation;
047: import org.cougaar.planning.ldm.plan.Task;
048:
049: /**
050: * <pre>
051: * An extension of the GLMStimulator Plugin that automatically
052: * sends batches of a given task and keep track of the time it
053: * takes to finish the batch.
054: * </pre>
055: */
056: public class GLMTestingStimulatorPlugin extends GLMStimulatorPlugin
057: implements UTILAllocationListener {
058:
059: /** Add the filter for allocations */
060: public void setupFilters() {
061: super .setupFilters();
062:
063: if (isInfoEnabled())
064: info(getName() + " : Filtering for Allocations...");
065: addFilter(myAllocCallback = createAllocCallback());
066: }
067:
068: /***************************/
069: /*** Allocation Listener ***/
070: /***************************/
071:
072: protected UTILAllocationCallback myAllocCallback;
073:
074: protected UTILAllocationCallback createAllocCallback() {
075: return new UTILAllocationCallback(this , logger);
076: }
077:
078: protected UTILAllocationCallback getAllocCallback() {
079: return myAllocCallback;
080: }
081:
082: public boolean interestingNotification(Task t) {
083: return true;
084: }
085:
086: public boolean needToRescind(Allocation alloc) {
087: return false;
088: }
089:
090: public boolean handleRescindedAlloc(Allocation alloc) {
091: return false;
092: }
093:
094: public void handleRemovedAlloc(Allocation alloc) {
095: }
096:
097: public void publishRemovalOfAllocation(Allocation alloc) {
098: }
099:
100: /**
101: * Everytime a successful allocation returns, we want to send a new batch of
102: * the same tasks until the desired total has been sent.
103: *
104: * Also keep track of the time spent on each batch.
105: *
106: * Cache the already handled allocations by their UID's
107: */
108: public void handleSuccessfulAlloc(Allocation alloc) {
109: // we were rehydrated -- no guarantees across rehydration
110: if (batchStart == null || testStart == null)
111: return;
112:
113: // Check for a completed batch
114: if ((alloc.getReportedResult() != null)
115: && (alloc.getReportedResult().getConfidenceRating() >= UTILAllocate.HIGHEST_CONFIDENCE)) {
116: if (!handledAllocations.contains(alloc.getUID())) {
117: // Cache the allocation UID
118: handledAllocations.add(alloc.getUID());
119:
120: // Print timing information for the completed batch
121: String t = getElapsedTime(batchStart);
122: String total = getElapsedTime(testStart);
123: if (isInfoEnabled())
124: info("\n*** Testing batch #" + batchesSent
125: + " completed in " + t + " total " + total);
126:
127: // Cache the timing information
128: batchTimes.add(t);
129:
130: // Send another batch if needed
131: if (batchesSent < totalBatches) {
132: sendTasksAgain(myLabel, myXmlTaskFile);
133: batchesSent++;
134: } else {
135: printTestingSummary();
136: }
137: }
138: }
139: }
140:
141: /**
142: * Actually create the GUI window.
143: * Adds an additional field for the number of times the tasks should be sent.
144: *
145: * @param infile - default input file. From param
146: * "ClusterInputFile" defined in <ClusterName>.env.xml.
147: */
148: protected void createGUI(String infile) {
149: frame = new JFrame(getClusterName()
150: + " - GLMTestingStimulatorPlugin");
151: frame.getContentPane().setLayout(new BorderLayout());
152:
153: JPanel panel = new JPanel();
154: JButton button = new JButton("Send Tasks");
155: JButton button2 = new JButton("Rescind Tasks");
156: JTextField text = new JTextField(infile);
157: JTextField iterations = new JTextField("1");
158: JLabel label = new JLabel(
159: " ");
160:
161: myGLMListener = new GLMTestingButtonListener(label, text,
162: iterations);
163: button.addActionListener(myGLMListener);
164: button2.addActionListener(myGLMListener);
165:
166: panel.add(button);
167: panel.add(button2);
168: frame.getRootPane().setDefaultButton(button); // hitting return sends the tasks
169: panel.add(text);
170: panel.add(iterations);
171:
172: frame.getContentPane().add("Center", panel);
173: frame.getContentPane().add("South", label);
174: frame.pack();
175: frame.setVisible(true);
176: }
177:
178: /**
179: * <pre>
180: * An ActionListener that listens to the GLM buttons.
181: *
182: * If the Send Tasks button is clicked, sends the tasks from
183: * the file in the text field of the dialog.
184: *
185: * Otherwise, if the rescind button is clicked, rescinds the
186: * last task sent.
187: *
188: * </pre>
189: */
190: class GLMTestingButtonListener extends GLMButtonListener {
191: GLMTestingButtonListener(JLabel label, JTextField text,
192: JTextField iterations) {
193: super (label, text);
194: this .iterations = iterations;
195: }
196:
197: protected JTextField iterations;
198:
199: public void actionPerformed(ActionEvent e) {
200: String lnfName = e.getActionCommand();
201:
202: if (lnfName.equals("Send Tasks")) {
203: // Get name of XML data file
204: if (getFinder().locateFile(text.getText()) == null) {
205: label
206: .setText("Couldn't find file. Check path, try again.");
207: return;
208: }
209: totalBatches = Integer.parseInt(iterations.getText());
210: batchesSent = 1;
211:
212: testStart = new Date();
213:
214: sendTasks(label, text.getText());
215: // Should disable the input button after the task has been sent
216: } else {
217: rescindTasks(label);
218: }
219: }
220: }
221:
222: protected void sendTasks(JLabel label, String xmlTaskFile) {
223: batchStart = new Date();
224:
225: myLabel = label;
226: myXmlTaskFile = xmlTaskFile;
227:
228: Collection tasks = null;
229: tasksSent.clear();
230: if (!subordinatesHaveReported()) {
231: label
232: .setText("No subordinates to task yet. Wait until they report.");
233: } else {
234: try {
235: // Have to do this all within transaction boundary
236:
237: // For some reason, every setXXX (e.g. setVerb) on a NewTask
238: // fires off a changeReport, and these must be inside of a transaction,
239: // apparently. There is no way currently to temporarily turn off this feature.
240: // GWFV 10/11/2000
241:
242: blackboard.openTransaction();
243: // Get the tasks out of the XML file
244: tasks = readXmlTasks(xmlTaskFile);
245:
246: // First find the organizations that we will allocate to.
247: Collection supportedOrgs = getSupportedOrgs();
248: if (supportedOrgs.isEmpty()) {
249: label
250: .setText("No task sent, since no subordinates have reported (yet).");
251: } else {
252: allocateTasks(tasks, supportedOrgs);
253: label
254: .setText("Sent 1 batch to "
255: + ((Organization) supportedOrgs
256: .iterator().next())
257: .getItemIdentificationPG()
258: .getItemIdentification()
259: + ((supportedOrgs.size() > 1) ? ", etc. clusters"
260: : " cluster"));
261: }
262: } catch (Exception exc) {
263: System.err.println("Could not send tasks.");
264: System.err.println(exc.getMessage());
265: exc.printStackTrace();
266: } finally {
267: blackboard.closeTransactionDontReset();
268: }
269: }
270: }
271:
272: protected void sendTasksAgain(JLabel label, String xmlTaskFile) {
273: batchStart = new Date();
274:
275: Collection tasks = null;
276: try {
277: tasks = readXmlTasks(xmlTaskFile);
278:
279: Collection supportedOrgs = getSupportedOrgs();
280: if (supportedOrgs.isEmpty()) {
281: label
282: .setText("No task sent, since no subordinates have reported (yet).");
283: } else {
284: allocateTasks(tasks, supportedOrgs);
285: label
286: .setText("Sent "
287: + batchesSent
288: + " batches to "
289: + ((Organization) supportedOrgs
290: .iterator().next())
291: .getItemIdentificationPG()
292: .getItemIdentification()
293: + ((supportedOrgs.size() > 1) ? ", etc. clusters"
294: : " cluster"));
295: }
296: } catch (Exception exc) {
297: logger.error("Could not send tasks.", exc);
298: }
299: }
300:
301: protected String getElapsedTime(Date start) {
302: Date end = new Date();
303: long diff = end.getTime() - start.getTime();
304: long min = diff / 60000l;
305: long sec = (diff - (min * 60000l)) / 1000l;
306:
307: return min + ":" + ((sec < 10) ? "0" : "") + sec;
308: }
309:
310: protected void printTestingSummary() {
311: String totalTime = getElapsedTime(testStart);
312:
313: info("\n************************* Testing Summary *************************\n");
314: info(" Batch Number Batch Time ");
315: for (int i = 0; i < batchTimes.size(); i++)
316: info(" " + (i + 1) + " "
317: + batchTimes.elementAt(i));
318: info("\n Total Time: " + totalTime);
319: info("\n*******************************************************************\n");
320:
321: }
322:
323: protected Date testStart = null;
324: protected Date batchStart = null;
325: protected Vector batchTimes = new Vector();
326: protected Collection handledAllocations = new HashSet();
327:
328: protected JLabel myLabel = null;
329: protected String myXmlTaskFile = "";
330: protected int batchesSent = 0;
331: protected int totalBatches = 0;
332: }
|