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.construction;
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.oplan.Oplan;
040: import org.cougaar.glm.ldm.oplan.OrgActivity;
041: import org.cougaar.planning.ldm.plan.AspectType;
042: import org.cougaar.planning.ldm.plan.AspectValue;
043: import org.cougaar.planning.ldm.plan.Expansion;
044: import org.cougaar.planning.ldm.plan.NewTask;
045: import org.cougaar.planning.ldm.plan.NewWorkflow;
046: import org.cougaar.planning.ldm.plan.Preference;
047: import org.cougaar.planning.ldm.plan.ScoringFunction;
048: import org.cougaar.planning.ldm.plan.Task;
049: import org.cougaar.planning.ldm.plan.Verb;
050: import org.cougaar.planning.plugin.legacy.SimplePlugin;
051: import org.cougaar.planning.plugin.util.ExpanderHelper;
052: import org.cougaar.planning.plugin.util.PluginHelper;
053: import org.cougaar.util.UnaryPredicate;
054:
055: /** ExpanderPlugin that takes Determine Requirements of Type construction tasks and expands them into predefined construction tasks.
056: *
057: **/
058:
059: public class DTConstructionExpanderPlugin extends SimplePlugin {
060:
061: private IncrementalSubscription interestingTasks;
062: private IncrementalSubscription myExpansions;
063: private IncrementalSubscription myOrgActivities;
064:
065: protected Date cday = null;
066:
067: public void setupSubscriptions() {
068: //System.out.println("In DTConstructionExpanderPlugin.setupSubscriptions");
069: myOrgActivities = (IncrementalSubscription) subscribe(orgActsPred());
070:
071: if (didRehydrate()) {
072: // Only need one
073: Object first = myOrgActivities.first();
074: if (first != null) {
075: getCDay((OrgActivity) first);
076: }
077: }
078: }
079:
080: // only setup the task subscriptions after we have a valid cday from the
081: // orgactivity.
082: private void setupTaskSubscriptions() {
083: myExpansions = (IncrementalSubscription) subscribe(expansionsPredicate());
084: interestingTasks = (IncrementalSubscription) subscribe(tasksPredicate());
085: }
086:
087: public void execute() {
088: //System.out.println("DTConstructionExpanderPlugin.execute");
089:
090: if (cday == null && myOrgActivities.hasChanged()) {
091: // Only need one
092: Object first = myOrgActivities.first();
093: if (first != null) {
094: getCDay((OrgActivity) first);
095: }
096: }
097: // We may still be waiting for the orgactivity
098: if (interestingTasks == null) {
099: return;
100: }
101:
102: for (Enumeration e = interestingTasks.getAddedList(); e
103: .hasMoreElements();) {
104: Task task = (Task) e.nextElement();
105: Collection subtasks = createSubTasks(task);
106: NewWorkflow wf = theLDMF.newWorkflow();
107: wf.setParentTask(task);
108: Iterator stiter = subtasks.iterator();
109: while (stiter.hasNext()) {
110: NewTask subtask = (NewTask) stiter.next();
111: subtask.setWorkflow(wf);
112: wf.addTask(subtask);
113: publishAdd(subtask);
114: }
115: // create the Expansion ...??? Do we want to provide an EstimatedAllocationResult ???
116: Expansion newexpansion = theLDMF.createExpansion(theLDMF
117: .getRealityPlan(), task, wf, null);
118: publishAdd(newexpansion);
119:
120: }
121:
122: for (Enumeration exp = myExpansions.getChangedList(); exp
123: .hasMoreElements();) {
124: Expansion myexp = (Expansion) exp.nextElement();
125: if (PluginHelper.updatePlanElement(myexp)) {
126: publishChange(myexp);
127: }
128: }
129: }
130:
131: private Collection createSubTasks(Task task) {
132:
133: Vector subtasks = new Vector();
134: Task apronsubtask = createSubTask(task,
135: ConstructionConstants.Verb.EXPANDRUNWAYAPRON, 10, 60);
136: Task shortrunway = createSubTask(task,
137: ConstructionConstants.Verb.BUILDSHORTRUNWAY, 20, 50);
138: Task tentcity = createSubTask(task,
139: ConstructionConstants.Verb.BUILDSMALLTENTCITY, 10, 60);
140: subtasks.add(apronsubtask);
141: subtasks.add(shortrunway);
142: subtasks.add(tentcity);
143:
144: //System.out.println("\n About to return: " + subtasks.size() + "Subtasks for Workflow");
145: return subtasks;
146: }
147:
148: /** Create a simple subtask
149: * @param parent - parent task
150: * @param st_name - new subtask's verb
151: * @param offset new subtask's offset time from C0 start time
152: * @param duration new subtask's duration
153: * @return Task - The new subtask
154: **/
155: private Task createSubTask(Task parent, String st_verb, int offset,
156: int duration) {
157: NewTask subtask = theLDMF.newTask();
158: subtask.setParentTask(parent);
159: subtask.setVerb(Verb.get(st_verb));
160: subtask.setPlan(parent.getPlan());
161: // use parent's prep phrases for now.
162: subtask.setPrepositionalPhrases(parent
163: .getPrepositionalPhrases());
164: //set up preferences - first find the start time of the parent task
165: Date childST = null;
166: Date childET = null;
167: Enumeration parentprefs = parent.getPreferences();
168: while (parentprefs.hasMoreElements()) {
169: Preference apref = (Preference) parentprefs.nextElement();
170: if (apref.getAspectType() == AspectType.START_TIME) {
171: Calendar cal = Calendar.getInstance();
172: cal.setTime(cday);
173: cal.add(Calendar.DATE, offset);
174: childST = cal.getTime();
175: cal.add(Calendar.DATE, duration);
176: childET = cal.getTime();
177: }
178: break;
179: }
180:
181: Vector prefs = new Vector();
182: AspectValue startAV = AspectValue.newAspectValue(
183: AspectType.START_TIME, childST.getTime());
184: ScoringFunction startSF = ScoringFunction
185: .createPreferredAtValue(startAV, 2);
186: Preference startPref = theLDMF.newPreference(
187: AspectType.START_TIME, startSF);
188: prefs.addElement(startPref);
189: AspectValue endAV = AspectValue.newAspectValue(
190: AspectType.END_TIME, childET.getTime());
191: ScoringFunction endSF = ScoringFunction.createPreferredAtValue(
192: endAV, 2);
193: Preference endPref = theLDMF.newPreference(AspectType.END_TIME,
194: endSF);
195: prefs.addElement(endPref);
196:
197: subtask.setPreferences(prefs.elements());
198:
199: return subtask;
200: }
201:
202: private void getCDay(OrgActivity orgact) {
203: Collection oplanCol = query(new OplanByUIDPred(orgact
204: .getOplanUID()));
205: // Should only be one
206: Oplan oplan = (Oplan) oplanCol.iterator().next();
207: this .cday = new Date(oplan.getCday().getTime());
208: if (interestingTasks == null) {
209: setupTaskSubscriptions();
210: }
211: }
212:
213: // predicates
214: protected static UnaryPredicate expansionsPredicate() {
215: return new UnaryPredicate() {
216: public boolean execute(Object o) {
217: if (o instanceof Expansion) {
218: Task task = ((Expansion) o).getTask();
219: if (Constants.Verb.DetermineRequirements
220: .equals(task.getVerb())) {
221: return true;
222: }
223: }
224: return false;
225: }
226: };
227: }
228:
229: /**
230: * Predicate to find a specific Oplan by UID
231: **/
232: protected static class OplanByUIDPred implements UnaryPredicate {
233: UID oplanUID;
234:
235: OplanByUIDPred(UID uid) {
236: oplanUID = uid;
237: }
238:
239: public boolean execute(Object o) {
240: if (o instanceof Oplan) {
241: if (oplanUID.equals(((Oplan) o).getUID())) {
242: return true;
243: }
244: }
245: return false;
246: }
247: }
248:
249: protected static UnaryPredicate tasksPredicate() {
250: return new UnaryPredicate() {
251: public boolean execute(Object o) {
252: if (o instanceof Task) {
253: Task t = (Task) o;
254: if (Constants.Verb.DetermineRequirements.equals(t
255: .getVerb())) {
256: return (ExpanderHelper.isOfType(t,
257: Constants.Preposition.OFTYPE,
258: "Construction"));
259: }
260: }
261: return false;
262: }
263: };
264: }
265:
266: protected static UnaryPredicate orgActsPred() {
267: return new UnaryPredicate() {
268: public boolean execute(Object o) {
269: return (o instanceof OrgActivity);
270: }
271: };
272: }
273:
274: }
|