1 27 28 package org.objectweb.clif.scenario.util.transitions; 29 30 import org.objectweb.clif.util.ClifClassLoader; 31 32 import java.io.BufferedReader ; 33 import java.io.FileNotFoundException ; 34 import java.io.FileReader ; 35 import java.io.IOException ; 36 import java.io.InputStreamReader ; 37 import java.util.NoSuchElementException ; 38 import java.util.Properties ; 39 import java.util.Random ; 40 import java.util.Stack ; 41 import java.util.StringTokenizer ; 42 43 59 public class TransitionTable implements Cloneable { 60 61 private int nbColumns; 62 63 private int nbRows; 64 65 private float transitions[][]; 67 68 private int transitionsTime[]; 70 71 private int beforeStep; 72 73 private Random rand = new Random (); 74 75 private Stack previousStates = new Stack (); 78 79 private int currentState = 0; 81 82 private boolean useMatrixThinkTime; 84 85 private boolean identifyByOrigin; 86 87 private String [] stateNames; 89 90 private String [] action; 92 93 private String [][] actionByOrigin; 94 95 private boolean isBackClicked; 96 97 private Properties prop = new Properties (); 98 99 private Transition trans = new Transition(); 100 101 private boolean DEBUG = false; 102 103 118 public TransitionTable(String filename, String actions_file, 119 boolean useMatrixThinkTime, boolean identifyByOrigin) { 120 121 this.useMatrixThinkTime = useMatrixThinkTime; 122 this.identifyByOrigin = identifyByOrigin; 123 readMatrixTextFile(filename); 124 readActionsTextFile(actions_file); 125 trans.setProperties(prop); 126 } 127 128 132 private TransitionTable() { 133 } 134 135 140 public TransitionTable createNewTransitionTable() { 141 TransitionTable copy = new TransitionTable(); 143 copy.nbColumns = this.nbColumns; 145 copy.nbRows = this.nbRows; 146 copy.transitions = new float[this.transitions.length][transitions[0].length]; 147 for (int i = 0; i < this.transitions.length; i++) { 148 for (int j = 0; j < this.transitions[i].length; j++) { 149 copy.transitions[i][j] = this.transitions[i][j]; 150 } 151 } 152 copy.transitionsTime = new int[this.transitionsTime.length]; 153 for (int i = 0; i < this.transitionsTime.length; i++) { 154 copy.transitionsTime[i] = this.transitionsTime[i]; 155 } 156 copy.beforeStep = this.beforeStep; 157 copy.rand = new Random (); 158 copy.previousStates = new Stack (); 160 copy.useMatrixThinkTime = this.useMatrixThinkTime; 161 copy.identifyByOrigin = this.identifyByOrigin; 162 copy.stateNames = new String [this.stateNames.length]; 163 for (int i = 0; i < this.stateNames.length; i++) { 164 copy.stateNames[i] = new String (this.stateNames[i]); 165 } 166 copy.action = new String [this.action.length]; 167 for (int i = 0; i < this.action.length; i++) { 168 copy.action[i] = new String (this.action[i]); 169 } 170 if (this.actionByOrigin != null) { 171 copy.actionByOrigin = new String [this.actionByOrigin.length][this.actionByOrigin[0].length]; 172 for (int i = 0; i < this.actionByOrigin.length; i++) { 173 if (this.actionByOrigin[i] != null) { 174 for (int j = 0; j < this.actionByOrigin[i].length; j++) { 175 copy.actionByOrigin[i][j] = this.actionByOrigin[i][j]; 176 } 177 } 178 } 179 } 180 copy.isBackClicked = this.isBackClicked; 181 copy.prop = new Properties (this.prop); 182 copy.trans = new Transition(); 183 return copy; 184 } 185 186 189 public void resetPreviousState() { 190 this.currentState = 0; 191 this.previousStates.clear(); 192 } 193 194 201 private boolean readMatrixTextFile(String filename) { 202 BufferedReader reader; 203 Float f; 204 Integer t; 205 206 try { 208 reader = new BufferedReader (new FileReader (filename)); 209 } catch (FileNotFoundException fnf) { 210 try { 211 reader = new BufferedReader ( 212 new InputStreamReader ( 213 ClifClassLoader.getClassLoader().getResourceAsStream(filename))); 214 } catch (Exception e) { 215 System.err.println("File " + filename + " not found. " + e); 216 return false; 217 } 218 } 219 220 try { 222 223 while (!reader.readLine().startsWith("#")) { 224 } 225 226 reader.readLine(); 228 StringTokenizer st = new StringTokenizer (reader.readLine(), "\t"); 230 231 nbColumns = st.countTokens(); 233 234 nbRows = nbColumns; 236 237 stateNames = new String [nbRows]; 238 transitions = new float[nbRows][nbColumns - 2]; 239 transitionsTime = new int[nbRows]; 240 241 for (int i = 0; i < nbRows; i++) { 243 st = new StringTokenizer (reader.readLine(), "\t"); 244 stateNames[i] = st.nextToken(); 246 247 for (int j = 0; j < nbColumns - 2; j++) { 249 f = new Float (st.nextToken()); 251 transitions[i][j] = f.floatValue(); 253 } 254 255 t = new Integer (st.nextToken()); 257 transitionsTime[i] = t.intValue(); 258 } 259 260 reader.close(); 261 262 } catch (IOException ioe) { 263 System.err.println("An error occured while reading " + filename 264 + ". (" + ioe.getMessage() + ")"); 265 return false; 266 } catch (NoSuchElementException nsu) { 267 System.err.println("File format error in file " + filename 268 + " Reason: " + nsu.getMessage()); 269 return false; 270 } catch (NumberFormatException ne) { 271 System.err.println("Number format error in file " + filename 272 + "Reason: " + ne.getMessage()); 273 return false; 274 } 275 276 return true; 277 } 278 279 285 private boolean readActionsTextFile(String filename) { 286 BufferedReader reader; 287 StringTokenizer st; 288 String line; 289 int j; 290 291 try { 293 reader = new BufferedReader (new FileReader (filename)); 294 } catch (FileNotFoundException fnf) { 295 try { 296 reader = new BufferedReader (new InputStreamReader (this 297 .getClass().getClassLoader().getResourceAsStream( 298 filename))); 299 } catch (Exception e) { 300 System.err.println("File " + filename + " not found. " + e); 301 return false; 302 } 303 } 304 305 try { 306 307 while (!(line = reader.readLine()).startsWith("#")) { 308 } 309 310 if (line.indexOf("Transition") != -1) { 313 while (!(line = reader.readLine()).startsWith("#")) { 314 } 315 } 316 if (line.indexOf("Variable") != -1) { 319 while (!(line = reader.readLine()).startsWith("#")) 320 loadProperty(line); 321 } 322 323 if (line.indexOf("Action") != -1) { 324 326 if (identifyByOrigin) 330 actionByOrigin = new String [nbColumns - 2][nbColumns - 2]; 331 else 332 action = new String [nbColumns - 2]; 333 334 if (identifyByOrigin) { 335 336 for (int i = 0; i < nbColumns - 2; i++) { 337 st = new StringTokenizer (reader.readLine(), "\t"); 338 st.nextToken(); j = 0; 340 while (st.hasMoreTokens()) { 341 actionByOrigin[i][j] = st.nextToken(); 342 j++; 343 } 344 } 345 } else { 346 347 for (int i = 0; i < nbColumns - 2; i++) { 348 st = new StringTokenizer (reader.readLine(), "\t"); 349 String temp = st.nextToken(); action[i] = st.nextToken(); 351 } 352 } 353 } 354 355 reader.close(); 356 357 } catch (IOException ioe) { 358 System.err.println("An error occured while reading " + filename 359 + ". (" + ioe.getMessage() + ")"); 360 return false; 361 } catch (NoSuchElementException nsu) { 362 System.err.println("File format error in file " + filename 363 + " Reason: " + nsu.getMessage()); 364 return false; 365 } catch (NumberFormatException ne) { 366 System.err.println("Number format error in file " + filename 367 + "Reason: " + ne.getMessage()); 368 return false; 369 } 370 371 return true; 372 } 373 374 377 private void displayMatrix() { 378 379 System.out.println("Listing table states: "); 380 System.out.println("--------------------------"); 381 for (int i = 0; i < stateNames.length; i++) { 382 System.out.println(stateNames[i]); 383 } 384 385 System.out.println(); 386 387 System.out.println("Listing transition table: "); 388 System.out.println("------------------------------"); 389 for (int i = 0; i < transitions.length; i++) { 390 for (int j = 0; j < transitions[0].length; j++) { 391 System.out.print("(" + i + "," + j + ")" + transitions[i][j] 392 + " "); 393 } 394 System.out.println(); 395 } 396 397 if (!identifyByOrigin) { 398 399 System.out.println(); 400 401 System.out.println("Listing state action: "); 402 System.out.println("------------------------"); 403 for (int i = 0; i < action.length; i++) { 404 System.out.println(action[i]); 405 } 406 } else { 407 408 System.out.println(); 409 410 System.out.println("Listing state action by origin: "); 411 System.out.println("-------------------------------------"); 412 for (int i = 0; i < actionByOrigin.length; i++) { 413 for (int j = 0; j < actionByOrigin[0].length; j++) { 414 System.out.println("(" + i + "," + j + ")" 415 + actionByOrigin[i][j]); 416 } 417 } 418 } 419 420 System.out.println(); 421 422 System.out.println("Listing Wait time: "); 423 System.out.println("-------------------"); 424 for (int i = 0; i < transitionsTime.length; i++) { 425 System.out.println(transitionsTime[i]); 426 } 427 428 System.out.println(); 429 430 System.out.println("Listing variables: "); 431 System.out.println("-------------------"); 432 for (int i = 0; i < prop.size(); i++) { 433 System.out.println(prop.keys()); 434 } 435 436 } 437 438 443 private int nextState() { 444 445 beforeStep = currentState; 446 float step = rand.nextFloat(); 447 float cumul = 0; 448 int i; 449 450 if (DEBUG) { 451 System.out.print("rand = " + step + " --- "); 452 System.out.print("previous state = " + beforeStep + " --- "); 453 } 454 455 for (i = 0; i < nbRows; i++) { 457 cumul = cumul + transitions[i][currentState]; 458 if (step < cumul) { 459 currentState = i; 460 break; 461 } 462 } 463 464 if (DEBUG) 465 System.out.print("current state = " + currentState + " --- "); 466 467 if (currentState == nbRows - 2) { 469 isBackClicked = true; 470 if (DEBUG) 471 System.out.println("Back..."); 472 if (previousStates.empty()) 473 System.out 474 .println("Error detected: Trying to go back but no previous state is available (currentState:" 475 + currentState + ", beforeStep:" + beforeStep); 476 else { 477 try { 478 if (useMatrixThinkTime) 479 Thread 480 .sleep((long) ((float) transitionsTime[currentState])); 481 } catch (java.lang.InterruptedException ie) { 482 System.err.println("Thread " 483 + Thread.currentThread().getName() 484 + " has been interrupted."); 485 } 486 487 Integer previous = (Integer ) previousStates.pop(); 488 currentState = previous.intValue(); 489 490 return currentState; 491 } 492 } else { isBackClicked = false; 494 if (!isEndOfSession()) { 495 if (DEBUG) 496 System.out.print("Not end session --- "); 497 if (transitions[nbRows - 2][currentState] == 0) { 500 if (DEBUG) 501 System.out.println("no back possibility "); 502 previousStates.removeAllElements(); 503 } else { if (DEBUG) 506 System.out.println(); 507 previousStates.push(new Integer (beforeStep)); 508 } 509 } else { 510 currentState = -1; 511 if (DEBUG) 512 System.out.println("End session"); 513 } 514 } 515 516 if (currentState != -1) { 517 try { 518 519 if (useMatrixThinkTime) 520 Thread 521 .sleep((long) ((float) transitionsTime[currentState])); 522 523 } catch (java.lang.InterruptedException ie) { 524 System.err.println("Thread " + Thread.currentThread().getName() 525 + " has been interrupted."); 526 } 527 } 528 529 return currentState; 530 } 531 532 537 private boolean isEndOfSession() { 538 return currentState == nbRows - 1; 539 } 540 541 547 public String getCurrentStateName() { 548 return stateNames[currentState]; 549 } 550 551 556 public long getCurrentWaitingTime() { 557 return transitionsTime[currentState]; 558 } 559 560 563 public Transition getNextTransition() { 564 trans.setTransition(getStateTransition(nextState())); 565 return trans; 566 } 567 568 private void resetToInitialState() { 569 currentState = 0; 570 previousStates.removeAllElements(); 571 } 572 573 private boolean isBackClicked() { 574 return isBackClicked; 575 } 576 577 584 private String getStateTransition(int state) { 585 if (state == -1) { 587 resetToInitialState(); 588 return null; 589 } else if (identifyByOrigin) { 590 if (isBackClicked) 594 return actionByOrigin[beforeStep][state]; 595 else 596 return actionByOrigin[state][beforeStep]; 597 } else 598 return action[state]; 599 } 600 601 606 private void loadProperty(String line) { 607 int index = line.indexOf("="); 608 prop.setProperty(line.substring(0, index), line.substring(index + 1, 609 line.length())); 610 } 611 612 } | Popular Tags |