001: /*--------------------------------------------------------------------------
002: * <copyright>
003: *
004: * Copyright 2000-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: package org.cougaar.glm.plugins;
027:
028: import java.util.Collection;
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.cougaar.glm.ldm.GLMFactory;
033: import org.cougaar.glm.ldm.asset.NewScheduledContentPG;
034: import org.cougaar.glm.ldm.asset.PropertyGroupFactory;
035: import org.cougaar.glm.ldm.asset.ScheduledContentPG;
036: import org.cougaar.glm.ldm.plan.NewQuantityScheduleElement;
037: import org.cougaar.glm.ldm.plan.PlanScheduleType;
038: import org.cougaar.glm.ldm.plan.QuantityScheduleElement;
039: import org.cougaar.planning.ldm.asset.Asset;
040: import org.cougaar.planning.ldm.plan.Schedule;
041: import org.cougaar.planning.ldm.plan.ScheduleElement;
042: import org.cougaar.planning.ldm.plan.ScheduleUtilities;
043:
044: /** Provide convenience methods for creating objects. */
045: public class ScheduleUtils {
046:
047: protected static final long MSEC_PER_DAY = 86400000;
048:
049: public static Vector convertEnumToVector(Enumeration e) {
050: Vector v = new Vector();
051: while (e.hasMoreElements()) {
052: v.addElement(e.nextElement());
053: }
054: return v;
055: }
056:
057: public static ScheduledContentPG createScheduledContentPG(Asset a,
058: Schedule s) {
059: NewScheduledContentPG scp = PropertyGroupFactory
060: .newScheduledContentPG();
061: scp.setAsset(a);
062: scp.setSchedule(s);
063: return scp;
064: }
065:
066: // truncates time span
067: // note amount can be positive or negative.
068: public static Schedule adjustSchedule(Schedule sched, long start,
069: long end, int amount, long time_incr) {
070: return adjustSchedule(sched, truncTime(start, time_incr),
071: truncTime(end + 1, time_incr) - 1, amount);
072: }
073:
074: // note amount can be positive or negative.
075: public static Schedule adjustSchedule(Schedule sched, long start,
076: long end, int amount) {
077: Schedule simple_sched = buildSimpleQuantitySchedule(amount,
078: start, end);
079: return ScheduleUtilities.addSchedules(sched, simple_sched);
080: }
081:
082: public static Schedule buildSimpleQuantitySchedule(double qty,
083: long start, long end) {
084: QuantityScheduleElement nqse = buildQuantityScheduleElement(
085: qty, start, end);
086: Vector sched_el = new Vector();
087: sched_el.addElement(nqse);
088:
089: return GLMFactory.newQuantitySchedule(sched_el.elements(),
090: PlanScheduleType.TOTAL_INVENTORY);
091: }
092:
093: public static Schedule buildSimpleQuantitySchedule(int qty,
094: long start, long end, long time_incr) {
095: return buildSimpleQuantitySchedule(qty, truncTime(start,
096: time_incr), truncTime(end + 1, time_incr) - 1);
097: }
098:
099: public static long truncTime(long time, long time_incr) {
100: return (time / time_incr) * time_incr;
101: }
102:
103: public static QuantityScheduleElement buildQuantityScheduleElement(
104: double qty, long start, long end) {
105: NewQuantityScheduleElement e = GLMFactory
106: .newQuantityScheduleElement();
107: e.setQuantity(qty);
108: e.setStartTime(start);
109: e.setEndTime(end);
110: return e;
111: }
112:
113: public static QuantityScheduleElement buildQuantityScheduleElement(
114: double qty, long start, long end, long time_incr) {
115: return buildQuantityScheduleElement(qty, truncTime(start,
116: time_incr), truncTime(end + 1, time_incr) - 1);
117: }
118:
119: // note amount can be positive or negative.
120: // adjust quantity from start until end of schedule
121: // if start is after end of schedule,
122: // append new element from start to start + a day
123: public static Schedule adjustSchedule(Schedule sched, long start,
124: int amount) {
125: long end = sched.getEndTime();
126: if (end > start)
127: end = start + MSEC_PER_DAY;
128: return adjustSchedule(sched, start, end, amount);
129: }
130:
131: public static boolean isOffendingSchedule(Schedule sched) {
132: long start = sched.getStartTime() - 1;
133: QuantityScheduleElement qse;
134: Enumeration elements = sched.getAllScheduleElements();
135: while (elements.hasMoreElements()) {
136: qse = (QuantityScheduleElement) elements.nextElement();
137: if (qse.getStartTime() < start) {
138: return true;
139: }
140: start = qse.getStartTime();
141: }
142: return false;
143: }
144:
145: public static ScheduleElement getElementWithTime(Schedule s,
146: long time) {
147: ScheduleElement se = null;
148: if (s != null) {
149: Collection c = s.getScheduleElementsWithTime(time);
150: if (!c.isEmpty()) {
151: se = (ScheduleElement) c.iterator().next();
152: }
153: }
154: return se;
155: }
156:
157: }
|