001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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:
027: package org.cougaar.planning.plugin.completion;
028:
029: import java.util.Collection;
030: import java.util.Enumeration;
031: import java.util.Iterator;
032: import java.util.Vector;
033:
034: import org.cougaar.core.agent.service.alarm.Alarm;
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.planning.ldm.PlanningFactory;
037: import org.cougaar.planning.ldm.measure.CountRate;
038: import org.cougaar.planning.ldm.measure.FlowRate;
039: import org.cougaar.planning.ldm.measure.Rate;
040: import org.cougaar.planning.ldm.plan.AspectRate;
041: import org.cougaar.planning.ldm.plan.AspectType;
042: import org.cougaar.planning.ldm.plan.AspectValue;
043: import org.cougaar.planning.ldm.plan.NewTask;
044: import org.cougaar.planning.ldm.plan.Preference;
045: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
046: import org.cougaar.planning.ldm.plan.ScoringFunction;
047: import org.cougaar.planning.ldm.plan.Task;
048: import org.cougaar.planning.ldm.plan.Verb;
049: import org.cougaar.planning.ldm.predicate.TaskPredicate;
050: import org.cougaar.planning.plugin.legacy.SimplePlugin;
051: import org.cougaar.planning.plugin.util.PluginHelper;
052:
053: /**
054: * A plugin for testing completion detection. Injects a slug of new
055: * Supply tasks every day (of execution) based on the ProjectSupply
056: * tasks in the logplan. Each ProjectSupply leads to one Supply task
057: * for which the quantity is one day's projected demand. The intention
058: * is to stimulate the inventory manager to run and set off a wave of
059: * activity through the society that the completion plugins detect to
060: * inhibit the speeded up advancement of time until the cessation of
061: * activity is detected.
062: **/
063: public class TestCompletionPlugin extends SimplePlugin {
064: private static final long ONE_DAY = 86400000L;
065: private static final long TIME_OFFSET = ONE_DAY / 2;
066: private static final Verb SUPPLY = Verb.get("Supply");
067: private static final Verb PROJECTSUPPLY = Verb.get("ProjectSupply");
068: private static final String MAINTAINING = "Maintaining";
069: private static final String FOR = "For";
070: private static final int DEMANDRATE = AspectType.N_CORE_ASPECTS + 0;
071:
072: private long demoNow;
073: private long nextWakeTime;
074: private Alarm timer;
075: private LoggingService logger;
076: private PlanningFactory ldmf;
077:
078: public void setLoggingService(LoggingService ls) {
079: logger = ls;
080: }
081:
082: public void setupSubscriptions() {
083: ldmf = (PlanningFactory) getFactory("planning");
084: demoNow = currentTimeMillis();
085: nextWakeTime = TIME_OFFSET + (((demoNow) / ONE_DAY) + 1)
086: * ONE_DAY;
087: timer = wakeAt(nextWakeTime);
088: }
089:
090: public void execute() {
091: if (timer.hasExpired()) {
092: demoNow = currentTimeMillis();
093: do {
094: generateSupplyTasks();
095: nextWakeTime += ONE_DAY;
096: } while (nextWakeTime < demoNow);
097: timer = wakeAt(nextWakeTime);
098: }
099: }
100:
101: private String parseMaintainedItemType(String mi) {
102: int pos1 = mi.indexOf(": <") + 3;
103: int pos2 = mi.indexOf(">", pos1);
104: return mi.substring(pos1, pos2);
105: }
106:
107: private void generateSupplyTasks() {
108: final String selfIdentification = getMessageAddress()
109: .toString();
110: TaskPredicate projectionTaskPredicate = new TaskPredicate() {
111: public boolean execute(Task task) {
112: if (task.getVerb().equals(PROJECTSUPPLY)) {
113: PrepositionalPhrase maintaining = task
114: .getPrepositionalPhrase(MAINTAINING);
115: if (maintaining != null) {
116: String maintainedItem = maintaining
117: .getIndirectObject().toString();
118: String maintainedItemType = parseMaintainedItemType(maintainedItem);
119: if (maintainedItemType.equals("Inventory"))
120: return false;
121: PrepositionalPhrase pp = task
122: .getPrepositionalPhrase(FOR);
123: String orgName = (String) pp
124: .getIndirectObject();
125: if (selfIdentification.equals(orgName)) {
126: return true;
127: }
128: }
129: }
130: return false;
131: }
132: };
133: Collection projections = query(projectionTaskPredicate);
134: if (projections.size() == 0)
135: return;
136: if (logger.isDebugEnabled()) {
137: logger.debug(selfIdentification + ": " + projections.size()
138: + " projections");
139: }
140: for (Iterator i = projections.iterator(); i.hasNext();) {
141: Task projectionTask = (Task) i.next();
142: NewTask supplyTask = ldmf.newTask();
143: supplyTask.setVerb(SUPPLY);
144: supplyTask
145: .setDirectObject(projectionTask.getDirectObject());
146: // Copy all but the demand rate pp
147: Enumeration fe = projectionTask.getPrepositionalPhrases();
148: // new FilteredEnumeration(, demandRateFilter);
149: supplyTask.setPrepositionalPhrases(fe);
150: supplyTask.setPlan(projectionTask.getPlan());
151: supplyTask.setContext(projectionTask.getContext());
152: Vector prefs = new Vector(3);
153: Preference pref;
154: ScoringFunction sf;
155: AspectValue av;
156: av = AspectValue.newAspectValue(AspectType.START_TIME,
157: demoNow);
158: sf = new ScoringFunction.StepScoringFunction(av, 0.00, 0.99);
159: pref = ldmf.newPreference(AspectType.START_TIME, sf, 1.0);
160: prefs.addElement(pref);
161: av = AspectValue.newAspectValue(AspectType.END_TIME,
162: demoNow);
163: sf = new ScoringFunction.StepScoringFunction(av, 0.00, 0.99);
164: pref = ldmf.newPreference(AspectType.END_TIME, sf, 1.0);
165: prefs.addElement(pref);
166: double rateValue = 0.0;
167: Rate rate = ((AspectRate) PluginHelper.getPreferenceBest(
168: projectionTask, DEMANDRATE)).getRateValue();
169: if (rate instanceof CountRate) {
170: rateValue = ((CountRate) rate)
171: .getValue(CountRate.EACHES_PER_DAY);
172: } else if (rate instanceof FlowRate) {
173: rateValue = ((FlowRate) rate)
174: .getValue(FlowRate.GALLONS_PER_DAY);
175: } else {
176: continue;
177: }
178: if (rateValue <= 0.0)
179: continue;
180: double theQuantity = rateValue * ONE_DAY;
181: av = AspectValue.newAspectValue(AspectType.QUANTITY,
182: theQuantity);
183: sf = new ScoringFunction.StrictValueScoringFunction(av);
184: pref = ldmf.newPreference(AspectType.QUANTITY, sf, 1.0);
185: prefs.addElement(pref);
186: supplyTask.setPreferences(prefs.elements());
187: publishAdd(supplyTask);
188: }
189: }
190: }
|