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.Rate;
030: import org.cougaar.util.OptionPane;
031: import java.util.Properties;
032:
033: /**
034: * Example F/C plugin illustrating how one might write a plugin that
035: * applies to a certain class of supply and how to use the GUI
036: * configuration features. Consult the default plugin for more mundane
037: * matters.
038: *
039: * Specifically, this plugin applies a fixed multiplier to the
040: * consumption rates of ammunition. The multiplier can be configured
041: * from the gui screen.
042: **/
043: public class DODICSpecialPlugin implements FailureConsumptionPlugin,
044: TimeConstants {
045: /**
046: * The fixed rate of consumption for DODICs. Can be configured from the configuration GUI
047: **/
048: private static final double INITIAL_FIXED_MULTIPLIER = 2.5; // Elevate rate 2.5 times
049:
050: private double fixedMultiplier = INITIAL_FIXED_MULTIPLIER;
051:
052: private String prefix = "DODIC";
053:
054: private class Item extends FailureConsumptionPluginItem {
055: long previousTime = 0L;
056:
057: public Item(FailureConsumptionRate aRate, long theExecutionTime) {
058: super (aRate);
059: previousTime = Math.max(theExecutionTime,
060: aRate.theStartTime);
061: }
062:
063: private double getQPerMilli() {
064: double result = theFailureConsumptionRate.theRateValue
065: / ONE_DAY;
066: result *= fixedMultiplier
067: / theFailureConsumptionRate.theRateMultiplier;
068: return result;
069: }
070:
071: public AnnotatedDouble getQuantity(long executionTime) {
072: double qPerMilli = getQPerMilli();
073: long elapsed = executionTime - previousTime;
074: double dr = elapsed * qPerMilli;
075: double result = Math.floor(dr);
076: previousTime += (long) (result / qPerMilli);
077: return new AnnotatedDouble(result, "DODICSpecialPlugin");
078: }
079:
080: public long getTimeQuantum(long executionTime) {
081: double qPerMilli = getQPerMilli();
082: return previousTime + ((long) (1.0 / qPerMilli))
083: - executionTime;
084: }
085: }
086:
087: /**
088: * @return the name of this plugin
089: **/
090: public String getPluginName() {
091: return prefix + " Special";
092: }
093:
094: public String getDescription() {
095: return "Special plugin for " + prefix
096: + " items that uses a fixed rate";
097: }
098:
099: public boolean isConfigurable() {
100: return true;
101: }
102:
103: public void setParameter(String parameter) {
104: prefix = parameter; // parameter is item prefix.
105: }
106:
107: public void configure(java.awt.Component c) {
108: String result = (String) OptionPane.showInputDialog(c,
109: "Enter the consumption rate multiplier", "Configure "
110: + getPluginName(), OptionPane.QUESTION_MESSAGE,
111: null, null, new Double(fixedMultiplier));
112: try {
113: fixedMultiplier = Double.parseDouble(result);
114: } catch (Exception e) { // Ignore errors.
115: }
116: }
117:
118: public void save(Properties props, String prefix) {
119: props.setProperty(prefix + "fixedMultiplier", Double
120: .toString(fixedMultiplier));
121: }
122:
123: public void restore(Properties props, String prefix) {
124: try {
125: fixedMultiplier = Double.parseDouble(props
126: .getProperty(prefix + "fixedMultiplier"));
127: } catch (Exception e) {
128: // State not present in props
129: }
130: }
131:
132: public void setEventGenerator(EventGenerator eg) {
133: }
134:
135: /**
136: * Create a FailureConsumptionItem for this plugin to handle a
137: * particular FailureConsumptionRate.
138: **/
139: public FailureConsumptionPluginItem createFailureConsumptionItem(
140: FailureConsumptionRate aRate,
141: FailureConsumptionSegment aSegment, long theExecutionTime,
142: FailureConsumptionPluginItem aFailureConsumptionPluginItem) {
143: if (aFailureConsumptionPluginItem instanceof Item
144: && aFailureConsumptionPluginItem.theFailureConsumptionRate == aRate) {
145: return aFailureConsumptionPluginItem;
146: }
147: if (aRate.theItemIdentification.startsWith(prefix)) {
148: return new Item(aRate, theExecutionTime);
149: }
150: return null;
151: }
152: }
|