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: * THIS SOFTWARE IS MODIFIED FOR TESTING QUO_ULTRALLOG INTEGRATION
026: */
027:
028: package org.cougaar.lib.quo.performance;
029:
030: //package org.cougaar.lib.quo;
031: import java.io.FileWriter;
032: import java.util.Date;
033: import java.util.Enumeration;
034: import java.util.Vector;
035:
036: import org.cougaar.core.blackboard.IncrementalSubscription;
037: import org.cougaar.planning.ldm.asset.Asset;
038: import org.cougaar.planning.ldm.asset.NewItemIdentificationPG;
039: import org.cougaar.planning.ldm.plan.Allocation;
040: import org.cougaar.planning.ldm.plan.AllocationResult;
041: import org.cougaar.planning.ldm.plan.AspectType;
042: import org.cougaar.planning.ldm.plan.AspectValue;
043: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
044: import org.cougaar.planning.ldm.plan.NewTask;
045: import org.cougaar.planning.ldm.plan.Preference;
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.util.UnaryPredicate;
050:
051: /**
052: * Adds a task and then immediately removes it
053: */
054: public class OneTaskAddAndRescindPlugin extends CommonUtilPlugin {
055:
056: // Two assets to use as direct objects for the CODE tasks
057: protected Asset what_to_code;
058: protected IncrementalSubscription allocations; // My allocations
059: protected IncrementalSubscription forceExecute; // My allocations
060:
061: protected int CPUCONSUME;
062: protected int MESSAGESIZE;
063: protected String FILENAME;
064: protected int MAXCOUNT;
065: protected int MYCOUNT = 1;
066: protected int OUTSTANDING_MESSAGES;
067: protected boolean DEBUG = false;
068: protected boolean LOG = false;
069: protected int BURST_TIME = 0;
070: protected String VERB;//="CODE1";
071:
072: private Date startTime;
073: private Task t, changedMind;
074: private int sequenceNum = 1;
075: private AspectValue aspectVal;
076:
077: private int count = 1;
078: private long minDelta;
079:
080: private FileWriter fw;
081: // private double lastReceived=0;
082:
083: private int wakeUpCount, taskAllocationCount;
084:
085: /**
086: * parsing the plugIn arguments and setting the values for CPUCONSUME and MESSAGESIZE
087: */
088: protected void parseParameter() {
089: Vector p = getParameters();
090: CPUCONSUME = getParameterIntValue(p, "CPUCONSUME");
091: MESSAGESIZE = getParameterIntValue(p, "MESSAGESIZE");
092: FILENAME = getParameterValue(p, "FILENAME");
093: MAXCOUNT = getParameterIntValue(p, "MAXCOUNT");
094: OUTSTANDING_MESSAGES = getParameterIntValue(p,
095: "OUTSTANDING_MESSAGES");
096: DEBUG = getParameterBooleanValue(p, "DEBUG");
097: LOG = getParameterBooleanValue(p, "LOG");
098: BURST_TIME = getParameterIntValue(p, "BURST_TIME");
099: VERB = getParameterValue(p, "VERB");
100: }
101:
102: public UnaryPredicate myAllocationPredicate = new UnaryPredicate() {
103: public boolean execute(Object o) {
104: if (o instanceof Allocation) {
105: Task t = ((Allocation) o).getTask();
106: return (t != null)
107: && (t.getVerb().equals(Verb.get(VERB)));
108: }
109: return false;
110: }
111: };
112:
113: /**
114: * Using setupSubscriptions to create the initial CODE tasks
115: */
116: protected void setupSubscriptions() {
117: parseParameter(); //read the plugIn arguments
118: for (int i = 0; i < OUTSTANDING_MESSAGES; i++) {
119: //System.out.println("enetring loop");
120: addTask();
121: // publishRemove(t); //removing immediatelly -- one option
122: }
123: allocations = (IncrementalSubscription) subscribe(myAllocationPredicate);
124: }
125:
126: /**
127: * This Plugin has no subscriptions so this method does nothing
128: */
129: protected void execute() {
130: wakeUpCount++;
131:
132: System.out.println("....Wokenup @" + System.currentTimeMillis()
133: + " wakeUpcount: " + wakeUpCount);
134: publishRemove(t); //removing once woken up
135: allocateChangedtasks(allocations.getChangedList()); // Process changed allocations
136: }
137:
138: protected void addTask() {
139: publishAsset(what_to_code, "The next Killer App",
140: "e something java");
141: t = makeTask(what_to_code, VERB);
142: setPreference(t, AspectType._ASPECT_COUNT, sequenceNum);
143:
144: publishAdd(t);
145:
146: }
147:
148: public void publishAsset(Asset asset, String nameOfAsset,
149: String itemIdentification) {
150: asset = theLDMF.createPrototype("AbstractAsset", nameOfAsset);
151: NewItemIdentificationPG iipg = (NewItemIdentificationPG) theLDMF
152: .createPropertyGroup("ItemIdentificationPG");
153: iipg.setItemIdentification(itemIdentification);
154: asset.setItemIdentificationPG(iipg);
155: publishAdd(asset);
156: }
157:
158: protected void allocateChangedtasks(Enumeration allo_enum) {
159:
160: AllocationResult est, rep;
161: double val = 0;
162: //double arr[] = null;
163: //double received = 0;
164: while (allo_enum.hasMoreElements()) {
165: taskAllocationCount++;
166:
167: int taskCount = (int) t
168: .getPreferredValue(AspectType._ASPECT_COUNT);
169: debug(DEBUG,
170: "ManagerPlugin:allocateChangedTasks ....taskAllocationCount.."
171: + taskAllocationCount + " for task# "
172: + taskCount);
173:
174: Allocation alloc = (Allocation) allo_enum.nextElement();
175: est = null;
176: rep = null;
177: est = alloc.getEstimatedResult();
178: rep = alloc.getReportedResult();
179: if (rep != null) {
180: //debug(DEBUG, FILENAME, fw,"ManagerPlugin:allocateChangedTasks ........" + received);
181: printTheChange();
182: waitFor(BURST_TIME);
183: for (int i = 0; i < OUTSTANDING_MESSAGES; i++) {
184: //addTask();
185: //sequenceNum++;
186: changeTasks(t);
187: //publishRemove(t);
188: }
189: }
190: breakFromLoop(count, MAXCOUNT);
191: }
192: //lastReceived = received;
193: }
194:
195: protected void setPreference(Task t, int aspectType,
196: int sequenceOfTask) {
197: startTime = new Date(); // Add a start_time and end_time strict preference
198: aspectVal = AspectValue.newAspectValue(aspectType,
199: sequenceOfTask);
200: ScoringFunction scorefcn = ScoringFunction
201: .createStrictlyAtValue(aspectVal);
202: Preference pref = theLDMF.newPreference(aspectType, scorefcn);
203: ((NewTask) t).setPreference(pref);
204: }
205:
206: /**
207: * Create a CODE task.
208: * @param what the direct object of the task
209: */
210: protected Task makeTask(Asset what, String verb) {
211: NewTask new_task = theLDMF.newTask();
212: new_task.setVerb(Verb.get(verb));// Set the verb as given
213: new_task.setPlan(theLDMF.getRealityPlan());// Set the reality plan for the task
214: new_task.setDirectObject(what);
215:
216: NewPrepositionalPhrase npp = theLDMF.newPrepositionalPhrase();
217: npp.setPreposition("USING_LANGUAGE");
218: if (MESSAGESIZE == -1)
219: npp.setIndirectObject(alterMessageSize(0));
220: else
221: npp.setIndirectObject(alterMessageSize(MESSAGESIZE));
222: new_task.setPrepositionalPhrases(npp);
223: return new_task;
224: }
225:
226: protected void changeTasks(Task t) {
227: if (CPUCONSUME != -1) //i.e. cpuconsume passed to plugin as a arg
228: consumeCPU(CPUCONSUME);
229: startTime = new Date();
230: if (t.getVerb().equals(VERB))
231: sequenceNum++;
232: setPreference(t, AspectType._ASPECT_COUNT, sequenceNum);
233: //debug(DEBUG, FILENAME, fw,"\nManagerPlugin::Changing task " + t.getVerb() + " with num "
234: // +t.getPreferredValue(AspectType._ASPECT_COUNT ));
235: publishChange(t);
236: }
237:
238: protected void printTheChange() {
239: Date endTime = new Date();
240: long delta = endTime.getTime() - startTime.getTime();
241: if (count == 1)
242: minDelta = delta;
243: else
244: minDelta = Math.min(minDelta, delta);
245: int taskCount = (int) t
246: .getPreferredValue(AspectType._ASPECT_COUNT);
247: String msg = t.getVerb() + "=>" + taskCount + "," + delta + ","
248: + minDelta;
249: log(LOG, FILENAME, fw, msg);
250: count++;
251: }
252:
253: }
|