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.io.IOException;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.io.Serializable;
033: import java.util.ArrayList;
034: import java.util.Collection;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.ListIterator;
038: import java.util.Vector;
039:
040: /** Composition Interface
041: * An Composition represents the aggregation of multiple tasks
042: * into a single task. Compositions are referenced by Aggregation PlanElements.
043: *
044: *
045: **/
046:
047: public class CompositionImpl implements Composition, NewComposition,
048: Cloneable, Serializable {
049:
050: private List aggregations = new ArrayList();
051: private MPTask compTask;
052: private transient AllocationResultDistributor ard;
053: private transient AllocationResultDistributor DEFAULT_DA = AllocationResultDistributor.DEFAULT;
054:
055: /*Constructor for factory for now */
056: public CompositionImpl() {
057: super ();
058: this .setDistributor(DEFAULT_DA);
059: }
060:
061: /* Simple Constructor uses a specified Distributor.
062: * @param aggregationpes The Aggregations of the tasks that are being combined.
063: * @param newtask The resulting task.
064: * @param distributor The Distributor to use in propogating notifications/results
065: * @return Composition.
066: */
067: public CompositionImpl(AllocationResultDistributor distributor,
068: Collection aggregationpes, MPTask newtask) {
069: super ();
070: this .setAggregations(aggregationpes);
071: this .setCombinedTask(newtask);
072: this .setDistributor(distributor);
073: }
074:
075: // Composition interface implementations
076:
077: /** Convenienve method that calculates the Tasks that are
078: * being aggregated by looking at all of the Aggregations.
079: * (Aggregation.getTask())
080: * @return List
081: * @see org.cougaar.planning.ldm.plan.Task
082: **/
083: public synchronized List getParentTasks() {
084: ListIterator lit = aggregations.listIterator();
085: List parents = new ArrayList();
086: while (lit.hasNext()) {
087: Aggregation anagg = (Aggregation) lit.next();
088: parents.add(anagg.getTask());
089: }
090: return parents;
091: }
092:
093: /** Returns the Aggregation PlanElements of the Tasks that
094: * are being combined
095: * @return List
096: */
097: public synchronized List getAggregations() {
098: return new ArrayList(aggregations);
099: }
100:
101: /** Returns the newly created task that represents all 'parent' tasks.
102: * @return Task
103: * @see org.cougaar.planning.ldm.plan.Task
104: */
105: public MPTask getCombinedTask() {
106: return compTask;
107: }
108:
109: /** Allows the AllocationResult to be properly dispersed among the
110: * original (or parent) tasks.
111: * @return AllocationResultDistributor
112: * @see org.cougaar.planning.ldm.plan.AllocationResultDistributor
113: */
114: public AllocationResultDistributor getDistributor() {
115: return ard;
116: }
117:
118: /**Calculate seperate AllocationResults for each parent task of the Composition.
119: * @return distributedresults
120: * @see org.cougaar.planning.ldm.plan.Composition
121: * @see org.cougaar.planning.ldm.plan.TaskScoreTable
122: * @see org.cougaar.planning.ldm.plan.AllocationResult
123: */
124: public TaskScoreTable calculateDistribution() {
125: Task task = getCombinedTask();
126: PlanElement pe = task.getPlanElement();
127: AllocationResult ar = null;
128: if (pe != null) {
129: ar = pe.getEstimatedResult();
130: }
131: // this should be cleaned up later - but for now copy the
132: // list into a vector for the distributor that expects a vector
133: Vector parentsv = new Vector(getParentTasks());
134: return getDistributor().calculate(parentsv, ar);
135: }
136:
137: // NewComposition interface implementations
138:
139: /** Set the Aggregation PlanElements of the tasks being combined
140: * @param aggs The Aggregations
141: * @see org.cougaar.planning.ldm.plan.Aggregation
142: */
143: public synchronized void setAggregations(Collection aggs) {
144: aggregations.clear();
145: Iterator aggit = aggs.iterator();
146: while (aggit.hasNext()) {
147: Aggregation anagg = (Aggregation) aggit.next();
148: if (anagg instanceof Aggregation) {
149: aggregations.add(anagg);
150: } else {
151: throw new IllegalArgumentException(
152: "Composition.setAggregations(Collection aggs) expects that all "
153: + "members of the Collection are of type Aggregation");
154: }
155: }
156: }
157:
158: public synchronized List clearAggregations() {
159: List l = aggregations;
160: aggregations = new ArrayList(5);
161: return l;
162: }
163:
164: /** Add a single Aggregation to the existing collection
165: */
166: public synchronized void addAggregation(Aggregation singleagg) {
167: aggregations.add(singleagg);
168: }
169:
170: /** for infrastructure */
171: protected synchronized void removeAggregation(Aggregation removeagg) {
172: aggregations.remove(removeagg);
173: }
174:
175: /** Set the newly created task that represents all 'parent' tasks.
176: * @param newTask
177: * @see org.cougaar.planning.ldm.plan.Task
178: */
179: public void setCombinedTask(MPTask newTask) {
180: compTask = newTask;
181: }
182:
183: /** Allows the AllocationResult to be properly dispersed among the
184: * original (or parent) tasks.
185: * @param distributor
186: * @see org.cougaar.planning.ldm.plan.AllocationResultDistributor
187: */
188: public void setDistributor(AllocationResultDistributor distributor) {
189: ard = distributor;
190: }
191:
192: private boolean _propagateP = true;
193:
194: public boolean isPropagating() {
195: return _propagateP;
196: }
197:
198: public void setIsPropagating(boolean isProp) {
199: _propagateP = isProp;
200: }
201:
202: /** @deprecated Use setIsPropagating(boolean isProp) -defaults to true*/
203: public void setIsPropagating() {
204: _propagateP = true;
205: }
206:
207: private boolean shouldCleanUp = true;
208:
209: public synchronized boolean shouldDoMassCleanUp() {
210: return shouldCleanUp;
211: }
212:
213: public synchronized void cleanedUp() {
214: shouldCleanUp = false;
215: }
216:
217: public synchronized boolean doingCleanUp() {
218: boolean old = shouldCleanUp;
219: shouldCleanUp = false;
220: return old;
221: }
222:
223: /* for user interface */
224: public Task[] getParentTasksAsArray() {
225: Collection ptasks = getParentTasks();
226: Task[] t = (Task[]) ptasks.toArray(new Task[ptasks.size()]);
227: return t;
228: }
229:
230: public Task getParentTaskFromArray(int i) {
231: Task t[] = getParentTasksAsArray();
232: if (i < t.length)
233: return t[i];
234: else
235: return null;
236: }
237:
238: public Aggregation[] getAggregationsAsArray() {
239: synchronized (aggregations) {
240: Aggregation[] a = (Aggregation[]) aggregations
241: .toArray(new Aggregation[aggregations.size()]);
242: return a;
243: }
244: }
245:
246: public Aggregation getAggregationFromArray(int i) {
247: Aggregation a[] = getAggregationsAsArray();
248: if (i < a.length)
249: return a[i];
250: else
251: return null;
252: }
253:
254: private void writeObject(ObjectOutputStream os) throws IOException {
255: os.defaultWriteObject();
256: if (ard == DEFAULT_DA) {
257: os.writeObject(null);
258: } else {
259: os.writeObject(ard);
260: }
261: }
262:
263: private void readObject(ObjectInputStream is) throws IOException,
264: ClassNotFoundException {
265: is.defaultReadObject();
266: DEFAULT_DA = AllocationResultDistributor.DEFAULT;
267: ard = (AllocationResultDistributor) is.readObject();
268: if (ard == null) {
269: ard = DEFAULT_DA;
270: }
271: }
272: }
|