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.examples;
028:
029: import java.awt.FlowLayout;
030: import java.awt.Label;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.util.ArrayList;
034: import java.util.Calendar;
035: import java.util.Date;
036: import java.util.Enumeration;
037: import java.util.List;
038:
039: import javax.swing.JButton;
040: import javax.swing.JFrame;
041: import javax.swing.JPanel;
042:
043: import org.cougaar.core.blackboard.IncrementalSubscription;
044: import org.cougaar.glm.ldm.Constants;
045: import org.cougaar.planning.ldm.PlanningFactory;
046: import org.cougaar.planning.ldm.plan.AllocationResult;
047: import org.cougaar.planning.ldm.plan.AspectType;
048: import org.cougaar.planning.ldm.plan.AspectValue;
049: import org.cougaar.planning.ldm.plan.BulkEstimate;
050: import org.cougaar.planning.ldm.plan.NewTask;
051: import org.cougaar.planning.ldm.plan.Preference;
052: import org.cougaar.planning.ldm.plan.ScoringFunction;
053: import org.cougaar.planning.ldm.plan.TimeAspectValue;
054: import org.cougaar.planning.plugin.legacy.SimplePlugin;
055: import org.cougaar.util.UnaryPredicate;
056:
057: /**
058: * The bulkestimate tester
059: *
060: *
061: */
062:
063: public class BulkEstimateTester extends SimplePlugin {
064: /** frame for 1-button UI **/
065: static JFrame frame;
066:
067: Label BELabel;
068:
069: protected JButton beButton;
070:
071: private IncrementalSubscription bulkests;
072:
073: // Have to provide these on this plugin class, else the inner class
074: // below will not be able to find them
075: public void openTheTransaction() {
076: openTransaction();
077: }
078:
079: public void closeTheTransaction(boolean b) {
080: closeTransaction(b);
081: }
082:
083: /** An ActionListener that listens to the GLS buttons. */
084: class BEButtonListener implements ActionListener {
085: public void actionPerformed(ActionEvent e) {
086: String lnfName = e.getActionCommand();
087: try {
088: openTheTransaction();
089: System.out.println("Creating BulkEstimates...");
090: createBulkEstimates();
091: closeTheTransaction(false);
092: BELabel.setText("sent bulk estimates");
093: } catch (Exception exc) {
094: JButton button = (JButton) e.getSource();
095: button.setEnabled(false);
096: System.err.println("Could not execute BE button: "
097: + lnfName);
098: }
099: }
100: }
101:
102: private void createGUI() {
103: frame = new JFrame("BulkEstimatePlugin");
104: frame.setLocation(0, 80);
105: frame.getContentPane().setLayout(new FlowLayout());
106: JPanel panel = new JPanel();
107: // Create the button
108: beButton = new JButton("Create BulkEstimate Objects");
109: beButton.setEnabled(true);
110: // Create a label for feedback on if the root task was sent
111: BELabel = new Label(
112: " ");
113: // Register a listener for the check box
114: BEButtonListener myBEListener = new BEButtonListener();
115: beButton.addActionListener(myBEListener);
116: panel.add(beButton);
117: panel.add(BELabel);
118: frame.getContentPane().add("Center", panel);
119: frame.pack();
120: frame.setVisible(true);
121: }
122:
123: protected void setupSubscriptions() {
124: bulkests = (IncrementalSubscription) subscribe(bepred());
125: createGUI();
126: }
127:
128: public synchronized void execute() {
129: if (bulkests.hasChanged()) {
130: Enumeration bechanges = bulkests.getChangedList();
131: while (bechanges.hasMoreElements()) {
132: BulkEstimate completebe = (BulkEstimate) bechanges
133: .nextElement();
134: printBulkEstimate(completebe);
135: }
136: }
137: }
138:
139: private void createBulkEstimates() {
140: PlanningFactory factory = getFactory();
141: NewTask ntask = factory.newTask();
142: // fill in some of the task since we are only using it for testing
143: // if it were a real task we would need to fill in more.
144: ntask.setVerb(Constants.Verb.Transport);
145: ntask.setPlan(factory.getRealityPlan());
146:
147: // create some AspectValues
148: Calendar now = Calendar.getInstance();
149: AspectValue time1 = TimeAspectValue.create(
150: AspectType.START_TIME, now.getTime());
151: now.add(Calendar.DATE, 5);
152: AspectValue time2 = TimeAspectValue.create(
153: AspectType.START_TIME, now.getTime());
154: now.add(Calendar.DATE, 5);
155: AspectValue time3 = TimeAspectValue.create(
156: AspectType.START_TIME, now.getTime());
157: now.add(Calendar.DATE, 5);
158: AspectValue time4 = TimeAspectValue.create(AspectType.END_TIME,
159: now.getTime());
160: now.add(Calendar.DATE, 5);
161: AspectValue time5 = TimeAspectValue.create(AspectType.END_TIME,
162: now.getTime());
163: now.add(Calendar.DATE, 5);
164: AspectValue time6 = TimeAspectValue.create(AspectType.END_TIME,
165: now.getTime());
166:
167: ScoringFunction sf1 = ScoringFunction
168: .createStrictlyAtValue(time1);
169: Preference pref1 = factory.newPreference(AspectType.START_TIME,
170: sf1);
171: ScoringFunction sf2 = ScoringFunction
172: .createStrictlyAtValue(time4);
173: Preference pref2 = factory.newPreference(AspectType.END_TIME,
174: sf2);
175: Preference[] prefset1 = { pref1, pref2 };
176:
177: ScoringFunction sf3 = ScoringFunction
178: .createStrictlyAtValue(time2);
179: Preference pref3 = factory.newPreference(AspectType.START_TIME,
180: sf3);
181: ScoringFunction sf4 = ScoringFunction
182: .createStrictlyAtValue(time5);
183: Preference pref4 = factory.newPreference(AspectType.END_TIME,
184: sf4);
185: Preference[] prefset2 = { pref3, pref4 };
186:
187: ScoringFunction sf5 = ScoringFunction
188: .createStrictlyAtValue(time3);
189: Preference pref5 = factory.newPreference(AspectType.START_TIME,
190: sf5);
191: ScoringFunction sf6 = ScoringFunction
192: .createStrictlyAtValue(time6);
193: Preference pref6 = factory.newPreference(AspectType.END_TIME,
194: sf6);
195: Preference[] prefset3 = { pref5, pref6 };
196:
197: List prefsets = new ArrayList();
198: prefsets.add(prefset1);
199: prefsets.add(prefset2);
200: prefsets.add(prefset3);
201:
202: // create the bulkestimate object by passing in the tasks, a collection
203: // of preference sets and the confidence rating you want the allocation
204: // result to reach.
205: BulkEstimate newbe = factory.newBulkEstimate(ntask, prefsets,
206: 1.0);
207:
208: // publish the BulkEstimate object
209: publishAdd(newbe);
210:
211: }
212:
213: // print utility to see what we get back.
214: private void printBulkEstimate(BulkEstimate compbe) {
215: AllocationResult[] results = compbe.getAllocationResults();
216: for (int x = 0; x < results.length; x++) {
217: AllocationResult anar = results[x];
218: System.out.println("\n\n AllocationResult: ");
219: AspectValue[] myresults = anar.getAspectValueResults();
220: for (int i = 0; i < myresults.length; i++) {
221: AspectValue anav = myresults[i];
222: if ((anav.getAspectType() == 0)
223: || (anav.getAspectType() == 1)) {
224: Date ndate = new Date((long) anav.getValue());
225: System.out.println("AspectValue type: "
226: + anav.getAspectType()
227: + " AspectValue value: "
228: + ndate.toString());
229: } else {
230: System.out.println("AspectValue type: "
231: + anav.getAspectType()
232: + " AspectValue value: "
233: + anav.getValue());
234: }
235: }
236: }
237: }
238:
239: // Predicate for getting all BulkEstimate objects that are complete.
240: // Since we know we are the only ones using this, its ours. If this was
241: // in a realy plugin in a realy society we would probably want to
242: // create a tighter predicate.
243: private static UnaryPredicate bepred() {
244: return new UnaryPredicate() {
245: public boolean execute(Object o) {
246: if (o instanceof BulkEstimate) {
247: return ((BulkEstimate) o).isComplete();
248: }
249: return false;
250: }
251: };
252: }
253: }
|