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.logistics.plugin.trans;
028:
029: import org.cougaar.planning.ldm.plan.Allocation;
030: import org.cougaar.planning.ldm.plan.NewTask;
031: import org.cougaar.planning.ldm.plan.PlanElement;
032: import org.cougaar.planning.ldm.plan.Relationship;
033: import org.cougaar.planning.ldm.plan.RelationshipSchedule;
034: import org.cougaar.planning.ldm.asset.ItemIdentificationPG;
035: import org.cougaar.planning.ldm.asset.NewItemIdentificationPG;
036: import org.cougaar.planning.ldm.plan.NewRoleSchedule;
037:
038: import org.cougaar.planning.ldm.plan.Role;
039: import org.cougaar.planning.ldm.plan.Task;
040:
041: import org.cougaar.util.ConfigFinder;
042: import org.cougaar.util.Filters;
043: import org.cougaar.util.UnaryPredicate;
044: import org.cougaar.util.TimeSpan;
045:
046: import java.awt.BorderLayout;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049:
050: import java.util.Collection;
051: import java.util.Enumeration;
052: import java.util.HashSet;
053: import java.util.Iterator;
054: import java.util.Vector;
055:
056: import javax.swing.JButton;
057: import javax.swing.JFrame;
058: import javax.swing.JPanel;
059: import javax.swing.JTextField;
060: import javax.swing.JLabel;
061:
062: import org.cougaar.logistics.ldm.Constants;
063: import org.cougaar.glm.ldm.asset.Organization;
064:
065: import org.cougaar.lib.callback.UTILAssetCallback;
066: import org.cougaar.lib.callback.UTILAssetListener;
067: import org.cougaar.glm.parser.GLMTaskParser;
068: import org.cougaar.glm.util.AssetUtil;
069: import org.cougaar.planning.ldm.LDMServesPlugin;
070: import org.cougaar.planning.ldm.asset.Asset;
071: import org.cougaar.glm.ldm.asset.PhysicalAsset;
072: import org.cougaar.planning.ldm.plan.Schedule;
073:
074: import org.cougaar.lib.filter.UTILPluginAdapter;
075:
076: import org.cougaar.lib.util.UTILAllocate;
077: import org.cougaar.lib.util.UTILPreference;
078: import org.cougaar.lib.util.UTILAsset;
079:
080: import java.util.Date;
081:
082: /**
083: * <pre>
084: * Parses an XML file defining tasks to send to other clusters.
085: * Creates tasks defined in the XML file and sends them
086: * to any subordinate, supporting, or provider clusters.
087: *
088: * Pops up a dialog box which provides a way to specify the
089: * XML file. This defaults to whatever is in the ClusterInputFile
090: * parameter.
091: *
092: * In addition to sending tasks, they can also be rescinded.
093: * Tasks that have been sent are rescinded one at a time, with
094: * each button press.
095: *
096: * Implements the org listener interface so it can get all
097: * reported orgs.
098: *
099: * (This code evolved from a version in the COUGAAR tree.)
100: * </pre>
101: */
102: public class AssetStimulator extends UTILPluginAdapter implements
103: UTILAssetListener {
104:
105: /** Add the filter for all organizations... */
106: public void setupFilters() {
107: super .setupFilters();
108:
109: if (isInfoEnabled())
110: info(getName() + " : Filtering for Physical Assets.");
111:
112: addFilter(myAssetCallback = new UTILAssetCallback(this , logger));
113: }
114:
115: public boolean interestingAsset(Asset a) {
116: return (a instanceof PhysicalAsset);
117: }
118:
119: /**
120: * needed to implement the Listener interface.
121: * Does nothing.
122: *
123: */
124: public void handleChangedAssets(Enumeration e) {
125: }
126:
127: /**
128: * needed to implement the Listener interface.
129: * Does nothing.
130: *
131: */
132: public void handleNewAssets(Enumeration e) {
133: }
134:
135: /**
136: * Actually create the GUI window. Two buttons, a text input box,
137: * and a status line.
138: *
139: * @param infile - default input file. From param
140: * "ClusterInputFile" defined in <ClusterName>.env.xml.
141: */
142: private void createGUI(String infile) {
143: frame = new JFrame(getClusterName() + " - AssetStimulator");
144: frame.getContentPane().setLayout(new BorderLayout());
145:
146: JPanel panel = new JPanel();
147: JButton button = new JButton("Add Asset");
148: JButton button2 = new JButton("Change Asset");
149: JLabel label = new JLabel(
150: " ");
151: JLabel label2 = new JLabel(
152: "Click add to add a new truck. Click change to change the item id of the first physical asset.");
153:
154: ActionListener myGLMListener = new ActionListener() {
155: public void actionPerformed(ActionEvent e) {
156: String lnfName = e.getActionCommand();
157:
158: if (lnfName.equals("Add Asset")) {
159: addAsset();
160: } else {
161: changeAsset();
162: }
163: }
164: };
165:
166: button.addActionListener(myGLMListener);
167: button2.addActionListener(myGLMListener);
168:
169: panel.add(button);
170: panel.add(button2);
171: frame.getRootPane().setDefaultButton(button); // hitting return sends the tasks
172:
173: frame.getContentPane().add("Center", panel);
174: frame.getContentPane().add("South", label);
175: frame.getContentPane().add("North", label2);
176: frame.pack();
177: frame.setVisible(true);
178: }
179:
180: void addAsset() {
181: try {
182: blackboard.openTransaction();
183: publishAdd(makeNewAsset(getLDMService().getLDM(),
184: "RAILCAR_68DODX", "brandNewAsset"));
185: } catch (Exception exc) {
186: error("Could not make asset.");
187: error(exc.getMessage());
188: exc.printStackTrace();
189: } finally {
190: blackboard.closeTransactionDontReset();
191: }
192: }
193:
194: void changeAsset() {
195: try {
196: blackboard.openTransaction();
197:
198: Collection collection = myAssetCallback.getSubscription()
199: .getCollection();
200:
201: Asset firstAsset = (Asset) collection.iterator().next();
202: ((NewItemIdentificationPG) firstAsset
203: .getItemIdentificationPG())
204: .setItemIdentification(firstAsset
205: .getItemIdentificationPG()
206: .getItemIdentification()
207: + "_changed");
208: publishChange(firstAsset);
209: } catch (Exception exc) {
210: error("Could not change asset.");
211: error(exc.getMessage());
212: exc.printStackTrace();
213: } finally {
214: blackboard.closeTransactionDontReset();
215: }
216: }
217:
218: protected Asset makeNewAsset(LDMServesPlugin ldm, String prototype,
219: String id) {
220: // Create the instance
221: UTILAsset assetHelper = new UTILAsset(logger);
222: Asset newAsset = assetHelper.createInstance(ldm, prototype, id);
223:
224: Date now = new Date();
225: Date then = new Date(now.getTime() - 24 * 60 * 60 * 1000);
226:
227: Schedule copySchedule = ldm.getFactory().newSimpleSchedule(
228: then, now);
229: // Set the Schedule
230: ((NewRoleSchedule) newAsset.getRoleSchedule())
231: .setAvailableSchedule(copySchedule);
232:
233: info("Making new asset " + newAsset + " with UID "
234: + newAsset.getUID());
235: return newAsset;
236: }
237:
238: /**
239: * Reads the ClusterInputFile parameter, brings up the GUI.
240: */
241: public void localSetup() {
242: String infile = null;
243: try {
244: infile = getMyParams().getStringParam("ClusterInputFile");
245: } catch (Exception e) {
246: infile = " ";
247: }
248: createGUI(infile);
249: }
250:
251: /** frame for 2-button UI */
252: JFrame frame;
253:
254: /** The callback mediating the org subscription */
255: protected UTILAssetCallback myAssetCallback;
256: }
|