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: import java.io.FileWriter;
031: import java.util.Date;
032: import java.util.Enumeration;
033: import java.util.Vector;
034:
035: import org.cougaar.core.blackboard.IncrementalSubscription;
036: import org.cougaar.planning.ldm.asset.Asset;
037: import org.cougaar.planning.ldm.asset.NewItemIdentificationPG;
038: import org.cougaar.planning.ldm.plan.Allocation;
039: import org.cougaar.planning.ldm.plan.AllocationResult;
040: import org.cougaar.planning.ldm.plan.AspectType;
041: import org.cougaar.planning.ldm.plan.AspectValue;
042: import org.cougaar.planning.ldm.plan.NewPrepositionalPhrase;
043: import org.cougaar.planning.ldm.plan.NewTask;
044: import org.cougaar.planning.ldm.plan.Preference;
045: import org.cougaar.planning.ldm.plan.ScoringFunction;
046: import org.cougaar.planning.ldm.plan.Task;
047: import org.cougaar.planning.ldm.plan.Verb;
048: import org.cougaar.util.UnaryPredicate;
049:
050: /**
051: * This COUGAAR Plugin creates and publishes "CODE" tasks and if
052: * allocationResult is a success it keeps producing more task
053: * It also reads the Plugin arguments and may alter MessageSize or slurp CPU
054: */
055: public class ManagerPlugin extends CommonUtilPlugin {
056:
057: // Two assets to use as direct objects for the CODE tasks
058: protected Asset what_to_code;
059: protected IncrementalSubscription allocations; // My allocations
060: protected IncrementalSubscription forceExecute; // My allocations
061: protected int CPUCONSUME, MESSAGESIZE, OUTSTANDING_MESSAGES,
062: MAXCOUNT = 1;
063: protected String FILENAME, VERB;
064: protected boolean DEBUG = false, LOG = false;
065: protected int BURST_TIME = 0;
066: protected Date startTime;
067: protected Task t, changedMind;
068: protected int sequenceNum = 1, count = 1;
069: protected AspectValue aspectVal;
070: protected long minDelta;
071: protected FileWriter fw;
072: protected int expectedTask, receivedTask;
073:
074: /**
075: * parsing the plugIn arguments and setting the values
076: */
077: protected void parseParameter() {
078: Vector p = getParameters();
079: CPUCONSUME = getParameterIntValue(p, "CPUCONSUME");
080: MESSAGESIZE = getParameterIntValue(p, "MESSAGESIZE");
081: FILENAME = getParameterValue(p, "FILENAME");
082: MAXCOUNT = getParameterIntValue(p, "MAXCOUNT");
083: OUTSTANDING_MESSAGES = getParameterIntValue(p,
084: "OUTSTANDING_MESSAGES");
085: DEBUG = getParameterBooleanValue(p, "DEBUG");
086: LOG = getParameterBooleanValue(p, "LOG");
087: BURST_TIME = getParameterIntValue(p, "BURST_TIME");
088: VERB = getParameterValue(p, "VERB");
089: }
090:
091: public UnaryPredicate myAllocationPredicate = new UnaryPredicate() {
092: public boolean execute(Object o) {
093: if (o instanceof Allocation) {
094: Task t = ((Allocation) o).getTask();
095: return (t != null)
096: && (t.getVerb().equals(Verb.get(VERB)));
097: }
098: return false;
099: }
100: };
101:
102: /**
103: * Using setupSubscriptions to create the initial CODE tasks
104: */
105: protected void setupSubscriptions() {
106: parseParameter(); //read the plugIn arguments
107: debug(DEBUG, "ManagerPlugin: setupSubscription entering loop");
108: addTask();
109: expectedTask = (int) t
110: .getPreferredValue(AspectType._ASPECT_COUNT);
111: allocations = (IncrementalSubscription) subscribe(myAllocationPredicate);
112: }
113:
114: protected void execute() {
115: // Process changed allocations
116: allocateChangedtasks(allocations.getChangedList());
117: }
118:
119: protected void allocateChangedtasks(Enumeration allo_enum) {
120: AllocationResult est, rep;
121: double val = 0;
122: Task task = null;
123:
124: while (allo_enum.hasMoreElements()) {
125: Allocation alloc = (Allocation) allo_enum.nextElement();
126: est = null;
127: rep = null;
128: task = null;
129: task = alloc.getTask();
130: est = alloc.getEstimatedResult();
131: rep = alloc.getReportedResult();
132: if (rep != null) {
133: receivedTask = (int) rep
134: .getValue(AspectType._ASPECT_COUNT);
135: if (receivedTask != expectedTask) {
136: System.out
137: .println("ERROR: expectedtask != receivedTask::"
138: + expectedTask + ":" + receivedTask);
139: } else {
140: printTheChange(receivedTask);
141: debug(DEBUG, task.getVerb()
142: + "=>expectedTask:received::"
143: + expectedTask + ":" + receivedTask);
144: waitFor(BURST_TIME);
145: for (int i = 0; i < OUTSTANDING_MESSAGES; i++) {
146: // This code was written in case one has to add instead of change
147: //addTask();sequenceNum++;
148: changeTasks();
149: }
150: }//else
151: }
152: breakFromLoop(count, MAXCOUNT);
153: }
154: }
155:
156: //#############HELPER FUNCTIONS
157: protected void addTask() {
158: publishAsset(what_to_code, "The next Killer App",
159: "e something java");
160: t = makeTask(what_to_code, VERB);
161: setPreference(t, AspectType._ASPECT_COUNT, sequenceNum);
162: publishAdd(t);
163: }
164:
165: public void publishAsset(Asset asset, String nameOfAsset,
166: String itemIdentification) {
167: asset = theLDMF.createPrototype("AbstractAsset", nameOfAsset);
168: NewItemIdentificationPG iipg = (NewItemIdentificationPG) theLDMF
169: .createPropertyGroup("ItemIdentificationPG");
170: iipg.setItemIdentification(itemIdentification);
171: asset.setItemIdentificationPG(iipg);
172: publishAdd(asset);
173: }
174:
175: protected void setPreference(Task t, int aspectType,
176: int sequenceOfTask) {
177: startTime = new Date();
178: aspectVal = AspectValue.newAspectValue(aspectType,
179: sequenceOfTask);
180: ScoringFunction scorefcn = ScoringFunction
181: .createStrictlyAtValue(aspectVal);
182: Preference pref = theLDMF.newPreference(aspectType, scorefcn);
183: ((NewTask) t).setPreference(pref);
184: }
185:
186: /**
187: * Create a CODE task.
188: * @param what the direct object of the task
189: */
190: protected Task makeTask(Asset what, String verb) {
191: NewTask new_task = theLDMF.newTask();
192: new_task.setVerb(Verb.get(verb));// Set the verb as given
193: // Set the reality plan for the task
194: new_task.setPlan(theLDMF.getRealityPlan());
195: new_task.setDirectObject(what);
196: NewPrepositionalPhrase npp = theLDMF.newPrepositionalPhrase();
197: npp.setPreposition("USING_LANGUAGE");
198: if (MESSAGESIZE == -1)
199: npp.setIndirectObject(alterMessageSize(0));
200: else
201: npp.setIndirectObject(alterMessageSize(MESSAGESIZE));
202: new_task.setPrepositionalPhrases(npp);
203: return new_task;
204: }
205:
206: protected void changeTasks() {
207: if (CPUCONSUME != -1) //i.e. cpuconsume passed to plugin as a arg
208: consumeCPU(CPUCONSUME);
209: startTime = new Date();
210: if (t.getVerb().equals(VERB))
211: sequenceNum++;
212: setPreference(t, AspectType._ASPECT_COUNT, sequenceNum);
213: publishChange(t);
214: expectedTask++;
215: }
216:
217: protected void printTheChange(int taskCount) {
218: Date endTime = new Date();
219: long delta = endTime.getTime() - startTime.getTime();
220: if (count == 1)
221: minDelta = delta;
222: else
223: minDelta = Math.min(minDelta, delta);
224: String msg = t.getVerb() + "=>" + taskCount + "," + delta + ","
225: + minDelta;
226: log(LOG, FILENAME, fw, msg);
227: count++;
228: }
229: }
|