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.planning.plugin.util;
028:
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.cougaar.core.blackboard.IncrementalSubscription;
033: import org.cougaar.core.blackboard.Subscriber;
034: import org.cougaar.planning.ldm.PlanningFactory;
035: import org.cougaar.planning.ldm.asset.AbstractAsset;
036: import org.cougaar.planning.ldm.plan.AllocationResult;
037: import org.cougaar.planning.ldm.plan.AspectScorePoint;
038: import org.cougaar.planning.ldm.plan.Context;
039: import org.cougaar.planning.ldm.plan.Expansion;
040: import org.cougaar.planning.ldm.plan.NewTask;
041: import org.cougaar.planning.ldm.plan.NewWorkflow;
042: import org.cougaar.planning.ldm.plan.PlanElement;
043: import org.cougaar.planning.ldm.plan.Preference;
044: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
045: import org.cougaar.planning.ldm.plan.ScoringFunction;
046: import org.cougaar.planning.ldm.plan.Task;
047: import org.cougaar.planning.ldm.plan.Workflow;
048:
049: /**
050: * Provides utility methods for building Expander Plugins.
051: */
052: public class ExpanderHelper {
053:
054: /**
055: * Checks if the Task is of specified OFTYPE.
056: */
057: public static boolean isOfType(Task t, String p, String typeid) {
058: return typeid.equals(getOfType(t, p));
059: }
060:
061: public static String getOfType(Task t, String p) {
062: PrepositionalPhrase pPhrase = t.getPrepositionalPhrase(p);
063: if (pPhrase != null) {
064: Object indirectobj = pPhrase.getIndirectObject();
065: if (indirectobj instanceof AbstractAsset) {
066: AbstractAsset aa = (AbstractAsset) indirectobj;
067: return aa.getTypeIdentificationPG()
068: .getTypeIdentification();
069: }
070: }
071: return null;
072: }
073:
074: /**
075: * Takes "a" subtask, generates a workflow for that subtask. This newly created
076: * Expansion is wired properly and returned.
077: * @deprecated use PluginHelper.wireExpansion(Task parent, NewTask subTask, PlanningFactory ldmf) instead
078: */
079: public static Expansion wireExpansion(Task parent, NewTask subTask,
080: PlanningFactory ldmf) {
081:
082: NewWorkflow wf = ldmf.newWorkflow();
083:
084: Task t = parent;
085:
086: wf.setParentTask(t);
087: subTask.setWorkflow(wf);
088: wf.addTask(subTask);
089:
090: //End of creating NewWorkflow. Start creating an Expansion.
091: // pass in a null estimated allocationresult for now
092: Expansion exp = ldmf.createExpansion(t.getPlan(), t, wf, null);
093:
094: // Set the Context of the subTask to be that of the parent, unless it has already been set
095: if ((Task) subTask.getContext() == null) {
096: subTask.setContext(parent.getContext());
097: }
098:
099: return exp;
100: }
101:
102: /**
103: * Takes a Vector of subtasks, generates a workflow for those subtasks. This newly created
104: * Expansion is wired properly and returned.
105: * @deprecated use PluginHelper.wireExpansion(Task parentTask, Vector subTasks, PlanningFactory ldmf) instead.
106: */
107: public static Expansion wireExpansion(Vector subTasks,
108: PlanningFactory ldmf, Task parentTask, NewWorkflow wf) {
109: wf.setParentTask(parentTask);
110:
111: Context context = parentTask.getContext();
112: for (Enumeration esubTasks = subTasks.elements(); esubTasks
113: .hasMoreElements();) {
114: Task myTask = (Task) esubTasks.nextElement();
115: ((NewTask) myTask).setWorkflow((Workflow) wf);
116: wf.addTask(myTask);
117: // Set the Context of the subtask if it hasn't already been set
118: if (myTask.getContext() == null) {
119: ((NewTask) myTask).setContext(context);
120: }
121: }
122:
123: //End of creating NewWorkflow. Start creating an Expansion.
124: // pass in a null estimated allocationresult for now
125: Expansion exp = ldmf.createExpansion(parentTask.getPlan(),
126: parentTask, (Workflow) wf, null);
127:
128: return exp;
129: }
130:
131: /** Publish a new Expansion and its subtasks.
132: * e.g.
133: * publishAddExpansion(getSubscriber(), myExpansion);
134: * @deprecated use PluginHelper.publishAddExpansion(Subscriber sub, PlanElement exp) instead
135: **/
136: public static void publishAddExpansion(Subscriber sub,
137: PlanElement exp) {
138: sub.publishAdd(exp);
139:
140: for (Enumeration esubTasks = ((Expansion) exp).getWorkflow()
141: .getTasks(); esubTasks.hasMoreElements();) {
142: Task myTask = (Task) esubTasks.nextElement();
143: sub.publishAdd(myTask);
144: }
145: }
146:
147: /** Takes a subscription, gets the changed list and updates the changedList.
148: * @deprecated use PluginHelper.updateAllocationResult(IncrementalSubscription sub) instead
149: */
150: public static void updateAllocationResult(
151: IncrementalSubscription sub) {
152:
153: Enumeration changedPEs = sub.getChangedList();
154: while (changedPEs.hasMoreElements()) {
155: PlanElement pe = (PlanElement) changedPEs.nextElement();
156: if (pe.getReportedResult() != null) {
157: //compare entire pv arrays
158: AllocationResult repar = pe.getReportedResult();
159: AllocationResult estar = pe.getEstimatedResult();
160: if ((estar == null) || (!repar.isEqual(estar))) {
161: pe.setEstimatedResult(repar);
162: sub.getSubscriber().publishChange(pe, null);
163: }
164: }
165: }
166: }
167:
168: /**
169: * @deprecated use PluginHelper.createEstimatedAllocationResult(Task t, PlanningFactory ldmf, double confrating, boolean success) instead
170: */
171: public static AllocationResult createEstimatedAllocationResult(
172: Task t, PlanningFactory ldmf) {
173: return createEstimatedAllocationResult(t, ldmf, 0.0);
174: }
175:
176: /**
177: * @deprecated use PluginHelper.createEstimatedAllocationResult(Task t, PlanningFactory ldmf, double confrating, boolean success) instead
178: */
179: public static AllocationResult createEstimatedAllocationResult(
180: Task t, PlanningFactory ldmf, double confrating) {
181: Enumeration preferences = t.getPreferences();
182: Vector aspects = new Vector();
183: Vector results = new Vector();
184: while (preferences != null && preferences.hasMoreElements()) {
185: Preference pref = (Preference) preferences.nextElement();
186: int at = pref.getAspectType();
187: aspects.addElement(new Integer(at));
188: ScoringFunction sf = pref.getScoringFunction();
189: // allocate as if you can do it at the "Best" point
190: double myresult = ((AspectScorePoint) sf.getBest())
191: .getValue();
192: results.addElement(new Double(myresult));
193: }
194: int[] aspectarray = new int[aspects.size()];
195: double[] resultsarray = new double[results.size()];
196: for (int i = 0; i < aspectarray.length; i++)
197: aspectarray[i] = (int) ((Integer) aspects.elementAt(i))
198: .intValue();
199: for (int j = 0; j < resultsarray.length; j++)
200: resultsarray[j] = (double) ((Double) results.elementAt(j))
201: .doubleValue();
202:
203: AllocationResult myestimate = ldmf.newAllocationResult(
204: confrating, true, aspectarray, resultsarray);
205: return myestimate;
206: }
207: }
|