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