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.strategictransport;
028:
029: import java.util.Enumeration;
030:
031: import org.cougaar.core.blackboard.IncrementalSubscription;
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.glm.ldm.Constants;
034: import org.cougaar.planning.ldm.PlanningFactory;
035: import org.cougaar.planning.ldm.plan.AllocationResult;
036: import org.cougaar.planning.ldm.plan.Expansion;
037: import org.cougaar.planning.ldm.plan.NewTask;
038: import org.cougaar.planning.ldm.plan.NewWorkflow;
039: import org.cougaar.planning.ldm.plan.PlanElement;
040: import org.cougaar.planning.ldm.plan.Task;
041: import org.cougaar.planning.ldm.plan.Workflow;
042: import org.cougaar.planning.plugin.legacy.SimplePlugin;
043: import org.cougaar.util.UnaryPredicate;
044:
045: public class TopsStubExpanderPlugin extends SimplePlugin {
046: PlanningFactory ldmf;
047: private IncrementalSubscription expandableTasks;
048: private IncrementalSubscription myExpansions;
049:
050: private static UnaryPredicate expTaskPred() {
051: return new UnaryPredicate() {
052: public boolean execute(Object o) {
053: if (o instanceof Task) {
054: Task t = (Task) o;
055: if ((t.getVerb().equals(Constants.Verb.TRANSPORT))
056: && (t.getWorkflow() == null)
057: && (t.getPlanElement() == null)) {
058: return true;
059: }
060: }
061: return false;
062: }
063: };
064: }
065:
066: //since this is a stub this plugin assumes that all the workflows which
067: //contain Transport tasks are created by itself and subscribes to the workflows
068: //containing verb:TRANSPORT
069: private static UnaryPredicate myExpansionsPred() {
070: return new UnaryPredicate() {
071: public boolean execute(Object o) {
072: if (o instanceof Expansion) {
073: Workflow wf = ((Expansion) o).getWorkflow();
074: Enumeration myTasks = wf.getTasks();
075: while (myTasks.hasMoreElements()) {
076: Task t = (Task) myTasks.nextElement();
077: if ((t.getVerb()
078: .equals(Constants.Verb.TRANSPORT))) {
079: return true;
080: }
081: }
082: }
083: return false;
084: }
085: };
086: }
087:
088: protected void setupSubscriptions() {
089: expandableTasks = (IncrementalSubscription) subscribe(expTaskPred());
090: myExpansions = (IncrementalSubscription) subscribe(myExpansionsPred());
091: }
092:
093: protected void execute() {
094: if (expandableTasks.hasChanged()) {
095: Enumeration newtasks = expandableTasks.getAddedList();
096: while (newtasks.hasMoreElements()) {
097: System.out
098: .println("%%%%%%%% TopsStubExpander: New TRANSPORT tasks received");
099: Task task = (Task) newtasks.nextElement();
100:
101: System.out.println("*****" + task.getVerb());
102: NewTask subtask = doExpansion(task);
103: Workflow wf = buildWorkflow(task, subtask);
104: PlanElement pe = buildPlanElement(wf);
105: publishAdd(pe);
106: System.out
107: .println("%%%%%%%% TopsStubExpander:Expansion complete");
108:
109: }
110: }
111:
112: if (myExpansions.hasChanged()) {
113: Enumeration changedExps = myExpansions.getChangedList();
114: while (changedExps.hasMoreElements()) {
115: updateAllocationResult((Expansion) changedExps
116: .nextElement());
117: }
118: }
119: }
120:
121: private NewTask doExpansion(Task task) {
122: MessageAddress me = this .getMessageAddress();
123: NewTask subtask = theLDMF.newTask();
124:
125: // Create copy of parent Task
126: subtask.setParentTask(task);
127: subtask.setDirectObject(task.getDirectObject());
128: subtask.setPrepositionalPhrases(task.getPrepositionalPhrases());
129: subtask.setVerb(task.getVerb());
130: subtask.setPlan(task.getPlan());
131: subtask.setPreferences(task.getPreferences());
132: subtask.setSource(me);
133: return subtask;
134: }
135:
136: /**
137: * Create a new Workflow from the subtask(s)
138: */
139: private Workflow buildWorkflow(Task task, NewTask subtask) {
140:
141: NewWorkflow wf = theLDMF.newWorkflow();
142: wf.setIsPropagatingToSubtasks(true);
143: wf.setParentTask(task); //subtask.getParentTask()
144: subtask.setWorkflow(wf);
145: wf.addTask(subtask);
146: //publishAdd(subtask);
147: return wf;
148: }
149:
150: /**
151: * Create new PlanElement from Workflow
152: */
153: private PlanElement buildPlanElement(Workflow wf) {
154: Task t = wf.getParentTask();
155: PlanElement pe = theLDMF.createExpansion(t.getPlan(), t, wf,
156: null);
157: return pe;
158: }
159:
160: private void updateAllocationResult(PlanElement cpe) {
161: if (cpe.getReportedResult() != null) {
162: // compare the allocationresult objects.
163: // If they are NOT ==, re-set the estimated result.
164: // For now, ignore whether the composition of the results are equal.
165: AllocationResult reportedresult = cpe.getReportedResult();
166: AllocationResult estimatedresult = cpe.getEstimatedResult();
167: if ((estimatedresult == null)
168: || (!(estimatedresult == reportedresult))) {
169: cpe.setEstimatedResult(reportedresult);
170: publishChange(cpe);
171: }
172: }
173: }
174: }
|