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