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 57 public class BehaviorExecutionPooledThread extends Thread { 58 static protected Logger log = IsacScenarioEngine.logger 60 .getLogger(BehaviorExecutionPooledThread.class.getName()); 61 62 private Object suspendLock; 65 66 private Object poolLock; 67 68 private Object groupExecutionLock; 69 70 private Object timerLock; 71 72 private Object activitiesLock; 73 74 private Object dataControlWrite_lock; 75 76 private DataCollectorWrite dataControlWrite; 78 79 private volatile BooleanHolder stopped; 81 82 private volatile BooleanHolder suspended; 83 84 private Vector behaviorsThreads; 86 87 private IntHolder behaviorsThreadsWaiting; 90 91 private IntHolder behaviorsThreadsRunningRequired; 94 95 private IntHolder behaviorsThreadAliveNumber; 96 97 private boolean localStopped; 99 100 private ExecutableNode executableNode; 102 103 private SessionObjectHashtable sessionsObjectsTable; 105 106 private Vector testsPile; 108 109 private int threadId; 111 112 private int groupId; 114 115 private String bladeId; 116 117 154 public BehaviorExecutionPooledThread(String bladeId, int threadId, 155 ExecutableNode executableNode, SessionObjectHashtable sot, Object sl, 156 Object pl, Object tl, Object al, Object gel, 157 DataCollectorWrite dcw, Object dcwl, BooleanHolder stopped, 158 BooleanHolder suspended, Vector bt, IntHolder btw, IntHolder btrr, 159 IntHolder btan) 160 { 161 this.bladeId = bladeId; 162 this.threadId = threadId; 163 this.executableNode = executableNode; 164 this.sessionsObjectsTable = sot; 165 this.suspendLock = sl; 166 this.poolLock = pl; 167 this.timerLock = tl; 168 this.activitiesLock = al; 169 this.groupExecutionLock = gel; 170 this.dataControlWrite = dcw; 171 this.dataControlWrite_lock = dcwl; 172 this.stopped = stopped; 173 this.suspended = suspended; 174 this.behaviorsThreads = bt; 175 this.behaviorsThreadsWaiting = btw; 176 this.behaviorsThreadsRunningRequired = btrr; 177 this.behaviorsThreadAliveNumber = btan; 178 this.localStopped = false; 180 this.testsPile = new Vector (); 182 } 183 184 192 public void run() { 193 synchronized (this.groupExecutionLock) { 195 this.behaviorsThreadAliveNumber 196 .setIntValue(this.behaviorsThreadAliveNumber.getIntValue() + 1); 197 } 198 while (!this.stopped.getBooleanValue()) { 200 synchronized (this.activitiesLock) { 201 synchronized (this.poolLock) { 202 this.behaviorsThreads.remove(this); 204 if (this.suspended.getBooleanValue()) { 206 if (this.behaviorsThreadsWaiting.getIntValue() == this.behaviorsThreads 207 .size()) { 208 this.poolLock.notify(); 213 } 214 } 215 if (this.localStopped) { 217 this.behaviorsThreadsRunningRequired 219 .setIntValue(this.behaviorsThreadsRunningRequired 220 .getIntValue() - 1); 221 if (this.behaviorsThreadsRunningRequired.getIntValue() == 0) { 222 this.poolLock.notify(); 225 } 226 this.localStopped = false; 228 } 229 } 230 231 log.log(BasicLevel.DEBUG, "\t\t-- BET -- WAIT WORK TO DO"); 232 try { 234 this.activitiesLock.wait(); 235 } catch (InterruptedException ex) { 236 log.log(BasicLevel.WARN, 238 "The thread have beeen interrupted, so stop it"); 239 break; 240 } 241 } 242 synchronized (this.poolLock) { 243 if (this.stopped.getBooleanValue()) 245 break; 246 247 this.behaviorsThreads.add(this); 249 250 this.behaviorsThreadsRunningRequired 252 .setIntValue(this.behaviorsThreadsRunningRequired 253 .getIntValue() - 1); 254 if (this.behaviorsThreadsRunningRequired.getIntValue() == 0) { 255 this.poolLock.notify(); 257 } 258 } 259 this.executeNode(this.executableNode); 261 sessionsObjectsTable.reset() ; 263 } sessionsObjectsTable.close() ; 267 log.log(BasicLevel.DEBUG, "\t\t-- BET --" + "WAIT FOR DIEING"); 269 synchronized (this.groupExecutionLock) { 270 this.behaviorsThreadAliveNumber 271 .setIntValue(this.behaviorsThreadAliveNumber.getIntValue() - 1); 272 if (this.behaviorsThreadAliveNumber.getIntValue() == 0) { 273 log.log(BasicLevel.DEBUG, "\t\t-- BET --" 274 + "WE ARE THE LAST ONE NOTIFY THE GROUP..."); 275 this.groupExecutionLock.notify(); 278 } 279 } 280 log.log(BasicLevel.WARN, "This thread DIE !!!"); 281 } 282 283 289 private void executeNode(ExecutableNode node) { 290 log.log(BasicLevel.DEBUG, "\t\t-- BET -- executeNode"); 291 for (int i = 0; i < this.testsPile.size(); i++) { 293 if (!this 295 .executeTest((TestDescription) this.testsPile.elementAt(i))) 296 return; 297 } 298 boolean executeChildren = false; 301 String type = node.getType(); 303 if (type.equals(Node.SAMPLE)) { 305 executeChildren = executeSample(node); 306 } 307 else if (type.equals(Node.TIMER)) { 309 executeChildren = executeTimer(node); 310 } 311 else if (type.equals(Node.IF)) { 313 executeChildren = executeIf(node); 314 } 315 else if (type.equals(Node.WHILE)) { 317 executeChildren = executeWhile(node); 318 } 319 else if (type.equals(Node.PREEMPTIVE)) { 321 executeChildren = executePreemptive(node); 322 } 323 else if (type.equals(Node.NCHOICE)) { 325 executeChildren = executeNChoice(node); 326 } 327 else if (type.equals(Node.CHOICE) || type.equals(Node.THEN) 329 || type.equals(Node.ELSE) || type.equals(Node.BEHAVIOR)) { 330 executeChildren = true; 331 } 332 else { 334 throw new IsacRuntimeException( 335 "This node type is UNKNOW, could not execute it : " + type); 336 } 337 if (executeChildren) { 339 for (int i = 0; i < node.getChildren().size() && !this.localStopped 340 && !this.stopped.getBooleanValue(); i++) { 341 this.executeSuspendIfNeeded(); 342 this.executeNode((ExecutableNode) node.getChildren().elementAt( 343 i)); 344 } 345 } 346 } 347 348 355 private boolean executeSample(ExecutableNode node) { 356 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute sample : "); 357 SampleDescription desc = (SampleDescription) node.getDescription(); 359 Object sessionObject = this.sessionsObjectsTable.get(desc 361 .getSessionObjectId()); 362 Hashtable params = desc.getParams(); 363 int nb = desc.getMethodNumber(); 364 ActionEvent report = ((SampleAction) sessionObject).doSample(nb, 366 params, new ActionEvent(System.currentTimeMillis(), bladeId, null, 0, 367 threadId, true, 0, null, "")); 368 if (report != null) 370 { 371 synchronized (this.dataControlWrite_lock) { 372 if (this.dataControlWrite != null) 373 this.dataControlWrite.add(report); 374 } 375 } 376 return false; 379 } 380 381 388 private boolean executeTimer(ExecutableNode node) { 389 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute timer : "); 390 TimerDescription desc = (TimerDescription) node.getDescription(); 392 Object sessionObject = this.sessionsObjectsTable.get(desc 394 .getSessionObjectId()); 395 Hashtable params = desc.getParams(); 396 int nb = desc.getMethodNumber(); 397 long waitingTime = ((TimerAction) sessionObject).doTimer(nb, params); 399 this.executeWaitTime(waitingTime); 401 return false; 404 } 405 406 414 private boolean executeIf(ExecutableNode node) { 415 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute if : "); 416 boolean condition = executeTest(((IfDescription) node.getDescription()) 418 .getCondition()); 419 log.log(BasicLevel.DEBUG, "\t\t-- BET -- condition : " + condition); 420 ExecutableNode thenNode = null; 422 ExecutableNode elseNode = null; 423 for (int i = 0; i < node.getChildren().size() && !this.localStopped 425 && !this.stopped.getBooleanValue(); i++) { 426 this.executeSuspendIfNeeded(); 427 ExecutableNode child = (ExecutableNode) node.getChildren() 429 .elementAt(i); 430 if (child.getType().equals(Node.THEN)) 431 thenNode = child; 432 else if (child.getType().equals(Node.ELSE)) 433 elseNode = child; 434 else 435 throw new IsacRuntimeException("Unexpected node type : " 436 + child.getType()); 437 } 438 if (this.localStopped || this.stopped.getBooleanValue()) { 440 return false; 441 } 442 log.log(BasicLevel.DEBUG, "\t\t-- BET -- then node : " + thenNode); 443 if (condition) 445 this.executeNode(thenNode); 446 else if (elseNode != null) 447 this.executeNode(elseNode); 448 return false; 450 } 451 452 460 private boolean executeWhile(ExecutableNode node) { 461 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute while : "); 462 boolean condition = executeTest(((WhileDescription) node 464 .getDescription()).getCondition()); 465 while (condition && !stopped.getBooleanValue() && !localStopped) { 467 for (int i = 0; i < node.getChildren().size() && !this.localStopped 469 && !this.stopped.getBooleanValue(); i++) { 470 this.executeSuspendIfNeeded(); 471 this.executeNode((ExecutableNode) node.getChildren().elementAt( 472 i)); 473 } 474 condition = executeTest(((WhileDescription) node.getDescription()) 476 .getCondition()); 477 } 478 return false; 480 } 481 482 490 private boolean executePreemptive(ExecutableNode node) { 491 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute preemptive : "); 492 this.testsPile.add(((PreemptiveDescription) node.getDescription()) 494 .getCondition()); 495 for (int i = 0; i < node.getChildren().size() && !this.localStopped 498 && !this.stopped.getBooleanValue(); i++) { 499 this.executeSuspendIfNeeded(); 500 this.executeNode((ExecutableNode) node.getChildren().elementAt(i)); 501 } 502 this.testsPile.remove(node.getDescription()); 504 return false; 506 } 507 508 516 private boolean executeNChoice(ExecutableNode node) { 517 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute nchoice : "); 518 int[] probabilityRange = new int[node.getChildren().size()]; 520 int totalPorbablities = 0; 522 for (int i = 0; i < node.getChildren().size(); i++) { 524 int proba = ((ChoiceDescription) ((ExecutableNode) node 525 .getChildren().elementAt(i)).getDescription()) 526 .getProbability(); 527 totalPorbablities += proba; 528 probabilityRange[i] = totalPorbablities; 529 } 530 int random = (int) (Math.random() * totalPorbablities); 532 for (int i = 0; i < probabilityRange.length; i++) { 534 if (random < probabilityRange[i]) { 536 if (!this.localStopped && !this.stopped.getBooleanValue()) { 537 this.executeSuspendIfNeeded(); 538 this.executeNode((ExecutableNode) node.getChildren() 540 .elementAt(i)); 541 } 542 return false; 544 } 545 } 546 throw new IsacRuntimeException( 548 "Unable to make a choice, no branch was selected"); 549 } 550 551 558 private boolean executeTest(TestDescription td) { 559 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute test"); 560 Object sessionObject = this.sessionsObjectsTable.get(td 562 .getSessionObjectId()); 563 Hashtable params = td.getParams(); 564 int nb = td.getMethodNumber(); 565 return ((TestAction) sessionObject).doTest(nb, params); 567 } 568 569 577 private void executeWaitTime(long duration) { 578 if (IsacScenarioEngine.DEBUG_ON && log.isLoggable(BasicLevel.DEBUG)) { 579 log.log(BasicLevel.DEBUG, "\t\t-- BET -- execute wait : " 580 + duration); 581 } 582 long ellapsedTime = 0; 584 while (ellapsedTime < duration && !stopped.getBooleanValue() 587 && !localStopped) { 588 this.executeSuspendIfNeeded(); 590 long startTimerTime = System.currentTimeMillis(); 592 synchronized (this.timerLock) { 594 try { 595 this.timerLock.wait(duration - ellapsedTime); 596 } catch (InterruptedException ex) { 597 throw new IsacRuntimeException("-- BET" + threadId + ":" 598 + groupId + " Unable to execute timer", ex); 599 } 600 } 601 ellapsedTime += System.currentTimeMillis() - startTimerTime; 606 } 607 } 608 609 613 private void executeSuspendIfNeeded() { 614 if (this.suspended.getBooleanValue()) { 616 log 617 .log(BasicLevel.WARN, 618 "\t\t-- BET -- WE MANAGE TO ENTER SUSPEND"); 619 synchronized (this.suspendLock) { 620 log.log(BasicLevel.WARN, 621 "\t\t-- BET -- WE MANAGE TO ENTER groupexec LOCK"); 622 synchronized (this.poolLock) { 623 this.behaviorsThreadsWaiting 625 .setIntValue(this.behaviorsThreadsWaiting 626 .getIntValue() + 1); 627 log.log(BasicLevel.WARN, "\t\t-- BET -- threads running=" 628 + this.behaviorsThreads.size() + " waiting=" 629 + this.behaviorsThreadsWaiting.getIntValue()); 630 if (this.behaviorsThreadsWaiting.getIntValue() == this.behaviorsThreads 631 .size()) { 632 log.log(BasicLevel.WARN, 634 "\t\t-- BET -- awake GROUPPPPPPPPP"); 635 this.poolLock.notify(); 636 } 637 } 638 log.log(BasicLevel.WARN, "\t\t-- BET -- do SUSPEND"); 639 try { 640 this.suspendLock.wait(); 641 } catch (InterruptedException ex) { 642 throw new IsacRuntimeException( 643 "Unable to wait during the suspended state",ex); 644 } 645 synchronized (this.poolLock) { 646 this.behaviorsThreadsWaiting 648 .setIntValue(this.behaviorsThreadsWaiting 649 .getIntValue() - 1); 650 if (this.behaviorsThreadsWaiting.getIntValue() == 0) { 651 this.poolLock.notify(); 655 } 656 } 657 } 658 } 659 } 660 661 665 public void setLocalStopped(boolean localStopped) { 666 this.localStopped = localStopped; 667 } 668 } | Popular Tags |