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.glm.ldm.plan;
028:
029: import java.util.ArrayList;
030: import java.util.Date;
031:
032: import org.cougaar.planning.ldm.plan.Schedule;
033: import org.cougaar.planning.ldm.plan.ScheduleImpl;
034: import org.cougaar.util.Collectors;
035: import org.cougaar.util.Thunk;
036:
037: public class LaborScheduleImpl extends ScheduleImpl implements
038: LaborSchedule {
039:
040: private Schedule qtySchedule;
041: private Schedule rateSchedule;
042:
043: /** constructor for factory that takes both schedules **/
044: public LaborScheduleImpl(Schedule qsched, Schedule rsched) {
045: super (getCombinedSet(qsched, rsched));
046: qtySchedule = qsched;
047: rateSchedule = rsched;
048:
049: // For now all labor schedules will represent a type of TOTAL_CAPACITY.
050: // Also, all returns from the Schedule interface will return the
051: // product of the quantityscheduleelements and the ratescheduleelements
052: // in the form of quantityscheduleelements.
053:
054: this .scheduleType = PlanScheduleType.TOTAL_CAPACITY;
055: this .scheduleElementType = PlanScheduleElementType.QUANTITY;
056: }
057:
058: public Schedule getQuantitySchedule() {
059: return qtySchedule;
060: }
061:
062: public Schedule getRateSchedule() {
063: return rateSchedule;
064: }
065:
066: public synchronized void setQuantitySchedule(
067: Schedule aQuantitySchedule) {
068: qtySchedule = aQuantitySchedule;
069: super .clear();
070: super .addAll(getCombinedSet(qtySchedule, rateSchedule));
071: }
072:
073: public synchronized void setRateSchedule(Schedule aRateSchedule) {
074: rateSchedule = aRateSchedule;
075: super .clear();
076: super .addAll(getCombinedSet(qtySchedule, rateSchedule));
077: }
078:
079: // use the qtySchedule for this info
080: /** @deprecated use getStartTime() */
081: public synchronized Date getStartDate() {
082: return new Date(qtySchedule.getStartTime());
083: }
084:
085: // duplicated code from above to avoid consing a date
086: public synchronized long getStartTime() {
087: return qtySchedule.getStartTime();
088: }
089:
090: // use the qtySchedule for this info
091: /** @deprecated use getEndTime() */
092: public synchronized Date getEndDate() {
093: return new Date(qtySchedule.getEndTime());
094: }
095:
096: // duplicated above to avoid consing the date
097: public synchronized long getEndTime() {
098: return qtySchedule.getEndTime();
099: }
100:
101: private static ArrayList getCombinedSet(Schedule q, Schedule r) {
102: return getProductElements(q, r);
103: }
104:
105: private static final ArrayList emptySet = new ArrayList(0);
106:
107: private static ArrayList getProductElements(Schedule qtys,
108: final Schedule rates) {
109: // if one of the schedules has no matching elements, return an empty OrderedSet.
110: if (qtys.isEmpty() || rates.isEmpty()) {
111: return emptySet;
112: }
113:
114: // make a new orderedset to hold the new quantityschedulelelements
115: final Schedule result = new ScheduleImpl();
116:
117: Thunk qt = new Thunk() {
118: public void apply(Object o) {
119: final QuantityScheduleElement qse = (QuantityScheduleElement) o;
120: final long qse_s = qse.getStartTime();
121: final long qse_e = qse.getEndTime();
122: final double q = qse.getQuantity();
123:
124: Thunk rt = new Thunk() {
125: public void apply(Object ro) {
126: RateScheduleElement rse = (RateScheduleElement) ro;
127: long rse_s = rse.getStartTime();
128: long rse_e = rse.getEndTime();
129: if (rse_s < qse_e && rse_e > qse_s) {
130: // find the boundaries
131: long s = (qse_s > rse_s) ? qse_s : rse_s;
132: long e = (qse_e < rse_e) ? qse_e : rse_e;
133:
134: result.add(newQSE(s, e, q * rse.getRate()));
135: }
136: }
137: };
138: Collectors.apply(rt, rates);
139: }
140: };
141: Collectors.apply(qt, qtys);
142: return new ArrayList(result);
143: }
144:
145: private static QuantityScheduleElement newQSE(long start, long end,
146: double v) {
147: return new QuantityScheduleElementImpl(start, end, v);
148: }
149:
150: public String toString() {
151: String tstring = "?";
152: String setstring = "?";
153: if (scheduleType != null)
154: tstring = scheduleType;
155: if (scheduleElementType != null)
156: setstring = scheduleElementType.toString();
157:
158: String qestring = qtySchedule.toString();
159: String restring = rateSchedule.toString();
160: return "<LaborSchedule " + tstring + " " + setstring + EOL
161: + "\tQuantities: " + qestring + EOL + "\tRates: "
162: + restring + ">" + EOL;
163: }
164: }
|