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.lib.util;
028:
029: import java.util.Date;
030: import java.util.Enumeration;
031: import java.util.Vector;
032:
033: import org.cougaar.planning.ldm.PlanningFactory;
034: import org.cougaar.planning.ldm.plan.Schedule;
035: import org.cougaar.planning.ldm.plan.ScheduleElement;
036: import org.cougaar.planning.ldm.plan.ScheduleElementImpl;
037: import org.cougaar.planning.ldm.plan.ScheduleImpl;
038: import org.cougaar.util.TimeSpan;
039:
040: public class UTILSchedule {
041:
042: public boolean scheduleAvailable(Schedule sched, Date start,
043: Date end) {
044: Enumeration x = sched.getAllScheduleElements();
045: Date target = start;
046: ScheduleElement se;
047: while (x.hasMoreElements()) {
048: se = (ScheduleElement) x.nextElement();
049: if (target.after(se.getStartDate())
050: || target.equals(se.getStartDate())) {
051: target = se.getEndDate();
052: } else
053: return false;
054: }
055: if (target.after(end) || target.equals(end))
056: return true;
057: else
058: return false;
059: }
060:
061: public Schedule andAvail(Schedule x, Schedule y,
062: PlanningFactory ldmf) {
063: // logger.debug("X: "+x);
064: // logger.debug("Y: "+y);
065: Vector newElem = new Vector();
066: Enumeration a = x.getAllScheduleElements();
067: Enumeration b = y.getAllScheduleElements();
068: ScheduleElement m = null;
069: ScheduleElement n = null;
070: while (a.hasMoreElements() || b.hasMoreElements()) {
071: if (m == null)
072: m = (ScheduleElement) a.nextElement();
073: if (n == null)
074: n = (ScheduleElement) b.nextElement();
075: Date latestStart;
076: Date earliestEnd;
077: if (m.getStartDate().before(n.getStartDate()))
078: latestStart = n.getStartDate();
079: else
080: latestStart = m.getStartDate();
081: if (m.getEndDate().before(n.getEndDate()))
082: earliestEnd = m.getEndDate();
083: else
084: earliestEnd = n.getEndDate();
085: if (latestStart.before(earliestEnd)) {
086: newElem
087: .addElement((ScheduleElement) new ScheduleElementImpl(
088: latestStart, earliestEnd));
089: }
090: if (a.hasMoreElements()
091: && m.getEndDate().before(n.getEndDate()))
092: m = (ScheduleElement) a.nextElement();
093: else if (b.hasMoreElements()
094: && n.getEndDate().before(m.getEndDate()))
095: n = (ScheduleElement) b.nextElement();
096: }
097: return ldmf.newSchedule(newElem.elements());
098: }
099:
100: public Schedule timeInverse(Schedule x, PlanningFactory ldmf) {
101: Vector newElem = new Vector();
102: Date point = new Date(0);
103: Enumeration en = x.getAllScheduleElements();
104: // As I understand it this is getting a enumeration from an ordered set and is therefore ordered
105: while (en.hasMoreElements()) {
106: ScheduleElement se = (ScheduleElement) en.nextElement();
107: if (point.before(se.getStartDate())) {
108: newElem
109: .addElement((ScheduleElement) new ScheduleElementImpl(
110: point, se.getStartDate()));
111: }
112: point = se.getEndDate();
113: }
114: if (!point.equals(new Date(TimeSpan.MAX_VALUE))) {
115: newElem
116: .addElement((ScheduleElement) new ScheduleElementImpl(
117: point, new Date(TimeSpan.MAX_VALUE)));
118: }
119: if (newElem.size() == 0)
120: return x;
121: else
122: return ldmf.newSchedule(newElem.elements());
123: }
124:
125: public void fill(Schedule x) {
126: ScheduleElement y = (ScheduleElement) new ScheduleElementImpl(
127: new Date(0), new Date(TimeSpan.MAX_VALUE));
128: ((ScheduleImpl) x).setScheduleElement(y);
129:
130: }
131:
132: }
|