001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.pm.assignment.contour;
051:
052: import com.projity.algorithm.IntervalGenerator;
053: import com.projity.pm.assignment.Assignment;
054: import com.projity.pm.calendar.WorkCalendar;
055: import com.projity.pm.task.Project;
056:
057: /**
058: * Generator which goes through a work contour. Will also treat gap due to a dependency
059: */
060: public class ContourBucketIntervalGenerator implements
061: IntervalGenerator {
062: protected final int BEFORE_START = -1;
063: protected final int AFTER_END = -2;
064: protected long consumedDuration = 0; // how much used up of planned duration - see DoubleContourBucketIntervalGenerator
065: protected int index = BEFORE_START;
066: long durationLeftUntilRemainingStartDependency = Long.MAX_VALUE;
067: long start = 0;
068: long end;
069: long assignmentDuration;
070: long assignmentActualDuration = 0;
071: private AbstractContourBucket[] contourBuckets = null;
072: WorkCalendar workCalendar;
073: long splitAtDuration = Long.MAX_VALUE;
074: long splitDuration = 0;
075: long remainingSplitBucketDuration = 0;
076: AbstractContourBucket specialBucket = null;
077: boolean didSplit = false;
078:
079: public static ContourBucketIntervalGenerator getInstance(
080: Assignment assignment, Object type) {
081: return new ContourBucketIntervalGenerator(assignment, type);
082: }
083:
084: protected ContourBucketIntervalGenerator(Assignment assignment,
085: Object type) {
086: workCalendar = assignment.getEffectiveWorkCalendar();
087: contourBuckets = assignment.getContour(type);
088:
089: assignmentDuration = assignment.getDurationMillis();
090: assignmentActualDuration = assignment.getActualDuration();
091: long assignmentStart = assignment.getStart();
092: end = assignmentStart; // treat 0th bucket as all time before contour begins
093:
094: Project project = assignment.getTask().getProject();
095: if (project != null && !project.isForward()) //TODO need to figure out what to do if completion in reverse scheduled...
096: return;
097:
098: if (assignment.getDependencyStart() > assignmentStart
099: && assignment.getPercentComplete() > 0.0D) { // if a split caused by remaining work being pushed out by a dependency
100: durationLeftUntilRemainingStartDependency = workCalendar
101: .compare(assignment.getDependencyStart(),
102: assignmentStart, false);
103: if (durationLeftUntilRemainingStartDependency > 0) {
104: if (durationLeftUntilRemainingStartDependency > assignmentActualDuration) {
105: splitAtDuration = assignmentActualDuration;
106: splitDuration = durationLeftUntilRemainingStartDependency
107: - assignmentActualDuration;
108: }
109: }
110: }
111: }
112:
113: public long currentEnd() {
114: return end;
115: }
116:
117: public long currentStart() {
118: return start;
119: }
120:
121: /* (non-Javadoc)
122: * @see com.projity.algorithm.IntervalGenerator#current()
123: */
124: public Object current() {
125: AbstractContourBucket bucket = null;
126: if (specialBucket != null)
127: bucket = specialBucket;
128: else if (index >= 0) // -1 is be
129: bucket = contourBuckets[index];
130: return bucket;
131: }
132:
133: public boolean hasNext() {
134: return index < contourBuckets.length - 1;
135: }
136:
137: boolean didFirstPart = false;
138:
139: /* (non-Javadoc)
140: * @see com.projity.algorithm.IntervalGenerator#next(com.projity.algorithm.ProblemSpace)
141: */
142: public boolean evaluate(Object obj) {
143: index++;
144: if (index == contourBuckets.length)
145: return false;
146: start = workCalendar.add(end, 0, false); // move to next working time, skipping non calendar time
147: specialBucket = null;
148: long bucketDuration = contourBuckets[index]
149: .getBucketDuration(assignmentDuration);
150: consumedDuration += bucketDuration;
151:
152: if (consumedDuration >= splitAtDuration) {
153:
154: remainingSplitBucketDuration = consumedDuration
155: - splitAtDuration; // for latter half of bucket
156:
157: bucketDuration -= remainingSplitBucketDuration;
158: if (bucketDuration > 0) {
159: specialBucket = PersonalContourBucket.getInstance(
160: bucketDuration, contourBuckets[index]
161: .getUnits());
162: index--; // need to repeat the last bucket
163: // System.out.println("before split bucket" + DurationFormat.format(bucketDuration) + " " + this);
164: } else {
165: // System.out.println("before split -nothing before");
166: }
167: splitAtDuration = Long.MAX_VALUE;// we don't want to treat the start split or this again
168: didFirstPart = true;
169: }
170: if (specialBucket == null && didFirstPart) {
171:
172: if (didSplit == false) {
173: bucketDuration = splitDuration;
174: // System.out.println("in split" + DurationFormat.format(bucketDuration)+ " " + this);
175: specialBucket = FillerContourBucket
176: .getInstance(splitDuration);
177: index--;
178: didSplit = true;
179: } else {
180: bucketDuration = remainingSplitBucketDuration;
181: // System.out.println("after split bucket" + DurationFormat.format(bucketDuration));
182: double units = contourBuckets[index].getUnits();
183:
184: if (bucketDuration > 0)
185: specialBucket = PersonalContourBucket.getInstance(
186: remainingSplitBucketDuration, units);
187: remainingSplitBucketDuration = 0;
188: didFirstPart = false;
189: }
190: }
191: //System.out.println("before " + new java.util.Date(end) + " after " + new java.util.Date(workCalendar.add(end,bucketDuration,true)));
192: end = workCalendar.add(end, bucketDuration, true);
193:
194: return true;
195: }
196:
197: // /* (non-Javadoc)
198: // * @see com.projity.algorithm.IntervalGenerator#next(com.projity.algorithm.ProblemSpace)
199: // */
200: // public boolean evaluate2(Object obj) {
201: // index++;
202: // if (index == contourBuckets.length)
203: // return false;
204: // start = workCalendar.add(end,0,false); // move to next working time, skipping non calendar time
205: // specialBucket = null;
206: // long bucketDuration = contourBuckets[index].getBucketDuration(assignmentDuration);
207: // consumedDuration += bucketDuration;
208: //
209: // if (consumedDuration > splitAtDuration) {
210: //System.out.println("consumedDuration > splitAtDuration");
211: // remainingSplitBucketDuration = consumedDuration - splitAtDuration; // for latter half of bucket
212: // consumedDuration = splitAtDuration - bucketDuration; // for next pass we want consumed==splitAt
213: //
214: // bucketDuration -= remainingSplitBucketDuration;
215: //
216: // specialBucket = PersonalContourBucket.getInstance(bucketDuration,contourBuckets[index].getUnits());
217: // index--; // need to repeat the last bucket
218: //
219: // } else if (consumedDuration == splitAtDuration) { // need to do dead time
220: // System.out.println("consumedDuration == splitAtDuration");
221: //
222: // bucketDuration = splitDuration;
223: // specialBucket = FillerContourBucket.getInstance(bucketDuration);
224: //
225: // index--;// need to repeat the last bucket
226: // splitAtDuration = Long.MAX_VALUE; // we don't want to treat the start split or this again
227: // } else if (remainingSplitBucketDuration > 0) {
228: // System.out.println("remainingSplitBucketDuration > 0");
229: //
230: // bucketDuration = remainingSplitBucketDuration;
231: // double units =contourBuckets[index].getUnits();
232: //// Turn off this handling. It seems as if we don't want it after all
233: //// if (units == 0) // split occurs during off time - reduce buckets duration to eliminate any off time that occurs during split
234: //// bucketDuration = Math.max(0,bucketDuration-splitDuration);// need to skip dead time during split duration
235: //
236: // if (bucketDuration > 0)
237: // specialBucket = PersonalContourBucket.getInstance(remainingSplitBucketDuration,units);
238: // remainingSplitBucketDuration = 0;
239: // }
240: //// consumedDuration += bucketDuration;
241: //
242: // end = workCalendar.add(end,bucketDuration,true);
243: //
244: // return true;
245: // }
246:
247: /**
248: * @return Returns the end.
249: */
250: public long getEnd() {
251: return end;
252: }
253:
254: /**
255: * @return Returns the start.
256: */
257: public long getStart() {
258: return start;
259: }
260:
261: /* (non-Javadoc)
262: * @see com.projity.algorithm.IntervalGenerator#isActive()
263: */
264: public boolean isCurrentActive() {
265: AbstractContourBucket cur = (AbstractContourBucket) current();
266: return (cur != null) && (cur.getUnits() != 0.0);
267: }
268:
269: /**
270: * @return Returns the workCalendar.
271: */
272: public WorkCalendar getWorkCalendar() {
273: return workCalendar;
274: }
275:
276: /* (non-Javadoc)
277: * @see com.projity.algorithm.IntervalGenerator#canBeShared()
278: */
279: public boolean canBeShared() {
280: return false;
281: }
282:
283: }
|