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 javax.swing.JPanel;
030: import javax.swing.JComponent;
031: import javax.swing.JTextField;
032: import javax.swing.JLabel;
033: import java.awt.GridBagLayout;
034: import java.awt.GridBagConstraints;
035: import org.cougaar.util.OptionPane;
036: import org.cougaar.util.Random;
037:
038: public class InventoryPilferPlugin implements InventoryPlugin,
039: TimeConstants {
040:
041: private String pilferItem = "DODIC/A576";
042: private double pilferRate;
043: private long minElapsed;
044: private long lastTime = -1L;
045: private static final Random random = new Random();
046: private static final long MIN_INTERVAL = ONE_HOUR;
047: private static final long MAX_INTERVAL = ONE_DAY;
048: private static final double MIN_LAMBDA = 0.1;
049:
050: public InventoryPilferPlugin() {
051: setPilferRate(1.0); // per day
052: }
053:
054: private void setPilferRate(double rate) {
055: pilferRate = rate; // per day
056: minElapsed = (long) Math.min(MAX_INTERVAL, Math.max(MIN_LAMBDA
057: * ONE_DAY / pilferRate, MIN_INTERVAL));
058: }
059:
060: /**
061: * @return the name of this plugin
062: **/
063: public String getPluginName() {
064: return "Pilfer";
065: }
066:
067: public String getDescription() {
068: return "Pilfer plugin steals a certain item";
069: }
070:
071: public void setParameter(String parameter) {
072: }
073:
074: public boolean isConfigurable() {
075: return true;
076: }
077:
078: private static void addItem(JPanel message, int x, int y,
079: JComponent c) {
080: GridBagConstraints gbc = new GridBagConstraints();
081: gbc.gridx = x;
082: gbc.gridy = y;
083: gbc.anchor = gbc.WEST;
084: message.add(c, gbc);
085: }
086:
087: public void configure(java.awt.Component c) {
088: JPanel message = new JPanel(new GridBagLayout());
089: JTextField tf1 = new JTextField(40);
090: tf1.setText(pilferItem);
091: JTextField tf2 = new JTextField(40);
092: tf2.setText("" + pilferRate);
093: addItem(message, 0, 0, new JLabel("Item Identification: "));
094: addItem(message, 1, 0, tf1);
095: addItem(message, 0, 1, new JLabel("Pilferage Rate (per day): "));
096: addItem(message, 1, 1, tf2);
097: int result = OptionPane.showOptionDialog(c, message,
098: "Configure " + getPluginName(),
099: OptionPane.OK_CANCEL_OPTION,
100: OptionPane.QUESTION_MESSAGE, null, null, null);
101: if (result == OptionPane.OK_OPTION) {
102: try {
103: setPilferRate(Double.parseDouble(tf2.getText()));
104: pilferItem = tf1.getText();
105: } catch (Exception e) { // Ignore errors.
106: e.printStackTrace();
107: }
108: }
109: }
110:
111: public void save(java.util.Properties props, String prefix) {
112: props.setProperty(prefix + "item", pilferItem);
113: props.setProperty(prefix + "rate", Double.toString(pilferRate));
114: }
115:
116: public void restore(java.util.Properties props, String prefix) {
117: pilferItem = props.getProperty(prefix + "item", pilferItem);
118: try {
119: pilferRate = Double.parseDouble(props.getProperty(prefix
120: + "rate"));
121: } catch (Exception e) {
122: // State probably not saved.
123: }
124: }
125:
126: public void setEventGenerator(EventGenerator eg) {
127: }
128:
129: /**
130: * Apply this plugin to an InventoryReport,
131: * @return true if this plugin was applicable to the report.
132: **/
133: public boolean apply(TimedInventoryReport tir, long theExecutionTime) {
134: if (lastTime == -1L) {
135: lastTime = theExecutionTime;
136: } else {
137:
138: double elapsed = (theExecutionTime - lastTime);
139: if (elapsed > ONE_HOUR) {
140: double pilferage = (double) random
141: .nextPoisson(pilferRate * elapsed / ONE_DAY);
142: InventoryReport theInventoryReport = tir
143: .getModifiableInventoryReport();
144: pilferage = Math.min(pilferage,
145: theInventoryReport.theQuantity);
146: theInventoryReport.theQuantity -= pilferage;
147: lastTime = theExecutionTime;
148: }
149: }
150: return true;
151: }
152: }
|