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.organization;
028:
029: import java.util.Calendar;
030: import java.util.Collection;
031: import java.util.Date;
032: import java.util.Enumeration;
033: import java.util.Iterator;
034: import java.util.Vector;
035:
036: import org.cougaar.core.blackboard.IncrementalSubscription;
037: import org.cougaar.core.util.UID;
038: import org.cougaar.glm.ldm.Constants;
039: import org.cougaar.glm.ldm.asset.Organization;
040: import org.cougaar.glm.ldm.oplan.Oplan;
041: import org.cougaar.mlm.plugin.RandomButtonPusher;
042: import org.cougaar.mlm.plugin.UICoordinator;
043: import org.cougaar.planning.ldm.plan.AspectType;
044: import org.cougaar.planning.ldm.plan.AspectValue;
045: import org.cougaar.planning.ldm.plan.ContextOfUIDs;
046: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
047: import org.cougaar.planning.ldm.plan.NewTask;
048: import org.cougaar.planning.ldm.plan.Preference;
049: import org.cougaar.planning.ldm.plan.ScoringFunction;
050: import org.cougaar.planning.ldm.plan.TimeAspectValue;
051: import org.cougaar.util.UnaryPredicate;
052:
053: /**
054: * The GLSGUIInitPlugin will create the initial GetLogSupport task
055: * with the OPLAN object.
056: *
057: */
058: public class GLSGUIInitPlugin extends GLSGUIBasePlugin {
059: private static final String MIN_SLEEP_PROP = "org.cougaar.mlm.plugin.organization.GLSGUIInitPlugin.minSleepTime";
060: private static final String MAX_SLEEP_PROP = "org.cougaar.mlm.plugin.organization.GLSGUIInitPlugin.maxSleepTime";
061: private static final int MIN_SLEEP_DFLT = 60000;
062: private static final int MAX_SLEEP_DFLT = 180000;
063: private static final String ENABLED_PROP = "org.cougaar.mlm.plugin.organization.GLSGUIInitPlugin.enabled";
064: private static final String ENABLED_DFLT = "false";
065: private static final String HIDDEN_PROP = "org.cougaar.mlm.plugin.organization.GLSGUIInitPlugin.hidden";
066: private static final String HIDDEN_DFLT = "true";
067: private IncrementalSubscription sendGLSRootSubscription;
068:
069: private static UnaryPredicate sendGLSRootPredicate = new UnaryPredicate() {
070: public boolean execute(Object o) {
071: if (o instanceof java.lang.String)
072: return ((o.toString()).equalsIgnoreCase("sendGLSRoot"));
073: return false;
074: }
075: };
076:
077: /** My private state **/
078: private MyPrivateState myPrivateState;
079:
080: private IncrementalSubscription myPrivateStateSubscription;
081:
082: private static class MyPrivateState extends RandomButtonPusher {
083: int taskNumber = 0;
084: boolean hidden;
085:
086: MyPrivateState() {
087: super (Integer.getInteger(MIN_SLEEP_PROP, MIN_SLEEP_DFLT)
088: .intValue(), Integer.getInteger(MAX_SLEEP_PROP,
089: MAX_SLEEP_DFLT).intValue(), "true"
090: .equalsIgnoreCase(System.getProperty(ENABLED_PROP,
091: ENABLED_DFLT)));
092: hidden = "true".equalsIgnoreCase(System.getProperty(
093: HIDDEN_PROP, HIDDEN_DFLT));
094: }
095:
096: public boolean isHidden() {
097: return hidden;
098: }
099: }
100:
101: protected void sendPSP(OplanWrapper wrapper) {
102: if (selfOrgAsset == null) {
103: System.out
104: .println("\n\nGLSGUIInitPlugin HAVEN'T RECEIVED SELF ORG ASSET YET. TRY AGAIN LATER\n\n");
105: } else {
106: doPublishRootGLS(selfOrgAsset, wrapper.oplan);
107: }
108: }
109:
110: protected void buttonPushed(OplanWrapper wrapper) {
111: if (selfOrgAsset == null) {
112: System.out
113: .println("\n\nGLSGUIInitPlugin HAVEN'T RECEIVED SELF ORG ASSET YET. TRY AGAIN LATER\n\n");
114: } else {
115: publishRootGLS(selfOrgAsset, wrapper.oplan);
116: }
117: }
118:
119: protected boolean isPrivateStateOk() {
120: return myPrivateState != null;
121: }
122:
123: protected String getGLSLabelText(int nTasks) {
124: if (myPrivateState.taskNumber == 0) {
125: return "Ready to send GLS task";
126: } else {
127: return "Sent root GLS task " + myPrivateState.taskNumber;
128: }
129: }
130:
131: protected String getButtonText() {
132: return "Send GLS Root";
133: }
134:
135: protected String getGUITitle() {
136: return "GLSInitPlugin";
137: }
138:
139: protected void createSubscriptions() {
140: myPrivateStateSubscription = RandomButtonPusher.subscribe(
141: getDelegate(), MyPrivateState.class);
142: sendGLSRootSubscription = (IncrementalSubscription) subscribe(sendGLSRootPredicate);
143: }
144:
145: protected void restorePrivateState() {
146: handlePrivateState(myPrivateStateSubscription.elements());
147: }
148:
149: protected void createPrivateState() {
150: publishAdd(new MyPrivateState()); // Prime the pump
151: }
152:
153: /* This will be called every time a new task matches the above predicate */
154:
155: protected void handlePrivateState() {
156: if (myPrivateStateSubscription.hasChanged()) {
157: handlePrivateState(myPrivateStateSubscription
158: .getAddedList());
159: }
160: }
161:
162: private void handlePrivateState(Enumeration e) {
163: if (myPrivateState == null && e.hasMoreElements()) {
164: myPrivateState = (MyPrivateState) e.nextElement();
165: if (!myPrivateState.isHidden()) {
166: UICoordinator.layoutSecondRow(panel, myPrivateState
167: .init("Random Send", getDelegate(), glsButton));
168: }
169: checkButtonEnable();
170: }
171: }
172:
173: public void sendThePSP() {
174: OplanWrapper wrapper = (OplanWrapper) oplanCombo
175: .getSelectedItem();
176: if (wrapper != null)
177: sendPSP(wrapper);
178: }
179:
180: protected void additionalExecute() {
181:
182: Collection sendIt = sendGLSRootSubscription
183: .getAddedCollection();
184: if (sendIt != null && sendIt.size() > 0) {
185: for (Iterator iterator = sendIt.iterator(); iterator
186: .hasNext();) {
187: Object object = iterator.next();
188: getBlackboardService().publishRemove(object);
189: }
190: sendThePSP();
191: }
192:
193: }
194:
195: public void publishRootGLS(Organization me, Oplan oplan) {
196: openTransaction();
197: doPublishRootGLS(me, oplan);
198: closeTransactionDontReset();
199: }
200:
201: private void doPublishRootGLS(Organization me, Oplan oplan) {
202: NewTask task = theLDMF.newTask();
203: // ensure this is a root level task
204: task.setPlan(theLDMF.getRealityPlan());
205: task.setSource(this .getMessageAddress());
206: task.setDestination(this .getMessageAddress());
207:
208: // set prepositional phrases
209: Vector phrases = new Vector(3);
210: NewPrepositionalPhrase newpp;
211:
212: // Removed following as being redundant with the task's context
213: // newpp = theLDMF.newPrepositionalPhrase();
214: // newpp.setPreposition(Constants.Preposition.WITH);
215: // newpp.setIndirectObject(oplan);
216: // phrases.add(newpp);
217:
218: newpp = theLDMF.newPrepositionalPhrase();
219: newpp.setPreposition(Constants.Preposition.FOR);
220: newpp.setIndirectObject(me);
221: phrases.add(newpp);
222:
223: newpp = theLDMF.newPrepositionalPhrase();
224: newpp.setPreposition("ForRoot");
225: newpp
226: .setIndirectObject(new Integer(
227: ++myPrivateState.taskNumber));
228: publishChange(myPrivateState);
229:
230: phrases.add(newpp);
231:
232: task.setPrepositionalPhrases(phrases.elements());
233:
234: // verb
235: task.setVerb(Constants.Verb.GetLogSupport);
236:
237: // schedule
238: long startTime = currentTimeMillis();
239: long endTime;
240: Date endDay = oplan.getEndDay();
241: if (endDay != null) {
242: endTime = endDay.getTime();
243: } else {
244: Calendar cal = Calendar.getInstance();
245: cal.setTime(new Date(startTime));
246: // increment date by 3 MONTHs
247: cal.add(Calendar.MONTH, 3);
248: endTime = cal.getTime().getTime();
249: }
250:
251: AspectValue startTav = TimeAspectValue.create(
252: AspectType.START_TIME, startTime);
253: AspectValue endTav = TimeAspectValue.create(
254: AspectType.END_TIME, endTime);
255:
256: ScoringFunction myStartScoreFunc = ScoringFunction
257: .createStrictlyAtValue(startTav);
258: ScoringFunction myEndScoreFunc = ScoringFunction
259: .createStrictlyAtValue(endTav);
260:
261: Preference startPreference = theLDMF.newPreference(
262: AspectType.START_TIME, myStartScoreFunc);
263: Preference endPreference = theLDMF.newPreference(
264: AspectType.END_TIME, myEndScoreFunc);
265:
266: Vector preferenceVector = new Vector(2);
267: preferenceVector.addElement(startPreference);
268: preferenceVector.addElement(endPreference);
269:
270: task.setPreferences(preferenceVector.elements());
271:
272: // Set the context
273: try {
274: UID oplanUID = oplan.getUID();
275: ContextOfUIDs context = new ContextOfUIDs(oplanUID);
276: System.out.println("GLSGUIInitPlugin: Setting context to: "
277: + oplanUID);
278: task.setContext(context);
279: } catch (Exception ex) {
280: ex.printStackTrace();
281: }
282:
283: publishAdd(task);
284: System.out.println("\n"
285: + formatDate(System.currentTimeMillis())
286: + " Send Task: " + task);
287: }
288: }
|