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.examples;
028:
029: import org.cougaar.core.blackboard.IncrementalSubscription;
030: import org.cougaar.planning.ldm.plan.*;
031: import org.cougaar.planning.ldm.asset.Asset;
032: import org.cougaar.planning.plugin.legacy.SimplePlugin;
033: import org.cougaar.util.UnaryPredicate;
034: import java.util.Enumeration;
035: import java.util.Vector;
036: import java.util.Date;
037:
038: // A simple plugin to respond with any task with an allocation result
039: // satisfying the preferences.
040: public class SimpleSinkAllocatorPlugin extends SimplePlugin {
041: // Subscription for all tasks
042: private IncrementalSubscription allTasks;
043: private UnaryPredicate allTasksPredicate = new UnaryPredicate() {
044: public boolean execute(Object o) {
045: return o instanceof Task;
046: }
047: };
048:
049: // Plan Element set for all tasks predicate
050: private PlanElementSet allTaskPEs;
051:
052: // Single asset to which to allocate all tasks
053: private Asset sink_asset;
054:
055: // Subscribe to all tasks, and create a dummy asset to which to allocate
056: // everything
057: public void setupSubscriptions() {
058:
059: // Subscribe for all tasks
060: allTasks = (IncrementalSubscription) subscribe(
061: allTasksPredicate, allTaskPEs);
062:
063: // Create an abstract asset to which to allocate everything
064: sink_asset = theLDMF.createPrototype("AbstractAsset",
065: "SinkAsset");
066: publishAdd(sink_asset);
067: }
068:
069: // Allocate every new task, and reallocate every changed task
070: public void execute() {
071:
072: // Allocate every new task
073: for (Enumeration e_added = allTasks.getAddedList(); e_added
074: .hasMoreElements();) {
075: Task task = (Task) e_added.nextElement();
076:
077: // Compute allocation result indicating success within preferences
078: AllocationResult result = computeAllocationResult(task);
079:
080: // Generate allocation
081: Allocation allocation = theLDMF.createAllocation(task
082: .getPlan(), task, sink_asset, result,
083: Role.AVAILABLE);
084:
085: // publish new allocation
086: publishAdd(allocation);
087: }
088:
089: // Change allocation result on every new task
090: for (Enumeration e_changed = allTasks.getChangedList(); e_changed
091: .hasMoreElements();) {
092: Task task = (Task) e_changed.nextElement();
093: // Compute new allocation result for changed task
094: AllocationResult result = computeAllocationResult(task);
095:
096: // Find PE (allocation) for task (don't use getPlanElement)
097: Allocation allocation = (Allocation) allTaskPEs
098: .findPlanElement(task);
099:
100: if (allocation != null) {
101:
102: // Set the new estimated result for the allocation based on changed
103: // preferences
104: allocation.setEstimatedResult(result);
105: publishChange(allocation);
106:
107: } else {
108: System.out
109: .println("Error! Should have a plan element for a changed task allocation!");
110: }
111: }
112: }
113:
114: private AllocationResult computeAllocationResult(Task task) {
115: System.out.print("[" + getMessageAddress()
116: + "] allocating task " + task.getUID() + "["
117: + task.getVerb() + "] :");
118:
119: // Grab the preferences and grab the aspect types and results
120: // Don't know how big the list is, so store in an array and then convert
121: Enumeration all_preferences = task.getPreferences();
122: Vector all_aspect_types = new Vector();
123: Vector all_aspect_results = new Vector();
124: while (all_preferences.hasMoreElements()) {
125: // Grab each preference and save aspect type and result
126: Preference preference = (Preference) all_preferences
127: .nextElement();
128: int aspect_type = preference.getAspectType();
129: double preference_result = computePreferenceResult(preference);
130: String preference_result_image = Double
131: .toString(preference_result);
132: if ((aspect_type == AspectType.START_TIME)
133: || (aspect_type == AspectType.END_TIME)) {
134: preference_result_image = new Date(
135: (long) preference_result).toString();
136: }
137: System.out.print(" [" + aspect_type + "] : "
138: + preference_result_image);
139: all_aspect_types.addElement(new Integer(aspect_type));
140: all_aspect_results
141: .addElement(new Double(preference_result));
142: }
143: System.out.println("");
144:
145: // Copy the Integer/Double values
146: // into aspect_type(int)/aspect_result(double) arrays
147: int[] aspect_types = new int[all_aspect_types.size()];
148: for (int i = 0; i < aspect_types.length; i++) {
149: aspect_types[i] = ((Integer) (all_aspect_types.elementAt(i)))
150: .intValue();
151: }
152:
153: double[] aspect_results = new double[all_aspect_results.size()];
154: for (int i = 0; i < aspect_results.length; i++) {
155: aspect_results[i] = ((Double) (all_aspect_results
156: .elementAt(i))).doubleValue();
157: }
158:
159: // Compute new allocation result
160: AllocationResult result = theLDMF.newAllocationResult(1.0, // rating,
161: true, // success,
162: aspect_types, aspect_results);
163:
164: // Add in any auxiliary queries
165: int[] query_types = task.getAuxiliaryQueryTypes();
166: for (int i = 0; i < query_types.length; i++) {
167: int query_type = query_types[i];
168: if (query_type >= 0)
169: result.addAuxiliaryQueryInfo(query_type,
170: "QueryResponse-" + query_type);
171: }
172:
173: return result;
174: }
175:
176: // Compute preference result for given preference based on optimal point
177: // in scoring function
178: private double computePreferenceResult(Preference preference) {
179: ScoringFunction func = preference.getScoringFunction();
180: AspectScorePoint point = func.getBest();
181: return point.getValue();
182: }
183:
184: }
|