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.asset.Asset;
036: import org.cougaar.planning.ldm.plan.AllocationResult;
037: import org.cougaar.planning.ldm.plan.Expansion;
038: import org.cougaar.planning.ldm.plan.NewTask;
039: import org.cougaar.planning.ldm.plan.NewWorkflow;
040: import org.cougaar.planning.ldm.plan.PlanElement;
041: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
042: import org.cougaar.planning.ldm.plan.Task;
043: import org.cougaar.planning.ldm.plan.Verb;
044: import org.cougaar.planning.ldm.plan.Workflow;
045: import org.cougaar.planning.plugin.legacy.SimplePlugin;
046: import org.cougaar.util.UnaryPredicate;
047:
048: /**
049: * The StrategicTransportExpanderPlugin will deal with tasks to provide
050: * strategic transport for other supported clusters.
051: *
052: * Current functionality (MB3.0) allows only for pass-through
053: * of Tasks through a Workflow.
054: *
055: *
056: *
057: */
058:
059: public class StrategicTransportExpanderPlugin extends SimplePlugin {
060: private PlanningFactory ldmf;
061:
062: /** IncrementalSubscription to hold collection of input tasks **/
063: private IncrementalSubscription expandableTasks;
064: private IncrementalSubscription myExpansions;
065: private IncrementalSubscription changedTasks;
066:
067: /** Predicate for dealing with Expandable Tasks of strategic transport */
068: private static UnaryPredicate expTaskPred() {
069: return new UnaryPredicate() {
070: public boolean execute(Object o) {
071: if (o instanceof Task) {
072: Verb verb = ((Task) o).getVerb();
073: if ((((Task) o).getWorkflow() == null)
074: && (((Task) o).getPlanElement() == null)
075: && (verb.toString()
076: .equals(Constants.Verb.TRANSPORT))) {
077: Enumeration epp = ((Task) o)
078: .getPrepositionalPhrases();
079: while (epp.hasMoreElements()) {
080: PrepositionalPhrase pp = (PrepositionalPhrase) epp
081: .nextElement();
082: if ((pp.getPreposition()
083: .equals(Constants.Preposition.OFTYPE))
084: && (pp.getIndirectObject() instanceof Asset)) {
085: String io = null;
086: try {
087: io = ((Asset) pp
088: .getIndirectObject())
089: .getTypeIdentificationPG()
090: .getTypeIdentification();
091: } catch (Exception excep) {
092: System.err
093: .println("error in StratTransExp predicate trying to access the typeID of an IO");
094: excep.printStackTrace();
095: }
096: if (io
097: .equals("StrategicTransportation")) {
098: return true;
099: }
100: }
101: }
102: }
103: }
104: return false;
105: }
106: };
107: }
108:
109: private static UnaryPredicate myExpansionsPred() {
110: return new UnaryPredicate() {
111: public boolean execute(Object o) {
112: if (o instanceof Expansion) {
113: Workflow wf = ((Expansion) o).getWorkflow();
114: Enumeration wfTasks = wf.getTasks();
115: while (wfTasks.hasMoreElements()) {
116: Task t = (Task) wfTasks.nextElement();
117: Verb verb = ((Task) t).getVerb();
118: if (verb.toString().equals(
119: Constants.Verb.TRANSPORT)) {
120: Enumeration epp = ((Task) t)
121: .getPrepositionalPhrases();
122: while (epp.hasMoreElements()) {
123: PrepositionalPhrase pp = (PrepositionalPhrase) epp
124: .nextElement();
125: if ((pp.getPreposition()
126: .equals(Constants.Preposition.OFTYPE))
127: && (pp.getIndirectObject() instanceof Asset)) {
128: String io = null;
129: try {
130: io = ((Asset) pp
131: .getIndirectObject())
132: .getTypeIdentificationPG()
133: .getTypeIdentification();
134: } catch (Exception excep) {
135: System.err
136: .println("error in StratTransExp predicate trying to access the typeID of an IO");
137: excep.printStackTrace();
138: }
139: if (io
140: .equals("StrategicTransportation")) {
141: return true;
142: }
143: }
144: }
145: }
146: }
147: }
148: return false;
149: }
150: };
151: }
152:
153: private static UnaryPredicate changedTaskPred() {
154: return new UnaryPredicate() {
155: public boolean execute(Object o) {
156: if (o instanceof Task) {
157: Verb verb = ((Task) o).getVerb();
158: if ((verb.equals(Constants.Verb.TRANSPORT))
159: && (((Task) o).getPlanElement() instanceof Expansion))
160: return true;
161: }
162: return false;
163: }
164: };
165: }
166:
167: protected void setupSubscriptions() {
168: expandableTasks = (IncrementalSubscription) subscribe(expTaskPred());
169: myExpansions = (IncrementalSubscription) subscribe(myExpansionsPred());
170: changedTasks = (IncrementalSubscription) subscribe(changedTaskPred());
171: }
172:
173: protected void execute() {
174: if (expandableTasks.hasChanged()) {
175: Enumeration newtasks = expandableTasks.getAddedList();
176: while (newtasks.hasMoreElements()) {
177: Task currentTask = (Task) newtasks.nextElement();
178: expand(currentTask);
179: }
180: }
181:
182: if (myExpansions.hasChanged()) {
183: Enumeration changedexps = myExpansions.getChangedList();
184: while (changedexps.hasMoreElements()) {
185: PlanElement cpe = (PlanElement) changedexps
186: .nextElement();
187: updateAllocationResult(cpe);
188: }
189: }
190:
191: if (changedTasks.hasChanged()) {
192: Enumeration chtasks = changedTasks.getChangedList();
193: while (chtasks.hasMoreElements()) {
194: Task task = (Task) chtasks.nextElement();
195: updatePreferences(task);
196: }
197: }
198:
199: }
200:
201: public void expand(Task task) {
202: NewTask subtask = doExpansion(task);
203: Workflow wf = buildWorkflow(task, subtask);
204: PlanElement pe = buildPlanElement(wf);
205: publishAdd(pe);
206: }
207:
208: /**
209: * Create the (currently only one - MB3.0) subtasks resulting from the
210: * given parent Task.
211: */
212: private NewTask doExpansion(Task task) {
213:
214: MessageAddress me = getMessageAddress();
215: NewTask subtask = ldmf.newTask();
216:
217: // Create copy of parent Task
218: subtask.setParentTask(task);
219: subtask.setDirectObject(task.getDirectObject());
220: subtask.setPrepositionalPhrases(task.getPrepositionalPhrases());
221: subtask.setVerb(task.getVerb());
222: subtask.setPlan(task.getPlan());
223: //use the same preferences of the parent task for now.
224: subtask.setPreferences(task.getPreferences());
225: subtask.setSource(me);
226:
227: return subtask;
228:
229: }
230:
231: /**
232: * Create a new Workflow from the subtask(s)
233: */
234: private Workflow buildWorkflow(Task parent, NewTask subtask) {
235:
236: NewWorkflow wf = ldmf.newWorkflow();
237: wf.setIsPropagatingToSubtasks(true);
238: wf.setParentTask(parent); // subtask.getParentTask()
239: subtask.setWorkflow(wf);
240: wf.addTask(subtask);
241: //publishAdd(subtask);
242: return wf;
243:
244: }
245:
246: /**
247: * Create new PlanElement from Workflow
248: */
249: private PlanElement buildPlanElement(Workflow wf) {
250: Task t = wf.getParentTask();
251: PlanElement pe = ldmf.createExpansion(t.getPlan(), t, wf, null);
252: return pe;
253:
254: }
255:
256: private void updateAllocationResult(PlanElement cpe) {
257: if (cpe.getReportedResult() != null) {
258: // compare the allocationresult objects
259: // If they are not == re-set the estimated slot.
260: // For now don't worry about the equality of the composition of the result objects.
261: AllocationResult reportedresult = cpe.getReportedResult();
262: AllocationResult estimatedresult = cpe.getEstimatedResult();
263: if ((estimatedresult == null)
264: || (!(estimatedresult == reportedresult))) {
265: cpe.setEstimatedResult(reportedresult);
266: publishChange(cpe);
267: }
268: }
269: }
270:
271: // N.B. if this Plugin was running in a more complex society, we may want to
272: // incorporate some thread-safe code here...
273: private void updatePreferences(Task task) {
274: // We know a few things about this Task from the predicate that caught it;
275: // for instance, we know it has an Expansion as its PE, therefore a Workflow, etc, etc;
276: Workflow wf = ((Expansion) task.getPlanElement()).getWorkflow();
277: Enumeration subtasks = wf.getTasks();
278: while (subtasks.hasMoreElements()) {
279: Task subtask = (Task) subtasks.nextElement();
280: ((NewTask) subtask).setPreferences(task.getPreferences());
281: publishChange(subtask);
282: }
283: }
284:
285: public static void main(String argv[]) {
286: StrategicTransportExpanderPlugin testplugin = new StrategicTransportExpanderPlugin();
287:
288: }
289:
290: }
|