1 23 package org.objectweb.clif.scenario.util.isac.engine.behavior; 24 25 import java.util.Hashtable ; 26 import java.util.Vector ; 27 28 import org.objectweb.clif.datacollector.api.DataCollectorWrite; 29 import org.objectweb.clif.scenario.util.isac.engine.IsacScenarioEngine; 30 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.ExecutableNode; 31 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.ChoiceDescription; 32 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.IfDescription; 33 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.PreemptiveDescription; 34 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.SampleDescription; 35 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.TestDescription; 36 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.TimerDescription; 37 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.WhileDescription; 38 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException; 39 import org.objectweb.clif.scenario.util.isac.plugin.SampleAction; 40 import org.objectweb.clif.scenario.util.isac.plugin.TestAction; 41 import org.objectweb.clif.scenario.util.isac.plugin.TimerAction; 42 import org.objectweb.clif.scenario.util.isac.util.BooleanHolder; 43 import org.objectweb.clif.scenario.util.isac.util.SessionObjectHashtable; 44 import org.objectweb.clif.scenario.util.isac.util.IntHolder; 45 import org.objectweb.clif.scenario.util.isac.util.tree.Node; 46 import org.objectweb.clif.storage.api.ActionEvent; 47 import org.objectweb.util.monolog.api.BasicLevel; 48 import org.objectweb.util.monolog.api.Logger; 49 50 58 64 public class BehaviorExecutionThread extends Thread { 65 static protected Logger log = IsacScenarioEngine.logger 67 .getLogger(BehaviorExecutionThread.class.getName()); 68 69 private Object scenarioLock; 72 73 private Object activitiesLock; 74 75 private Object timerLock; 76 77 private Object dataControlWrite_lock; 78 79 private DataCollectorWrite dataControlWrite; 81 82 private volatile BooleanHolder started; 84 85 private volatile BooleanHolder stopped; 86 87 private volatile BooleanHolder suspended; 88 89 private volatile Vector behaviorsThreads; 91 92 private volatile IntHolder behaviorsThreadsWaiting; 95 96 private volatile IntHolder behaviorsThreadsRunningRequired; 99 100 private boolean localStopped; 102 103 private ExecutableNode executableNode; 105 106 private SessionObjectHashtable sessionsObjectsTable; 108 109 private Vector testsPile; 111 112 private int threadId; 114 115 private int groupId; 117 118 private String bladeId; 119 120 154 public BehaviorExecutionThread(String bladeId, int threadId, ExecutableNode executableNode, 155 SessionObjectHashtable sot, Object sl, Object al, Object tl, 156 DataCollectorWrite dcw, Object dcwl, BooleanHolder started, 157 BooleanHolder stopped, BooleanHolder suspended, Vector bt, 158 IntHolder btw, IntHolder btrr) { 159 this.bladeId = bladeId; 160 this.threadId = threadId; 161 this.executableNode = executableNode; 162 this.sessionsObjectsTable = sot; 163 this.scenarioLock = sl; 164 this.activitiesLock = al; 165 this.timerLock = tl; 166 this.dataControlWrite = dcw; 167 this.dataControlWrite_lock = dcwl; 168 this.started = started; 169 this.stopped = stopped; 170 this.suspended = suspended; 171 this.behaviorsThreads = bt; 172 this.behaviorsThreadsWaiting = btw; 173 this.behaviorsThreadsRunningRequired = btrr; 174 this.localStopped = false; 176 this.testsPile = new Vector (); 178 } 179 180 187 public void run() { 188 if (!this.localStopped && !this.stopped.getBooleanValue()) { 190 this.executeSuspendIfNeeded(); 192 this.executeNode(this.executableNode); 194 } 195 synchronized (this.activitiesLock) { 198 this.behaviorsThreads.remove(this); 199 if (this.stopped.getBooleanValue() || this.localStopped) { 203 if (behaviorsThreads.size() == this.behaviorsThreadsRunningRequired 207 .getIntValue()) { 208 this.activitiesLock.notifyAll(); 209 } 210 } 211 else { 213 } 215 } 216 } 217 218 224 private void executeNode(ExecutableNode node) { 225 log.log(BasicLevel.DEBUG, "\t\t-- BET -- executeNode"); 226 for (int i = 0; i < this.testsPile.size(); i++) { 228 if (!this 230 .executeTest((TestDescription) this.testsPile.elementAt(i))) 231 return; 232 } 233 boolean executeChildren = false; 236 String type = node.getType(); 238 if (type.equals(Node.SAMPLE)) { 240 executeChildren = executeSample(node); 241 } 242 else if (type.equals(Node.TIMER)) { 244 executeChildren = executeTimer(node); 245 } 246 else if (type.equals(Node.IF)) { 248 executeChildren = executeIf(node); 249 } 250 else if (type.equals(Node.WHILE)) { 252 executeChildren = executeWhile(node); 253 } 254 else if (type.equals(Node.PREEMPTIVE)) { 256 executeChildren = executePreemptive(node); 257 } 258 else if (type.equals(Node.NCHOICE)) { 260 executeChildren = executeNChoice(node); 261 } 262 else if (type.equals(Node.CHOICE) || type.equals(Node.THEN) 264 || type.equals(Node.ELSE) || type.equals(Node.BEHAVIOR)) { 265 executeChildren = true; 266 } 267 else { 269 throw new IsacRuntimeException( 270 "This node type is UNKNOW, could not execute it : " + type); 271 } 272 if (executeChildren) { 274 for (int i = 0; i < node.getChildren().size() && !this.localStopped 275 && !this.stopped.getBooleanValue(); i++) { 276 this.executeSuspendIfNeeded(); 277 this.executeNode((ExecutableNode) node.getChildren().elementAt( 278 i)); 279 } 280 } 281 } 282 283 290 private boolean executeSample(ExecutableNode node) { 291 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute sample : "); 292 SampleDescription desc = (SampleDescription) node.getDescription(); 294 Object sessionObject = this.sessionsObjectsTable.get(desc 296 .getSessionObjectId()); 297 Hashtable params = desc.getParams(); 298 int nb = desc.getMethodNumber(); 299 ActionEvent report = ((SampleAction) sessionObject).doSample(nb, 301 params, new ActionEvent(System.currentTimeMillis(), bladeId, null, 0, 302 threadId, true, 0, null, "")); 303 if (report != null) 305 { 306 synchronized (this.dataControlWrite_lock) { 307 if (this.dataControlWrite != null) 308 this.dataControlWrite.add(report); 309 } 310 } 311 return false; 314 } 315 316 323 private boolean executeTimer(ExecutableNode node) { 324 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute timer : "); 325 TimerDescription desc = (TimerDescription) node.getDescription(); 327 Object sessionObject = this.sessionsObjectsTable.get(desc 329 .getSessionObjectId()); 330 Hashtable params = desc.getParams(); 331 int nb = desc.getMethodNumber(); 332 long waitingTime = ((TimerAction) sessionObject).doTimer(nb, params); 334 this.executeWaitTime(waitingTime); 336 return false; 339 } 340 341 349 private boolean executeIf(ExecutableNode node) { 350 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute if : "); 351 boolean condition = executeTest(((IfDescription) node.getDescription()) 353 .getCondition()); 354 ExecutableNode thenNode = null; 356 ExecutableNode elseNode = null; 357 for (int i = 0; i < node.getChildren().size() && !this.localStopped 359 && !this.stopped.getBooleanValue(); i++) { 360 this.executeSuspendIfNeeded(); 361 ExecutableNode child = (ExecutableNode) node.getChildren() 363 .elementAt(i); 364 if (child.getType().equals(Node.THEN)) 365 thenNode = child; 366 else if (child.getType().equals(Node.ELSE)) 367 elseNode = child; 368 else 369 throw new IsacRuntimeException("Unexpected node type : " 370 + child.getType()); 371 } 372 if (this.localStopped || this.stopped.getBooleanValue()) { 374 return false; 375 } 376 if (condition) 378 this.executeNode(thenNode); 379 else if (elseNode != null) 380 this.executeNode(elseNode); 381 return false; 383 } 384 385 393 private boolean executeWhile(ExecutableNode node) { 394 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute while : "); 395 boolean condition = executeTest(((WhileDescription) node 397 .getDescription()).getCondition()); 398 while (condition && !stopped.getBooleanValue() && !localStopped) { 400 for (int i = 0; i < node.getChildren().size() && !this.localStopped 402 && !this.stopped.getBooleanValue(); i++) { 403 this.executeSuspendIfNeeded(); 404 this.executeNode((ExecutableNode) node.getChildren().elementAt( 405 i)); 406 } 407 condition = executeTest(((WhileDescription) node.getDescription()) 409 .getCondition()); 410 } 411 return false; 413 } 414 415 423 private boolean executePreemptive(ExecutableNode node) { 424 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute preemptive : "); 425 this.testsPile.add(((PreemptiveDescription) node.getDescription()) 427 .getCondition()); 428 for (int i = 0; i < node.getChildren().size() && !this.localStopped 431 && !this.stopped.getBooleanValue(); i++) { 432 this.executeSuspendIfNeeded(); 433 this.executeNode((ExecutableNode) node.getChildren().elementAt(i)); 434 } 435 this.testsPile.remove(node.getDescription()); 437 return false; 439 } 440 441 449 private boolean executeNChoice(ExecutableNode node) { 450 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute nchoice : "); 451 int[] probabilityRange = new int[node.getChildren().size()]; 453 int totalPorbablities = 0; 455 for (int i = 0; i < node.getChildren().size(); i++) { 457 int proba = ((ChoiceDescription) ((ExecutableNode) node 458 .getChildren().elementAt(i)).getDescription()) 459 .getProbability(); 460 totalPorbablities += proba; 461 probabilityRange[i] = totalPorbablities; 462 } 463 int random = (int) (Math.random() * totalPorbablities); 465 for (int i = 0; i < probabilityRange.length; i++) { 467 if (random < probabilityRange[i]) { 469 if (!this.localStopped && !this.stopped.getBooleanValue()) { 470 this.executeSuspendIfNeeded(); 471 this.executeNode((ExecutableNode) node.getChildren() 473 .elementAt(i)); 474 } 475 return false; 477 } 478 } 479 throw new IsacRuntimeException( 481 "Unable to make a choice, no branch was selected"); 482 } 483 484 491 private boolean executeTest(TestDescription td) { 492 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute test"); 493 Object sessionObject = this.sessionsObjectsTable.get(td 495 .getSessionObjectId()); 496 Hashtable params = td.getParams(); 497 int nb = td.getMethodNumber(); 498 return ((TestAction) sessionObject).doTest(nb, params); 500 } 501 502 510 private void executeWaitTime(long duration) { 511 if (IsacScenarioEngine.DEBUG_ON && log.isLoggable(BasicLevel.DEBUG)) { 512 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute wait : " 513 + duration); 514 } 515 long ellapsedTime = 0; 517 while (ellapsedTime < duration && !stopped.getBooleanValue() 520 && !localStopped) { 521 this.executeSuspendIfNeeded(); 523 long startTimerTime = System.currentTimeMillis(); 525 synchronized (this.timerLock) { 527 try { 528 this.timerLock.wait(duration - ellapsedTime); 529 } catch (InterruptedException ex) { 530 throw new IsacRuntimeException("-- BET" + threadId + ":" 531 + groupId + " Unable to execute timer",ex); 532 } 533 } 534 ellapsedTime += System.currentTimeMillis() - startTimerTime; 539 } 540 } 541 542 546 private void executeSuspendIfNeeded() { 547 if (this.suspended.getBooleanValue()) { 549 log 550 .log(BasicLevel.WARN, 551 "\t\t-- BET -- WE MANAGE TO ENTER SUSPEND"); 552 synchronized (this.scenarioLock) { 553 synchronized (this.activitiesLock) { 554 this.behaviorsThreadsWaiting 556 .setIntValue(this.behaviorsThreadsWaiting 557 .getIntValue() + 1); 558 log.log(BasicLevel.WARN, "\t\t-- BET -- threads running=" 559 + this.behaviorsThreads.size() + " waiting=" 560 + this.behaviorsThreadsWaiting.getIntValue()); 561 if (this.behaviorsThreadsWaiting.getIntValue() == this.behaviorsThreads 562 .size()) { 563 log.log(BasicLevel.WARN, 567 "\t\t-- BET -- awake GROUPPPPPPPPP"); 568 this.activitiesLock.notifyAll(); 569 } 570 } 571 log.log(BasicLevel.WARN, "\t\t-- BET -- do SUSPEND"); 572 try { 573 this.scenarioLock.wait(); 574 } catch (InterruptedException ex) { 575 throw new IsacRuntimeException( 576 "Unable to wait during the suspended state",ex); 577 } 578 synchronized (this.activitiesLock) { 579 this.behaviorsThreadsWaiting 581 .setIntValue(this.behaviorsThreadsWaiting 582 .getIntValue() - 1); 583 if (this.behaviorsThreadsWaiting.getIntValue() == 0) { 584 this.activitiesLock.notifyAll(); 588 } 589 } 590 } 591 } 592 } 593 594 598 public void setLocalStopped(boolean localStopped) { 599 this.localStopped = localStopped; 600 } 601 } | Popular Tags |