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: package org.cougaar.mlm.plugin.sample;
027:
028: import java.awt.BorderLayout;
029: import java.awt.Container;
030: import java.text.NumberFormat;
031: import java.util.Comparator;
032: import java.util.Enumeration;
033: import java.util.TreeSet;
034:
035: import javax.swing.JFrame;
036: import javax.swing.JLabel;
037: import javax.swing.JScrollPane;
038: import javax.swing.JTable;
039: import javax.swing.table.AbstractTableModel;
040:
041: import org.cougaar.core.blackboard.IncrementalSubscription;
042: import org.cougaar.planning.ldm.plan.Aggregation;
043: import org.cougaar.planning.ldm.plan.Allocation;
044: import org.cougaar.planning.ldm.plan.AllocationResult;
045: import org.cougaar.planning.ldm.plan.AssetTransfer;
046: import org.cougaar.planning.ldm.plan.Expansion;
047: import org.cougaar.planning.ldm.plan.PlanElement;
048: import org.cougaar.planning.ldm.plan.Task;
049: import org.cougaar.planning.plugin.legacy.SimplePlugin;
050: import org.cougaar.util.UnaryPredicate;
051:
052: /**
053: * The TaskAssessorPlugin monitors the disposition of Tasks and
054: * displays a summary.
055: *
056: */
057:
058: public class TaskAssessorPlugin extends SimplePlugin {
059: /** frame displaying messages **/
060: static JFrame frame;
061:
062: private Comparator taskComparator = new Comparator() {
063: public int compare(Object l, Object r) {
064: Task lt = (Task) l;
065: Task rt = (Task) r;
066: return lt.getUID().compareTo(rt.getUID());
067: }
068: };
069:
070: private class MyTreeSet extends TreeSet {
071: public MyTreeSet(Comparator comparator) {
072: super (comparator);
073: }
074:
075: public synchronized boolean add(Object o) {
076: taskArray = null;
077: return super .add(o);
078: }
079:
080: public synchronized boolean remove(Object o) {
081: taskArray = null;
082: return super .remove(o);
083: }
084: }
085:
086: private Task[] taskArray = null;
087:
088: private MyTreeSet tasks = new MyTreeSet(taskComparator);
089:
090: private Task[] getTaskArray() {
091: if (taskArray == null) {
092: taskArray = (Task[]) tasks.toArray(new Task[tasks.size()]);
093: }
094: return taskArray;
095: }
096:
097: private NumberFormat confidenceFormat = NumberFormat
098: .getPercentInstance();
099:
100: private class MyTableModel extends AbstractTableModel {
101: public int getRowCount() {
102: return tasks.size();
103: }
104:
105: public int getColumnCount() {
106: return 4;
107: }
108:
109: public String getColumnName(int column) {
110: switch (column) {
111: case 0:
112: return "Task ID";
113: case 1:
114: return "Disposition";
115: case 2:
116: return "Status";
117: case 3:
118: return "Confidence";
119: }
120: return null;
121: }
122:
123: public Object getValueAt(int row, int column) {
124: synchronized (tasks) {
125: Task[] ary = getTaskArray();
126: if (row >= ary.length)
127: return null;
128: Task task = ary[row];
129: PlanElement pe = task.getPlanElement();
130: AllocationResult ar = null;
131: if (pe != null) {
132: ar = pe.getEstimatedResult();
133: }
134: switch (column) {
135: case 0: // The task id
136: return task.getUID();
137: case 1:
138: if (pe == null)
139: return "not disposed";
140: if (pe instanceof Allocation)
141: return "Allocation";
142: if (pe instanceof Aggregation)
143: return "Aggregation";
144: if (pe instanceof Expansion)
145: return "Expansion";
146: if (pe instanceof AssetTransfer)
147: return "AssetTransfer";
148: return pe.getClass().getName();
149: case 2: // The failed status
150: if (ar == null)
151: return "Unknown";
152: if (ar.isSuccess())
153: return "Succeeded";
154: return "Failed";
155: case 3: // The confidence rating
156: if (ar == null)
157: return "Unknown";
158: return confidenceFormat.format(ar
159: .getConfidenceRating());
160: }
161: return "???";
162: }
163: }
164: }
165:
166: /** Display Task Results **/
167: MyTableModel tableModel = new MyTableModel();
168: JTable taskList = new JTable(tableModel);
169: JLabel allocCountLabel = new JLabel(" 0 failed tasks");
170: JScrollPane scrollPane = new JScrollPane(taskList,
171: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
172: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
173:
174: /** Subscription to hold collection of input tasks **/
175: private IncrementalSubscription myalloc;
176:
177: /** Look for tasks **/
178: private static UnaryPredicate allocPred() {
179: return new UnaryPredicate() {
180: public boolean execute(Object o) {
181: return (o instanceof Task || o instanceof PlanElement);
182: }
183: };
184: }
185:
186: /** GUI to display failed task info **/
187: private void createGUI() {
188: frame = new JFrame("TaskAssessorPlugin for "
189: + getMessageAddress());
190: frame.setLocation(0, 160);
191: Container panel = frame.getContentPane();
192: panel.add(allocCountLabel, BorderLayout.NORTH);
193: panel.add(scrollPane, BorderLayout.CENTER);
194: frame.pack();
195: frame.setVisible(true);
196: }
197:
198: /**
199: * Overrides the setupSubscriptions() in the SimplePlugin.
200: */
201: protected void setupSubscriptions() {
202: getSubscriber().setShouldBePersisted(false);
203: myalloc = (IncrementalSubscription) subscribe(allocPred());
204: createGUI();
205: }
206:
207: /** Need this for creating new instances of certain objects **/
208: private void updateGUI() {
209: }
210:
211: private void checkAdd(Enumeration en) {
212: while (en.hasMoreElements()) {
213: Object object = en.nextElement();
214: if (object instanceof Task) {
215: Task task = (Task) object;
216: tasks.add(task);
217: }
218: tableModel.fireTableDataChanged();
219: }
220: }
221:
222: private void checkChange(Enumeration en) {
223: checkAdd(en);
224: }
225:
226: private void checkRemove(Enumeration en) {
227: while (en.hasMoreElements()) {
228: Object object = en.nextElement();
229: if (object instanceof Task) {
230: if (tasks.contains(object)) {
231: tasks.remove(object);
232: }
233: }
234: tableModel.fireTableDataChanged();
235: }
236: }
237:
238: /** This will be called every time a task matches the above predicate
239: * Note: Failed Tasks only come through on the changed list.
240: * Since Tasks are changed by other Plugins after we see them
241: * here, we need to keep track of the ones we've seen so we don't
242: * act on them more than once.
243: **/
244: public void execute() {
245: checkAdd(myalloc.getAddedList());
246: checkChange(myalloc.getChangedList());
247: checkRemove(myalloc.getRemovedList());
248: }
249: }
|