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.logistics.ui.inventory;
028:
029: import java.util.ArrayList;
030: import java.util.Date;
031:
032: import com.klg.jclass.chart.ChartDataModel;
033: import com.klg.jclass.chart.LabelledChartDataModel;
034: import com.klg.jclass.chart.ChartDataSupport;
035: import com.klg.jclass.chart.ChartDataEvent;
036: import com.klg.jclass.chart.ChartDataManageable;
037: import com.klg.jclass.chart.ChartDataManager;
038:
039: import org.cougaar.util.log.Logging;
040: import org.cougaar.util.log.Logger;
041:
042: import org.cougaar.logistics.plugin.inventory.LogisticsInventoryFormatter;
043:
044: import org.cougaar.logistics.ui.inventory.data.InventoryData;
045: import org.cougaar.logistics.ui.inventory.data.InventoryTask;
046: import org.cougaar.logistics.ui.inventory.data.InventoryAR;
047: import org.cougaar.logistics.ui.inventory.data.InventoryScheduleHeader;
048: import org.cougaar.logistics.ui.inventory.data.InventoryScheduleElement;
049:
050: import org.cougaar.util.TimeSpanSet;
051:
052: /**
053: * <pre>
054: *
055: * The RequisitionsChartDataModel is the ChartDataModel for the
056: * all non projection tasks. A Schedule of actuals and their
057: * corresponding allocation results are given as the
058: * values to compute into the x and y coordinates for the
059: * chart.
060: *
061: *
062: * @see InventoryBaseChartDataModel
063: *
064: **/
065:
066: public class RequisitionsChartDataModel extends
067: InventoryBaseChartDataModel {
068:
069: protected String reqScheduleName;
070: protected String reqARScheduleName;
071:
072: public static final String REQUISITION_SERIES_LABEL = "Requisition";
073: public static final String REQUISITION_ALLOCATION_SERIES_LABEL = "Requisition Response";
074: public static final String REQUISITION_LEGEND = "";
075:
076: public RequisitionsChartDataModel(String aReqScheduleName,
077: String aReqARScheduleName) {
078: this (REQUISITION_LEGEND, aReqScheduleName, aReqARScheduleName);
079: }
080:
081: public RequisitionsChartDataModel(String legendTitle,
082: String reqSchedule, String reqARSchedule) {
083: this (null, reqSchedule, reqARSchedule, legendTitle);
084: }
085:
086: public RequisitionsChartDataModel(InventoryData data,
087: String reqSchedule, String reqARSchedule,
088: String theLegendTitle) {
089: inventory = data;
090: legendTitle = theLegendTitle;
091: reqScheduleName = reqSchedule;
092: reqARScheduleName = reqARSchedule;
093: nSeries = 2;
094: scheduleNames = new String[2];
095: scheduleNames[0] = reqScheduleName;
096: scheduleNames[1] = reqARScheduleName;
097: seriesLabels = new String[2];
098: seriesLabels[0] = REQUISITION_SERIES_LABEL;
099: seriesLabels[1] = REQUISITION_ALLOCATION_SERIES_LABEL;
100: logger = Logging.getLogger(this );
101: initValues();
102: }
103:
104: public void setValues() {
105: if (valuesSet)
106: return;
107: setRequisitionValues();
108: valuesSet = true;
109: }
110:
111: public void setRequisitionValues() {
112:
113: if (inventory == null) {
114: xvalues = new double[nSeries][0];
115: yvalues = new double[nSeries][0];
116: return;
117: }
118:
119: InventoryScheduleHeader schedHeader = (InventoryScheduleHeader) inventory
120: .getSchedules().get(reqScheduleName);
121: ArrayList requisitions = schedHeader.getSchedule();
122: schedHeader = (InventoryScheduleHeader) inventory
123: .getSchedules().get(reqARScheduleName);
124: ArrayList reqARs = schedHeader.getSchedule();
125:
126: computeCriticalNValues();
127:
128: xvalues = new double[nSeries][nValues];
129: yvalues = new double[nSeries][nValues];
130: //initZeroYVal(nValues);
131:
132: for (int i = 0; i < nSeries; i++) {
133: for (int j = 0; j < nValues; j++) {
134: xvalues[i][j] = minBucket + (j * bucketDays);
135: yvalues[i][j] = 0;
136: }
137: }
138:
139: for (int i = 0; i < requisitions.size(); i++) {
140: InventoryTask task = (InventoryTask) requisitions.get(i);
141: long endTime = task.getEndTime();
142: int endBucket = (int) computeBucketFromTime(endTime);
143: try {
144: yvalues[0][endBucket - minBucket] += (task.getQty() * unitFactor);
145: } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
146: if (logger.isErrorEnabled()) {
147: logger.error("Whoa! endBucket is " + endBucket
148: + " and minBucket is " + minBucket
149: + " and maxBucket is " + maxBucket
150: + " and the task at the " + i
151: + "th place is " + task + " - startTime "
152: + new Date(task.getStartTime())
153: + " and the fatal end time is "
154: + new Date(endTime));
155: }
156: throw ex;
157: }
158: }
159:
160: for (int i = 0; i < reqARs.size(); i++) {
161: InventoryAR ar = (InventoryAR) reqARs.get(i);
162: if (ar.isSuccess()) {
163: long endTime = ar.getEndTime();
164: int endBucket = (int) computeBucketFromTime(endTime);
165: yvalues[1][endBucket - minBucket] += (ar.getQty() * unitFactor);
166: }
167: }
168:
169: //MWD this is more expensive than it needs to be.
170: //Cheaper to go through through all InventoryTasks
171: //take the end time figure which day it goes into
172: //and sum the value in the array bucket - same for
173: //ARs - fix tommorow. Probably don't even need
174: //schedules for this.
175:
176: /***
177: ** MWD Remove
178:
179: TimeSpanSet reqSchedule = new TimeSpanSet(requisitions);
180: TimeSpanSet reqARSchedule = new TimeSpanSet(reqARs);
181:
182: for(int i=minDay; i<=maxDay ; i+=bucketDays) {
183: long startTime = ((i * MILLIS_IN_DAY) + baseTime);
184: long endTime = (startTime + (bucketDays * MILLIS_IN_DAY)) - 1;
185: Collection reqs = reqSchdule.encapsulatedSet(startTime,endTime);
186: Collection reqARs = reqARSchdule.encapsulatedSet(startTime,endTime);
187: double totalReqs=0;
188: double totalARs=0;
189: Iterator it = reqs.iterator();
190: while(it.hasNext()) {
191: totalReqs+=(((InventoryTask) it.next()).getQty());
192: }
193: it = reqARs.iterator();
194: while(it.hasNext()) {
195: InventoryAR ar = ((InventoryAR) it.next());
196: if(ar.isSuccess()) {
197: totalARs+=ar.getQty();
198: }
199: }
200: yvalues[0][i-minDay] = totalReqs;
201: yvalues[1][i-minDay] = totalARs;
202: }
203:
204: ***/
205: }
206:
207: }
|