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.FlowLayout;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.util.ArrayList;
033: import java.util.Arrays;
034: import java.util.Enumeration;
035: import java.util.List;
036: import java.util.ListIterator;
037:
038: import javax.swing.JButton;
039: import javax.swing.JFrame;
040: import javax.swing.JLabel;
041: import javax.swing.JPanel;
042:
043: import org.cougaar.core.blackboard.IncrementalSubscription;
044: import org.cougaar.glm.ldm.Constants;
045: import org.cougaar.glm.ldm.asset.CargoVehicle;
046: import org.cougaar.mlm.plugin.UICoordinator;
047: import org.cougaar.planning.ldm.PlanningFactory;
048: import org.cougaar.planning.ldm.asset.AbstractAsset;
049: import org.cougaar.planning.ldm.asset.Asset;
050: import org.cougaar.planning.ldm.plan.Allocation;
051: import org.cougaar.planning.ldm.plan.AllocationResult;
052: import org.cougaar.planning.ldm.plan.AspectType;
053: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
054: import org.cougaar.planning.ldm.plan.NewTask;
055: import org.cougaar.planning.ldm.plan.PlanElement;
056: import org.cougaar.planning.ldm.plan.RoleSchedule;
057: import org.cougaar.planning.ldm.plan.Task;
058: import org.cougaar.planning.ldm.plan.Verb;
059: import org.cougaar.planning.ldm.trigger.Trigger;
060: import org.cougaar.planning.ldm.trigger.TriggerAction;
061: import org.cougaar.planning.ldm.trigger.TriggerMonitor;
062: import org.cougaar.planning.ldm.trigger.TriggerPredicateBasedMonitor;
063: import org.cougaar.planning.plugin.legacy.PluginDelegate;
064: import org.cougaar.planning.plugin.legacy.SimplePlugin;
065: import org.cougaar.util.UnaryPredicate;
066:
067: public class MCCTriggerCreatorPlugin extends SimplePlugin {
068:
069: JFrame frame;
070:
071: /** Create a label for feedback on if the root task was sent **/
072: protected JLabel triggerLabel = new JLabel();
073:
074: private IncrementalSubscription myAssets;
075: private IncrementalSubscription maintAssets;
076: private ArrayList myVehicles = new ArrayList();
077: private static long ONE_DAY = 86400000L;
078: private AbstractAsset maintenanceAsset;
079:
080: //
081: // Begin inner class MCCTriggerAction
082: //
083: private static class MCCTriggerAction implements TriggerAction {
084: // Empty Constructor
085: MCCTriggerAction() {
086: }
087:
088: public void Perform(Object[] objects, PluginDelegate pid) {
089: PlanningFactory ldmf = pid.getFactory();
090: List al = Arrays.asList(objects);
091: ListIterator li = al.listIterator();
092: while (li.hasNext()) {
093: Object o = li.next();
094: if (o instanceof Allocation) {
095: Allocation alloc = (Allocation) o;
096: Asset asset = alloc.getAsset();
097: if (!(asset.getTypeIdentificationPG()
098: .getTypeIdentification()
099: .equals("MAINTENANCE"))) {
100: // This Allocation is conflicting with the new MAINTENANCE Allocation, so
101: // we want to rescind this Allocation and create a failed Disposition in its place
102: Task t = alloc.getTask();
103: pid.publishRemove(alloc);
104: AllocationResult ar = ldmf.newAllocationResult(
105: 0.0, false, new int[1], new double[1]);
106: PlanElement fa = ldmf.createFailedDisposition(
107: ldmf.getRealityPlan(), t, ar);
108: pid.publishAdd(fa);
109: }
110: }
111: }
112: }
113:
114: }
115:
116: //
117: // End inner class MCCTriggerAction
118: //
119:
120: //
121: // Begin inner class TriggerButtonListener
122: //
123: class TriggerButtonListener implements ActionListener {
124: public void actionPerformed(ActionEvent e) {
125: String ac = e.getActionCommand();
126: openTheTransaction();
127: tweakTrigger();
128: closeTheTransaction();
129: triggerLabel.setText("HETs in Maintenance");
130: }
131: }
132:
133: //
134: // End inner class TriggerButtonListener
135: //
136:
137: // Provide these methods for benefit of inner classes
138: public void openTheTransaction() {
139: openTransaction();
140: }
141:
142: public void closeTheTransaction() {
143: closeTransaction();
144: }
145:
146: PlanningFactory getLDMF() {
147: return theLDMF;
148: }
149:
150: private static UnaryPredicate triggPred() {
151: return new UnaryPredicate() {
152: public boolean execute(Object o) {
153: if (o instanceof Allocation) {
154: /*
155: // disable calling isPotentialConflict for now
156: if ( ((Allocation)o).isPotentialConflict() ) {
157: return true;
158: }
159: */
160: }
161: return false;
162: }
163: };
164: }
165:
166: // Predicate for our transportation Assets (ships, HETS, and the like)...
167: private static UnaryPredicate assetPred() {
168: return new UnaryPredicate() {
169: public boolean execute(Object o) {
170: return (o instanceof CargoVehicle);
171: }
172: };
173: }
174:
175: // Predicate to keep track of our (AbstractAsset) Maintenance orgs
176: private static UnaryPredicate maintAssetPred() {
177: return new UnaryPredicate() {
178: public boolean execute(Object o) {
179: if (o instanceof AbstractAsset) {
180: if (((AbstractAsset) o).getTypeIdentificationPG()
181: .getTypeIdentification().equals(
182: "MAINTENANCE"))
183: return true;
184: }
185: return false;
186: }
187: };
188: }
189:
190: private void createGUI() {
191: frame = new JFrame("MCCTriggerCreatorPlugin");
192: frame.getContentPane().setLayout(new FlowLayout());
193: JPanel panel = new JPanel();
194: // Create the button
195: JButton triggerButton = new JButton("Maintain HETs");
196:
197: // Register a listener for the radio buttons
198: TriggerButtonListener myTListener = new TriggerButtonListener();
199: triggerButton.addActionListener(myTListener);
200: UICoordinator.layoutButtonAndLabel(panel, triggerButton,
201: triggerLabel);
202: frame.setContentPane(panel);
203: frame.pack();
204: UICoordinator.setBounds(frame);
205: frame.setVisible(true);
206: }
207:
208: private boolean tweakTrigger() {
209: if (myVehicles.size() == 0)
210: return false;
211: ListIterator li = myVehicles.listIterator();
212: long now = currentTimeMillis();
213: while (li.hasNext()) {
214: Asset asset = (Asset) li.next();
215: RoleSchedule rs = asset.getRoleSchedule();
216: long startTime = now;
217: long endTime = now + ONE_DAY;
218: Task maintTask = createTask(asset);
219: int[] aspectarray = new int[2];
220: double[] resultsarray = new double[2];
221: aspectarray[0] = AspectType.START_TIME;
222: aspectarray[1] = AspectType.END_TIME;
223: resultsarray[0] = (double) startTime;
224: resultsarray[1] = (double) endTime;
225: AllocationResult ar = theLDMF.newAllocationResult(0.0,
226: true, aspectarray, resultsarray);
227: Allocation alloc = theLDMF.createAllocation(theLDMF
228: .getRealityPlan(), maintTask, asset, ar,
229: Constants.Role.MAINTAINER);
230: publishAdd(alloc);
231: }
232: return true;
233: }
234:
235: private Task createTask(Asset asset) {
236: NewTask nt = theLDMF.newTask();
237: nt.setPlan(theLDMF.getRealityPlan());
238: nt.setVerb(Verb.get(Constants.Verb.MAINTAIN));
239: nt.setDirectObject(asset);
240: NewPrepositionalPhrase npp = theLDMF.newPrepositionalPhrase();
241: npp.setPreposition(Constants.Preposition.USING);
242: npp.setIndirectObject(maintenanceAsset);
243: nt.setPrepositionalPhrases(npp);
244: publishAdd(nt);
245: return nt;
246: }
247:
248: private void checkAssets(Enumeration assets) {
249: while (assets.hasMoreElements()) {
250: Asset asset = (Asset) assets.nextElement();
251: addAsset(asset);
252: }
253: }
254:
255: private void addAsset(Asset asset) {
256: if (myVehicles.contains(asset))
257: return;
258: myVehicles.add(asset);
259: }
260:
261: private void removeAsset(Asset asset) {
262: if (!myVehicles.contains(asset))
263: return;
264: myVehicles.remove(asset);
265: }
266:
267: private void checkMaintAssets() {
268: if (maintenanceAsset == null) {
269: Enumeration tmp = maintAssets.getAddedList();
270: while (tmp.hasMoreElements()) {
271: // Really only expecting one AbstractAsset here
272: maintenanceAsset = (AbstractAsset) tmp.nextElement();
273: continue;
274: }
275: }
276: }
277:
278: public void setupSubscriptions() {
279: getSubscriber().setShouldBePersisted(false);
280: myAssets = (IncrementalSubscription) subscribe(assetPred());
281: maintAssets = (IncrementalSubscription) subscribe(maintAssetPred());
282: if (!didRehydrate()) {
283: createTriggers();
284: }
285: createGUI();
286: }
287:
288: /**
289: * Setup the Triggers and add them to the LogPlan
290: */
291: public void execute() {
292: if (maintAssets.hasChanged())
293: checkMaintAssets();
294: checkAssets(myAssets.getAddedList());
295: checkAssets(myAssets.getChangedList());
296: Enumeration e = myAssets.getRemovedList();
297: while (e.hasMoreElements()) {
298: removeAsset((Asset) e.nextElement());
299: }
300: }
301:
302: /**
303: * Create the Triggers
304: */
305: private void createTriggers() {
306: TriggerMonitor tm = new TriggerPredicateBasedMonitor(
307: triggPred());
308: // We don't need a Tester, as all objects in the Monitor will be
309: // elibible for action.
310: TriggerAction ta = new MCCTriggerAction();
311: Trigger myTrigger = new Trigger(tm, null, ta);
312: publishAdd(myTrigger);
313: }
314:
315: }
|