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.Color;
030: import java.awt.GridBagConstraints;
031: import java.awt.GridBagLayout;
032: import java.awt.Insets;
033: import java.awt.LayoutManager;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036: import java.util.Enumeration;
037: import java.util.Vector;
038:
039: import javax.swing.JButton;
040: import javax.swing.JFrame;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043:
044: import org.cougaar.core.blackboard.IncrementalSubscription;
045: import org.cougaar.core.mts.MessageAddress;
046: import org.cougaar.glm.ldm.Constants;
047: import org.cougaar.glm.ldm.asset.Organization;
048: import org.cougaar.glm.ldm.oplan.Oplan;
049: import org.cougaar.planning.ldm.PlanningFactory;
050: import org.cougaar.planning.ldm.asset.AbstractAsset;
051: import org.cougaar.planning.ldm.asset.Asset;
052: import org.cougaar.planning.ldm.plan.AspectType;
053: import org.cougaar.planning.ldm.plan.AspectValue;
054: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
055: import org.cougaar.planning.ldm.plan.NewTask;
056: import org.cougaar.planning.ldm.plan.Preference;
057: import org.cougaar.planning.ldm.plan.ScoringFunction;
058: import org.cougaar.planning.ldm.plan.Task;
059: import org.cougaar.planning.ldm.plan.TimeAspectValue;
060: import org.cougaar.planning.ldm.plan.Verb;
061: import org.cougaar.planning.plugin.legacy.SimplePlugin;
062: import org.cougaar.util.UnaryPredicate;
063:
064: /**
065: * Source Cluster Start/Stop GUI for Strategic Transportation Tasks.
066: * <p>
067: * Based on plugins used in minitestconfig GLS*Plugin, especially
068: * GLSGUIPlugin and GLSExpanderPlugin. One could use a source configuration
069: * similar to minitestconfig, but that would require a multi-node source
070: * (since GLS is sent to subordinates, and the source shouldn't list itself
071: * as a subordinate) and extra plugins (Oplan forwarding).
072: * <p>
073: * For a source cluster all we want is to generate and remove our
074: * DetermineRequirements Task.
075: */
076:
077: public class StrategicTransportSourceGUIPlugin extends SimplePlugin {
078: /** frame for 1-button UI **/
079: static JFrame frame;
080:
081: /** for feedback to user on whether DetermineRequirements was successful **/
082: private JLabel statusLabel;
083:
084: /** Running task **/
085: Task sourceTask;
086:
087: /** Control buttons */
088: private JButton startButton;
089: private JButton stopButton;
090:
091: /** A panel to hold the GUI **/
092: JPanel panel = new JPanel((LayoutManager) null);
093:
094: /** Subscription to hold collection of input tasks **/
095: private IncrementalSubscription selfOrgsSub;
096: private IncrementalSubscription oplansSub;
097:
098: /** Need this for creating new instances of certain objects **/
099: PlanningFactory ldmf;
100:
101: /**
102: * Self Organization predicate.
103: **/
104: protected static UnaryPredicate newSelfOrgPred() {
105: return new UnaryPredicate() {
106: public boolean execute(Object o) {
107: if (o instanceof Organization) {
108: return ((Organization) o).isSelf();
109: }
110: return false;
111: }
112: };
113: }
114:
115: /**
116: * Oplan predicate.
117: **/
118: protected static UnaryPredicate newOplanPred() {
119: return new UnaryPredicate() {
120: public boolean execute(Object o) {
121: return (o instanceof Oplan);
122: }
123: };
124: }
125:
126: /** Lazy Accessor that caches a reference to the ClusterObjectFactory,
127: * provided I am in a state other than UNITIALIZED, UNLOADED, or LOADED.
128: * @return ClusterObjectFactory
129: */
130:
131: // Have to provide these on this plugin class, else the inner class
132: // below will not be able to find them
133: public void openTheTransaction() {
134: openTransaction();
135: }
136:
137: public void closeTheTransaction(boolean b) {
138: closeTransaction(b);
139: }
140:
141: public void setStatus(String s) {
142: setStatus(false, s);
143: }
144:
145: public void setStatus(boolean success, String s) {
146: statusLabel
147: .setForeground((success ? Color.darkGray : Color.red));
148: statusLabel.setText(s);
149: }
150:
151: /**
152: * An ActionListener that listens to the DetermineRequirementsTasks
153: * buttons.
154: */
155: class DRTListener implements ActionListener {
156: public void actionPerformed(ActionEvent e) {
157: JButton button = (JButton) e.getSource();
158:
159: Organization selfOrg;
160: Oplan oplan;
161: if ((selfOrg = getSelfOrg()) == null) {
162: setStatus("No self Organization yet.");
163: } else if ((oplan = getOplan()) == null) {
164: setStatus("No Oplan yet.");
165: } else {
166: try {
167: // Have to do this as within transaction boundary
168: openTheTransaction();
169: if (button == startButton) {
170: sourceTask = startSource(oplan, selfOrg);
171: } else {
172: stopSource(sourceTask);
173: }
174: closeTheTransaction(false);
175: if (button == startButton) {
176: setStatus(true, "Source Started");
177: startButton.setEnabled(false);
178: stopButton.setEnabled(true);
179: } else {
180: setStatus(true, "Source Stopped");
181: startButton.setEnabled(true);
182: stopButton.setEnabled(false);
183: }
184: } catch (Exception exc) {
185: setStatus("Failed: " + exc.getMessage());
186: System.err.println("Could not execute button: "
187: + e.getActionCommand());
188: }
189: }
190: }
191: }
192:
193: private void createGUI() {
194: // create buttons and labels
195: startButton = new JButton("Start Source");
196: stopButton = new JButton("Stop Source");
197: startButton.addActionListener(new DRTListener());
198: stopButton.addActionListener(new DRTListener());
199: stopButton.setEnabled(false);
200: statusLabel = new JLabel("< >");
201:
202: // do layout
203: frame = new JFrame("StrategicTransportSourceGUIPlugin");
204: frame.setLocation(0, 0);
205: GridBagLayout gbl = new GridBagLayout();
206: GridBagConstraints gbc = new GridBagConstraints();
207: panel.setLayout(gbl);
208: gbc.insets = new Insets(15, 15, 15, 15);
209: gbc.fill = GridBagConstraints.HORIZONTAL;
210: gbc.gridx = 0;
211: gbc.gridy = 0;
212: gbl.setConstraints(startButton, gbc);
213: panel.add(startButton);
214: gbc.gridx = 1;
215: gbl.setConstraints(stopButton, gbc);
216: panel.add(stopButton);
217: gbc.gridx = 0;
218: gbc.gridy = 1;
219: gbc.gridwidth = 2;
220: gbl.setConstraints(statusLabel, gbc);
221: panel.add(statusLabel);
222: frame.setContentPane(panel);
223: frame.pack();
224: frame.setVisible(true);
225: setStatus(true, "Ready");
226: }
227:
228: /**
229: * Overrides the setupSubscriptions() in the SimplePlugin.
230: */
231: protected void setupSubscriptions() {
232: ldmf = theLDMF;
233:
234: getSubscriber().setShouldBePersisted(false);
235:
236: selfOrgsSub = (IncrementalSubscription) subscribe(newSelfOrgPred());
237: oplansSub = (IncrementalSubscription) subscribe(newOplanPred());
238:
239: createGUI();
240: }
241:
242: public synchronized void execute() {
243: }
244:
245: protected Organization getSelfOrg() {
246: Enumeration eSelfOrgs = selfOrgsSub.elements();
247: if (eSelfOrgs.hasMoreElements()) {
248: Organization orgSelf = (Organization) eSelfOrgs
249: .nextElement();
250: return orgSelf;
251: }
252: return null;
253: }
254:
255: protected Oplan getOplan() {
256: Enumeration eOplans = oplansSub.elements();
257: if (eOplans.hasMoreElements()) {
258: Oplan oplan = (Oplan) eOplans.nextElement();
259: return oplan;
260: }
261: return null;
262: }
263:
264: protected Task startSource(Oplan oplan, Organization selfOrg) {
265: MessageAddress clusterID = this .getAgentIdentifier();
266:
267: NewTask task = ldmf.newTask();
268:
269: // Plan reality
270: task.setPlan(ldmf.getRealityPlan());
271:
272: // Source clusterID
273: task.setSource(clusterID);
274:
275: // Destination clusterID
276: task.setDestination(clusterID);
277:
278: // Phrases
279: Vector prepphrases = new Vector();
280: NewPrepositionalPhrase pp;
281:
282: // OfType StrategicTranportation
283: pp = ldmf.newPrepositionalPhrase();
284: pp.setPreposition(Constants.Preposition.OFTYPE);
285: AbstractAsset strans = null;
286: try {
287: PlanningFactory ldmfactory = getFactory();
288: Asset strans_proto = ldmfactory
289: .createPrototype(
290: Class
291: .forName("org.cougaar.planning.ldm.asset.AbstractAsset"),
292: "StrategicTransportation");
293: strans = (AbstractAsset) ldmfactory
294: .createInstance(strans_proto);
295: } catch (Exception exc) {
296: System.out
297: .println("Unable to create abstract strategictransport\n"
298: + exc);
299: }
300: pp.setIndirectObject(strans);
301: prepphrases.addElement(pp);
302:
303: // For self
304: pp = ldmf.newPrepositionalPhrase();
305: pp.setPreposition(Constants.Preposition.FOR);
306: pp.setIndirectObject(selfOrg);
307: prepphrases.addElement(pp);
308:
309: task.setPrepositionalPhrases(prepphrases.elements());
310:
311: // Verb DetermineRequirements
312: task.setVerb(Verb.get(Constants.Verb.DETERMINEREQUIREMENTS));
313:
314: // Preferences
315: task.setPreferences(getPreferences());
316:
317: publishAdd(task);
318:
319: return task;
320: }
321:
322: protected void stopSource(Task t) {
323: publishRemove(t);
324: }
325:
326: private final static long ONE_DAY = 24L * 60L * 60L * 1000L;
327: private final static long ONE_MONTH = 30L * ONE_DAY;
328:
329: protected Enumeration getPreferences() {
330: // schedule
331: long now = currentTimeMillis();
332: long startTime = now;
333: // increment date by 3 MONTHs
334: long endTime = now + 3 * ONE_MONTH;
335:
336: AspectValue startTav = TimeAspectValue.create(
337: AspectType.START_TIME, startTime);
338: AspectValue endTav = TimeAspectValue.create(
339: AspectType.END_TIME, endTime);
340:
341: ScoringFunction myStartScoreFunc = ScoringFunction
342: .createStrictlyAtValue(startTav);
343: ScoringFunction myEndScoreFunc = ScoringFunction
344: .createStrictlyAtValue(endTav);
345:
346: Preference startPreference = ldmf.newPreference(
347: AspectType.START_TIME, myStartScoreFunc);
348: Preference endPreference = ldmf.newPreference(
349: AspectType.END_TIME, myEndScoreFunc);
350:
351: Vector preferenceVector = new Vector(2);
352: preferenceVector.addElement(startPreference);
353: preferenceVector.addElement(endPreference);
354:
355: return preferenceVector.elements();
356: }
357: }
|