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: package org.cougaar.glm.execution.eg;
027:
028: import org.cougaar.glm.execution.common.*;
029: import org.cougaar.planning.ldm.measure.Duration;
030:
031: public class FailureConsumptionSegment extends Timed implements
032: TimeConstants {
033: public String theSource;
034: protected FailureConsumptionRateManager theFailureConsumptionRateManager;
035: public FailureConsumptionRate theFailureConsumptionRate;
036: public FailureConsumptionRate theOriginalFailureConsumptionRate;
037: public FailureConsumptionPluginItem thePluginItem;
038: private int theUnits = Duration.DAYS;
039:
040: public FailureConsumptionSegment(String aSource,
041: FailureConsumptionRate aFailureConsumptionRate,
042: FailureConsumptionRateManager aFailureConsumptionRateManager) {
043: theSource = aSource.intern();
044: theFailureConsumptionRate = aFailureConsumptionRate;
045: theFailureConsumptionRateManager = aFailureConsumptionRateManager;
046: setPluginItem(theFailureConsumptionRateManager.getPluginItem(
047: aFailureConsumptionRate, this , null));
048: setEnabled(true);
049: }
050:
051: public void setPluginItem(
052: FailureConsumptionPluginItem newFailureConsumptionPluginItem) {
053: thePluginItem = newFailureConsumptionPluginItem;
054: }
055:
056: public static Object getKey(FailureConsumptionRate fcr) {
057: return fcr.theTaskUID;
058: }
059:
060: public Object getKey() {
061: return getKey(theFailureConsumptionRate);
062: }
063:
064: protected int compareToTieBreaker(Timed other) {
065: if (other instanceof FailureConsumptionSegment) {
066: FailureConsumptionSegment that = (FailureConsumptionSegment) other;
067: long diff = this .theFailureConsumptionRate.theEndTime
068: - that.theFailureConsumptionRate.theEndTime;
069: if (diff < 0)
070: return -1;
071: if (diff > 0)
072: return 1;
073: }
074: return super .compareToTieBreaker(other);
075: }
076:
077: public String getCluster() {
078: return theSource;
079: }
080:
081: /**
082: **/
083: public boolean expired(long time) {
084: long schedulingTime = time
085: + theFailureConsumptionRateManager.theEventGenerator
086: .getSchedulingLookAhead();
087: try {
088: AnnotatedDouble quantity = thePluginItem
089: .getQuantity(schedulingTime);
090: int q = (int) Math.floor(quantity.value);
091: if (q > 0) {
092: // System.out.println(System.identityHashCode(this));
093: // System.out.println("Consumed "
094: // + q
095: // + " "
096: // + theFailureConsumptionRate.theItemIdentification
097: // + " by "
098: // + theFailureConsumptionRate.theConsumer);
099: FailureConsumptionReport theReport = new FailureConsumptionReport(
100: theFailureConsumptionRate.theTaskUID,
101: theFailureConsumptionRate.theItemIdentification,
102: schedulingTime, schedulingTime, (double) q,
103: theFailureConsumptionRate.theConsumer);
104: theFailureConsumptionRateManager
105: .enqueueFailureConsumptionReport(theSource,
106: theReport, quantity.annotation);
107: }
108: } catch (Exception ioe) {
109: ioe.printStackTrace();
110: }
111: long timeQuantum = thePluginItem.getTimeQuantum(schedulingTime);
112: timeQuantum = Math.max(timeQuantum, ONE_MINUTE);
113: timeQuantum = Math.min(timeQuantum, ONE_DAY);
114: theFailureConsumptionRate.theStartTime = time + timeQuantum;
115: return (theFailureConsumptionRate.theStartTime >= theFailureConsumptionRate.theEndTime);
116: }
117:
118: public long getTime() {
119: return theFailureConsumptionRate.theStartTime;
120: }
121:
122: public String getItem() {
123: return theFailureConsumptionRate.theItemIdentification;
124: }
125:
126: public int getUnits() {
127: return theUnits;
128: }
129:
130: public String toString() {
131: return super .toString() + " for " + getItem();
132: }
133: }
|