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.assessor;
028:
029: import java.awt.LayoutManager;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.util.Collection;
033: import java.util.Iterator;
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:
041: import org.cougaar.core.blackboard.IncrementalSubscription;
042: import org.cougaar.core.plugin.ComponentPlugin;
043: import org.cougaar.core.util.UID;
044: import org.cougaar.glm.ldm.Constants;
045: import org.cougaar.glm.ldm.oplan.Oplan;
046: import org.cougaar.mlm.plugin.UICoordinator;
047: import org.cougaar.planning.ldm.PlanningFactory;
048: import org.cougaar.planning.ldm.plan.AspectType;
049: import org.cougaar.planning.ldm.plan.AspectValue;
050: import org.cougaar.planning.ldm.plan.ContextOfUIDs;
051: import org.cougaar.planning.ldm.plan.NewTask;
052: import org.cougaar.planning.ldm.plan.Preference;
053: import org.cougaar.planning.ldm.plan.ScoringFunction;
054: import org.cougaar.planning.ldm.plan.Task;
055: import org.cougaar.planning.service.LDMService;
056: import org.cougaar.util.UnaryPredicate;
057:
058: /**
059: * Publishes an AssessReadiness task to the logplan every time the button is pressed.
060: *
061: **/
062: public class InjectAssessReadinessGUIPlugin extends ComponentPlugin {
063: /** frame for UI **/
064: private JFrame frame;
065:
066: /** for feedback to user on whether root GLS was successful **/
067: JLabel label;
068:
069: protected JButton arButton;
070: protected JButton rescindButton;
071:
072: protected PlanningFactory rootFactory;
073: private long rollupSpan = 10;
074:
075: private IncrementalSubscription assessReadinessSubscription;
076: private static UnaryPredicate assessReadinessPredicate = new UnaryPredicate() {
077: public boolean execute(Object o) {
078: if (o instanceof Task) {
079: Task t = (Task) o;
080: if (t.getVerb().equals(Constants.Verb.AssessReadiness)) {
081: if (!t.getPrepositionalPhrases().hasMoreElements()) {
082: if (t.getPlanElement() == null) {
083: return true;
084: }
085: }
086: }
087: }
088: return false;
089: }
090: };
091:
092: private IncrementalSubscription oplanSubscription;
093: private static UnaryPredicate oplanPredicate = new UnaryPredicate() {
094: public boolean execute(Object o) {
095: if (o instanceof Oplan) {
096: return true;
097: }
098: return false;
099: }
100: };
101:
102: // called by introspection
103: public void setLDMService(LDMService service) {
104: rootFactory = service.getFactory();
105: }
106:
107: protected void setupSubscriptions() {
108: assessReadinessSubscription = (IncrementalSubscription) blackboard
109: .subscribe(assessReadinessPredicate);
110: oplanSubscription = (IncrementalSubscription) blackboard
111: .subscribe(oplanPredicate);
112:
113: // refill contributors Collection on rehydrate
114: processAdds(assessReadinessSubscription.getCollection());
115:
116: createGUI();
117: }
118:
119: /**
120: * Executes Plugin functionality.
121: */
122: protected void execute() {
123:
124: if (assessReadinessSubscription.hasChanged()) {
125: Collection adds = assessReadinessSubscription
126: .getAddedCollection();
127: if (adds != null) {
128: processAdds(adds);
129: }
130: Collection changes = assessReadinessSubscription
131: .getChangedCollection();
132: if (changes != null) {
133: processChanges(changes);
134: }
135: Collection deletes = assessReadinessSubscription
136: .getRemovedCollection();
137: if (deletes != null) {
138: processDeletes(deletes);
139: }
140: }
141:
142: if (oplanSubscription.hasChanged()) {
143: if (oplanSubscription.size() > 0)
144: if (!arButton.isEnabled())
145: arButton.setEnabled(true);
146: else
147: arButton.setEnabled(false);
148: }
149: }
150:
151: private long getOplanStartTime() {
152: // Yes, I know there can be more than one Oplan. I'll deal with it if I have time
153: Oplan oplan = (Oplan) oplanSubscription.iterator().next();
154: return oplan.getCday().getTime();
155: }
156:
157: private long getOplanEndTime() {
158: // Yes, I know there can be more than one Oplan. I'll deal with it if I have time
159: Oplan oplan = (Oplan) oplanSubscription.iterator().next();
160: return oplan.getEndDay().getTime();
161: }
162:
163: private UID getOplanUID() {
164: // Yes, I know there can be more than one Oplan. I'll deal with it if I have time
165: Oplan oplan = (Oplan) oplanSubscription.iterator().next();
166: return oplan.getUID();
167: }
168:
169: private void createGUI() {
170: frame = new JFrame("InjectAssessReadinessGUIPlugin for "
171: + getAgentIdentifier());
172: JPanel panel = new JPanel((LayoutManager) null);
173: // Create the button
174: arButton = new JButton("Publish AssessReadiness Task");
175: arButton.setEnabled(false);
176: label = new JLabel(
177: "No AssessReadiness task have been published.");
178: // Register a listener for the check box
179: ARButtonListener myARListener = new ARButtonListener();
180: arButton.addActionListener(myARListener);
181: UICoordinator.layoutButtonAndLabel(panel, arButton, label);
182:
183: rescindButton = new JButton("Rescind AssessReadiness Tasks");
184: rescindButton.setEnabled(false);
185: RescindButtonListener myRescindListener = new RescindButtonListener();
186: rescindButton.addActionListener(myRescindListener);
187: UICoordinator.layoutButton(panel, rescindButton);
188:
189: frame.setContentPane(panel);
190: frame.pack();
191: UICoordinator.setBounds(frame);
192: frame.setVisible(true);
193: }
194:
195: /** An ActionListener that listens to the GLS buttons. */
196: class ARButtonListener implements ActionListener {
197: public void actionPerformed(ActionEvent ae) {
198: rescindAssessReadiness();
199: publishAssessReadiness();
200: }
201: }
202:
203: private void updateLabel() {
204: int subSize = assessReadinessSubscription.size();
205: label.setText(subSize + " AssessReadiness tasks on blackboard");
206: if (subSize > 0)
207: rescindButton.setEnabled(true);
208: else
209: rescindButton.setEnabled(false);
210: }
211:
212: private void publishAssessReadiness() {
213: blackboard.openTransaction();
214: NewTask task = rootFactory.newTask();
215: task.setVerb(Constants.Verb.AssessReadiness);
216:
217: Vector prefs = new Vector(2);
218: Preference p = rootFactory.newPreference(AspectType.START_TIME,
219: ScoringFunction.createStrictlyAtValue(AspectValue
220: .newAspectValue(AspectType.START_TIME,
221: getOplanStartTime())));
222:
223: prefs.add(p);
224: p = rootFactory.newPreference(AspectType.END_TIME,
225: ScoringFunction.createStrictlyAtValue(AspectValue
226: .newAspectValue(AspectType.END_TIME,
227: getOplanEndTime())));
228:
229: prefs.add(p);
230: p = rootFactory.newPreference(AspectType.INTERVAL,
231: ScoringFunction
232: .createStrictlyAtValue(AspectValue
233: .newAspectValue(AspectType.INTERVAL,
234: rollupSpan)));
235: prefs.add(p);
236:
237: task.setPreferences(prefs.elements());
238: task.setContext(new ContextOfUIDs(getOplanUID()));
239: blackboard.publishAdd(task);
240: blackboard.closeTransactionDontReset();
241: updateLabel();
242: }
243:
244: /** An ActionListener that listens to the Rescind button. */
245: class RescindButtonListener implements ActionListener {
246: public void actionPerformed(ActionEvent ae) {
247: rescindAssessReadiness();
248: }
249: }
250:
251: private void rescindAssessReadiness() {
252: blackboard.openTransaction();
253: for (Iterator taskIt = assessReadinessSubscription.iterator(); taskIt
254: .hasNext();) {
255: Task task = (Task) taskIt.next();
256: blackboard.publishRemove(task);
257: }
258: blackboard.closeTransactionDontReset();
259: updateLabel();
260: }
261:
262: private void processAdds(Collection adds) {
263: for (Iterator it = adds.iterator(); it.hasNext();) {
264: Task t = (Task) it.next();
265: updateLabel();
266: }
267: }
268:
269: private void processChanges(Collection changes) {
270: }
271:
272: private void processDeletes(Collection deletes) {
273: for (Iterator it = deletes.iterator(); it.hasNext();) {
274: Task t = (Task) it.next();
275: updateLabel();
276: }
277: }
278:
279: // found by introspection
280: public void setParameter(Object param) {
281: System.out.println("InjectAssessReadiness.setParameter()");
282: for (Iterator paramIt = ((Collection) param).iterator(); paramIt
283: .hasNext();) {
284: String sParam = (String) paramIt.next();
285: int sep = sParam.indexOf('=');
286: if (sep > 0) {
287: String name = sParam.substring(0, sep).trim();
288: String val = sParam.substring(sep + 1).trim();
289: if (name.equalsIgnoreCase("rollupspan"))
290: rollupSpan = Long.parseLong(val);
291: else
292: System.out
293: .println("InjectAssessReadinessGUIPlugin.parseParameters() unexpected parameter "
294: + sParam);
295: }
296: }
297: }
298: }
|