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.mlm.plugin.perturbation;
027:
028: import java.util.Calendar;
029: import java.util.StringTokenizer;
030: import java.util.Vector;
031:
032: import org.cougaar.core.blackboard.Subscriber;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035:
036: /**
037: * The PerturbationNode class encapsulates all of the information
038: * the PerturbationScheduler needs in or to manage the scheduling
039: * and execution of each of the specified perturbations.
040: */
041: public class PerturbationNode {
042: public static final String OBJECT = "object";
043: public static final String START = "start";
044: public static final String CYCLES = "cycles";
045: public static final String PERIODICITY = "periodicity";
046:
047: public Runnable job;
048: public long executeAt;
049: public long interval;
050: public int count;
051: public Subscriber subscriber;
052:
053: public String threadId;
054: /**@shapeType AggregationLink
055: @associates <b>Perturbation</b>*/
056: private Vector lnkUnnamed1;
057: private PerturbationPlugin pertPlugin_;
058:
059: /**
060: * @param perturbationData XML perturbation data node
061: * @param subscriber The asset subscriber
062: */
063: public PerturbationNode(Node perturbationData,
064: Subscriber subscriber, PerturbationPlugin pertPlugin) {
065: this .pertPlugin_ = pertPlugin;
066: this .subscriber = subscriber;
067: setJob(perturbationData);
068: parseData(perturbationData);
069: }
070:
071: public PerturbationPlugin getPerturbationPlugin() {
072: return pertPlugin_;
073: }
074:
075: /**
076: * Sets the perturbation job.
077: * @param jobData The perturbation in data in XML to be run
078: */
079: public void setJob(Node jobData) {
080: Perturbation tempJob = new Perturbation(jobData, pertPlugin_);
081: tempJob.setSubscriber(subscriber);
082: job = (Runnable) tempJob;
083:
084: }
085:
086: /**
087: * Sets the perturbation job.
088: */
089: public void setJob(Runnable theJob) {
090: job = theJob;
091: }
092:
093: /**
094: * Sets the execution time of the job.
095: * @param theTime The execution time of the job
096: */
097: public void setExecutionTime(String theTime) {
098: Calendar executionTime;
099: StringTokenizer tokenizer;
100:
101: threadId = theTime;
102: int index = 0;
103: int timeAmount = 0;
104:
105: // Get the current scenario time
106: long currentMills = subscriber.getClient().currentTimeMillis();
107: // MML Check to see if the start time is specified in hours
108: // Set the execution time accordingly.
109: if ((index = threadId.indexOf("hours")) != -1) {
110: timeAmount = (new Integer((threadId.substring(0, index))
111: .trim())).intValue();
112: executeAt = currentMills + ((timeAmount * 60 * 60) * 100);
113: }// if statement
114: else if ((index = threadId.indexOf("mins")) != -1) {
115: timeAmount = (new Integer((threadId.substring(0, index))
116: .trim())).intValue();
117: executeAt = currentMills + ((timeAmount * 60) * 100);
118: }// if statement
119: else if ((index = threadId.indexOf("secs")) != -1) {
120: timeAmount = (new Integer((threadId.substring(0, index))
121: .trim())).intValue();
122: executeAt = currentMills + (timeAmount * 100);
123: }// if statement
124: // If the time interval is a CDAY
125: else if ((index = threadId.indexOf("CDAY")) != -1) {
126: // Search for the plus sign
127: if ((index = threadId.indexOf("+")) != -1) {
128: timeAmount = (new Integer((threadId.substring(
129: index + 1, threadId.length())).trim()))
130: .intValue();
131: executeAt = currentMills
132: + ((((timeAmount * 24) * 60) * 60) * 100);
133: }
134: }// if statement
135: // If the time interval is in milliseconds
136: else if ((index = threadId.indexOf("milliseconds")) != -1) {
137: timeAmount = (new Integer((threadId.substring(0, index))
138: .trim())).intValue();
139: executeAt = currentMills + timeAmount;
140: }// if statement
141: }
142:
143: /**
144: * Sets the execution time of the job.
145: * @param theTime The execution time of the job
146: */
147: public void setExecutionTime(long theTime) {
148: executeAt = theTime;
149: }
150:
151: /**
152: * Returns the execution time of the job.
153: */
154: public long getExecutionTime() {
155: return executeAt;
156: }
157:
158: /**
159: * Sets the interval of the job.
160: * @param theInterval The interval of the job
161: */
162: public void setInterval(String theInterval) {
163: setInterval((new Long(theInterval)).longValue());
164: setExecutionTime(System.currentTimeMillis() + interval);
165: }
166:
167: /**
168: * Sets the interval of the job.
169: * @param theInterval The interval of the job
170: */
171: public void setInterval(long theInterval) {
172: interval = theInterval;
173: }
174:
175: /**
176: * Sets the number of times to repeat the perturbation.
177: * @param theCount Number of times to repeat the perturbation
178: */
179: public void setCount(String theCount) {
180: setCount((new Integer(theCount)).intValue());
181: }
182:
183: /**
184: * Sets the number of times to repeat the perturbation.
185: * @param theCount Number of times to repeat the perturbation
186: */
187: public void setCount(int theCount) {
188: count = theCount;
189: }
190:
191: /**
192: * Parses the perturbation data.
193: * @param node XML perturbation data node
194: */
195: public void parseData(Node node) {
196:
197: // is there anything to do?
198: if (node == null) {
199: return;
200: }
201:
202: realParseData(node, "firstrun");
203: }
204:
205: /**
206: * Recursively parses the perturbation data.
207: * @param node XML perturbation data node
208: * @param previousName Previous XML node name
209: */
210: private void realParseData(Node node, String previousName) {
211: // Is there anything to do?
212: if (node == null) {
213: return;
214: }
215:
216: int type = node.getNodeType();
217: switch (type) {
218: // print element with attributes
219: case Node.ELEMENT_NODE: {
220: String tempName = node.getNodeName();
221: NodeList children = node.getChildNodes();
222: if (children != null) {
223: int len = children.getLength();
224: for (int i = 0; i < len; i++) {
225: realParseData(children.item(i), tempName);
226: }
227: }
228: break;
229: }
230:
231: // print text
232: case Node.TEXT_NODE: {
233: processIt(previousName, node.getNodeValue());
234: break;
235: }
236: }
237: } // realParseData(Node,String)
238:
239: /**
240: * Sets the appopriate perturbation value based on the
241: * XML tag.
242: * @param stuff XML perturbation data tag type
243: * @param value XML perturbation data tag value
244: */
245: protected void processIt(String stuff, String value) {
246:
247: if ((stuff != null) && (value != null)) {
248: // set the Executiontime
249: if (stuff.compareTo(START) == 0)
250: setExecutionTime(value);
251:
252: // set the Count
253: if (stuff.compareTo(CYCLES) == 0)
254: setCount(value);
255:
256: // set the Interval tied to this perturbation
257: if (stuff.compareTo(PERIODICITY) == 0)
258: setInterval(value);
259: }
260: }
261: }
|