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.ldm.plan;
028:
029: import java.util.Enumeration;
030: import java.util.HashMap;
031: import java.util.Hashtable;
032:
033: import org.cougaar.core.util.UID;
034:
035: /** Provides a table containing tasks (keys) and their AllocationResults
036: **/
037:
038: public final class TaskScoreTable {
039: private Task tasks[];
040: private AllocationResult results[];
041:
042: public TaskScoreTable(Hashtable table) {
043: // initialize our arrays
044: int l = table.size();
045: tasks = new Task[l];
046: results = new AllocationResult[l];
047:
048: int i = 0;
049: for (Enumeration e = table.keys(); e.hasMoreElements(); i++) {
050: Task t = (Task) e.nextElement();
051: tasks[i] = t;
052: results[i] = (AllocationResult) table.get(t);
053: }
054: }
055:
056: /**
057: * Use this constructor when no allocation results have been computed
058: **/
059: public TaskScoreTable(Task tasks[]) {
060: this (tasks, new AllocationResult[tasks.length]);
061: }
062:
063: /**
064: * Use this constructor when allocation results have already been computed
065: **/
066: public TaskScoreTable(Task tasks[], AllocationResult results[]) {
067: this .tasks = tasks;
068: this .results = results;
069: }
070:
071: /** @return AllocationResult The current allocation result associated
072: * with the task
073: * @param task The task to use for the key
074: **/
075:
076: public AllocationResult getAllocationResult(Task task) {
077: int l = tasks.length;
078:
079: for (int i = 0; i < l; i++) {
080: if (tasks[i] == task)
081: return results[i];
082: }
083: return null;
084: }
085:
086: public int size() {
087: return tasks.length;
088: }
089:
090: public Task getTask(int i) {
091: return tasks[i];
092: }
093:
094: public AllocationResult getAllocationResult(int i) {
095: return results[i];
096: }
097:
098: public void setAllocationResult(int i, AllocationResult newAR) {
099: results[i] = newAR;
100: }
101:
102: public AllocationResult getAllocationResult(UID uid) {
103: int l = tasks.length;
104: for (int i = 0; i < l; i++) {
105: if (tasks[i].getUID().equals(uid))
106: return results[i];
107: }
108: return null;
109: }
110:
111: public Task getTask(UID uid) {
112: int l = tasks.length;
113: for (int i = 0; i < l; i++) {
114: Task t = tasks[i];
115: if (t.getUID().equals(uid))
116: return t;
117: }
118: return null;
119: }
120:
121: public int getTaskIndex(UID uid) {
122: int l = tasks.length;
123:
124: for (int i = 0; i < l; i++) {
125: Task t = tasks[i];
126: if (t.getUID().equals(uid))
127: return i;
128: }
129: return -1;
130: }
131:
132: // support for ExpansionImpl.
133: public void fillSubTaskResults(HashMap staskinfo, UID changedUID) {
134: // if the set is empty, just copy in the info in
135: if (staskinfo.isEmpty()) {
136: int l = size();
137: for (int i = 0; i < l; i++) {
138: Task t = getTask(i);
139: AllocationResult ar = getAllocationResult(i);
140: UID uid = t.getUID();
141: boolean changedp = uid.equals(changedUID);
142: // should the "t" below be searched for in the workflow??
143: SubTaskResult sr = new SubTaskResult(t, changedp, ar);
144: staskinfo.put(uid.toString(), sr);
145: }
146: } else {
147: // we can cheat and just update the right one.
148: int ti = getTaskIndex(changedUID);
149: if (ti >= 0) {
150: Task t = getTask(ti);
151: AllocationResult ar = getAllocationResult(ti);
152: SubTaskResult sr = new SubTaskResult(t, true, ar);
153: staskinfo.put(changedUID.toString(), sr);
154: } else {
155: // Task not found (no longer in the expansion): drop it on the floor.
156: }
157: }
158: }
159: }
|