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.util.Random;
030:
031: public class FailureConsumptionDefaultPlugin implements
032: FailureConsumptionPlugin, TimeConstants {
033: public static final long MIN_INTERVAL = ONE_HOUR;
034: private static Random random = new Random();
035:
036: private class Item extends FailureConsumptionPluginItem {
037: long previousTime = 0L;
038:
039: public Item(FailureConsumptionRate aRate, long theExecutionTime) {
040: super (aRate);
041: previousTime = Math.max(theExecutionTime,
042: aRate.theStartTime);
043: }
044:
045: public AnnotatedDouble getQuantity(long executionTime) {
046: if (executionTime - previousTime < MIN_INTERVAL)
047: return new AnnotatedDouble(0.0);
048: double qPerMilli = theFailureConsumptionRate.theRateValue
049: / ONE_DAY;
050: if (qPerMilli <= 0.0)
051: return new AnnotatedDouble(0.0);
052: long elapsed = executionTime - previousTime;
053: previousTime = executionTime;
054: return new AnnotatedDouble(random.nextPoisson(elapsed
055: * qPerMilli));
056: }
057:
058: /**
059: * Get a good time quantum for this failure/consumption rate.
060: * Tries to not let qPerMilli drop below 0.001 but at least once
061: * per day and no more often than once per hour.
062: * @param executionTime the current execution time (not used)
063: **/
064: public long getTimeQuantum(long executionTime) {
065: double qPerMilli = theFailureConsumptionRate.theRateValue
066: / ONE_DAY;
067: return (long) Math.min(ONE_DAY, Math.max(.001 / qPerMilli,
068: MIN_INTERVAL));
069: }
070: }
071:
072: /**
073: * @return the name of this plugin
074: **/
075: public String getPluginName() {
076: return "Default";
077: }
078:
079: /**
080: * A brief description of this plugin.
081: **/
082: public String getDescription() {
083: return "Default plugin fails/consumes integer amounts at the exact rate";
084: }
085:
086: public void setParameter(String parameter) {
087: // No parameter needed
088: }
089:
090: /**
091: * Thus plugin is not configurable.
092: **/
093: public boolean isConfigurable() {
094: return false;
095: }
096:
097: /**
098: * This plugin is not configurable, so this method does nothing.
099: **/
100: public void configure(java.awt.Component c) {
101: }
102:
103: public void save(java.util.Properties props, String prefix) {
104: }
105:
106: public void restore(java.util.Properties props, String prefix) {
107: }
108:
109: public void setEventGenerator(EventGenerator eg) {
110: }
111:
112: /**
113: * Create a FailureConsumptionItem for this plugin to handle a
114: * particular FailureConsumptionRate.
115: **/
116: public FailureConsumptionPluginItem createFailureConsumptionItem(
117: FailureConsumptionRate aRate,
118: FailureConsumptionSegment aSegment, long theExecutionTime,
119: FailureConsumptionPluginItem aFailureConsumptionPluginItem) {
120: if (aFailureConsumptionPluginItem instanceof Item
121: && aFailureConsumptionPluginItem.theFailureConsumptionRate == aRate) {
122: return aFailureConsumptionPluginItem;
123: }
124: return new Item(aRate, theExecutionTime);
125: }
126: }
|