1 24 package edu.rice.rubis.client; 25 26 import java.io.BufferedReader ; 27 import java.io.FileReader ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.lang.NumberFormatException ; 31 import java.util.StringTokenizer ; 32 import java.util.NoSuchElementException ; 33 import java.util.Random ; 34 import java.util.Stack ; 35 36 90 91 public class TransitionTable 92 { 93 private int nbColumns; 94 private int nbRows; 95 private float transitions[][]; 96 private int transitionsTime[]; 97 private String tableName = null; 98 private Random rand = new Random (); 99 private Stack previousStates = new Stack (); 100 private int currentState = 0; 101 private Stats stats; 102 private boolean useTPCWThinkTime; 103 private static String [] stateNames; 104 105 108 public TransitionTable(int columns, int rows, Stats statistics, boolean UseTPCWThinkTime) 109 { 110 nbColumns = columns; 111 nbRows = rows; 112 stats = statistics; 113 transitions = new float[nbColumns][nbRows]; 114 transitionsTime = new int[nbRows]; 115 useTPCWThinkTime = UseTPCWThinkTime; 116 } 117 118 119 124 public String getTableName() 125 { 126 return tableName; 127 } 128 129 130 133 public void resetToInitialState() 134 { 135 currentState = 0; 136 stats.incrementCount(currentState); 137 } 138 139 140 145 public int getCurrentState() 146 { 147 return currentState; 148 } 149 150 151 156 public int getPreviousState() 157 { 158 if (previousStates.empty()) 159 return -1; 160 else 161 { 162 Integer state = (Integer )previousStates.peek(); 163 return state.intValue(); 164 } 165 } 166 167 168 173 public int backToPreviousState() 174 { 175 if (previousStates.empty()) 176 return -1; 177 else 178 { 179 Integer state = (Integer )previousStates.pop(); 180 currentState = state.intValue(); 181 return currentState; 182 } 183 } 184 185 186 191 public boolean isEndOfSession() 192 { 193 return currentState == (nbRows-1); 194 } 195 196 197 202 public String getCurrentStateName() 203 { 204 return stateNames[currentState]; 205 } 206 207 208 213 public static String getStateName(int state) 214 { 215 return stateNames[state]; 216 } 217 218 219 224 public int nextState() 225 { 226 int beforeStep = currentState; 227 float step = rand.nextFloat(); 228 float cumul = 0; 229 int i; 230 231 for (i = 0 ; i < nbRows ; i++) 232 { 233 cumul = cumul + transitions[currentState][i]; 234 if (step < cumul) 235 { 236 currentState = i; 237 break; 238 } 239 } 240 if (currentState == nbRows-2) 242 { 243 if (previousStates.empty()) 244 System.out.println("Error detected: Trying to go back but no previous state is available (currentState:"+currentState+", beforeStep:"+beforeStep); 245 else 246 { stats.incrementCount(currentState); try 250 { 251 if (useTPCWThinkTime) 252 Thread.currentThread().sleep((long)((float)TPCWthinkTime()*ClientEmulator.getSlowDownFactor())); 253 else 254 Thread.currentThread().sleep((long)((float)transitionsTime[currentState]*ClientEmulator.getSlowDownFactor())); 255 } 256 catch (java.lang.InterruptedException ie) 257 { 258 System.err.println("Thread "+Thread.currentThread().getName()+" has been interrupted."); 259 } 260 Integer previous = (Integer )previousStates.pop(); 261 currentState = previous.intValue(); 262 stats.incrementCount(currentState); return currentState; 265 } 266 } 267 else 268 { if (!isEndOfSession()) 270 { if (transitions[currentState][nbRows-2] == 0) 272 previousStates.removeAllElements(); 273 else previousStates.push(new Integer (beforeStep)); 275 } 277 } 278 stats.incrementCount(currentState); 279 try 280 { 281 if (useTPCWThinkTime) 282 Thread.currentThread().sleep((long)((float)TPCWthinkTime()*ClientEmulator.getSlowDownFactor())); 283 else 284 Thread.currentThread().sleep((long)((float)transitionsTime[currentState]*ClientEmulator.getSlowDownFactor())); 285 } 286 catch (java.lang.InterruptedException ie) 287 { 288 System.err.println("Thread "+Thread.currentThread().getName()+" has been interrupted."); 289 } 290 return currentState; 291 } 292 293 294 301 public boolean ReadExcelTextFile(String filename) 302 { 303 BufferedReader reader; 304 int i = 0; 305 int j = 0; 306 307 try 309 { 310 reader = new BufferedReader (new FileReader (filename)); 311 } 312 catch (FileNotFoundException f) 313 { 314 System.err.println("File "+filename+" not found."); 315 return false; 316 } 317 318 try 320 { 321 StringTokenizer st = new StringTokenizer (reader.readLine(), "\t"); 323 String s = st.nextToken(); tableName = st.nextToken(); 325 reader.readLine(); reader.readLine(); reader.readLine(); 330 stateNames = new String [nbRows]; 331 for (i = 0 ; i < nbRows ; i++) 333 { 334 st = new StringTokenizer (reader.readLine(), "\t"); 335 stateNames[i] = st.nextToken(); 336 for (j = 0 ; j < nbColumns ; j++) 337 { 338 Float f = new Float (st.nextToken()); 339 transitions[j][i] = f.floatValue(); 340 } 341 Integer t = new Integer (st.nextToken()); 343 transitionsTime[i] = t.intValue(); 344 } 345 reader.close(); 346 } 347 catch (IOException ioe) 348 { 349 System.err.println("An error occured while reading "+filename+". ("+ioe.getMessage()+")"); 350 return false; 351 } 352 catch (NoSuchElementException nsu) 353 { 354 System.err.println("File format error in file "+filename+" when reading line "+i+", column "+j+". ("+nsu.getMessage()+")"); 355 return false; 356 } 357 catch (NumberFormatException ne) 358 { 359 System.err.println("Number format error in file "+filename+" when reading line "+i+", column "+j+". ("+ne.getMessage()+")"); 360 return false; 361 } 362 return true; 364 } 365 366 367 371 protected void displayMatrix() 372 { 373 int i,j; 374 375 System.out.println("\n<h3><br>### Transition table ###</h3>\n"); 376 System.out.println("Transition set: '"+tableName+"'<br>\n"); 377 System.out.println("<TABLE border=\"1\" summary=\"transition table\"><TBODY>\n"); 378 System.out.println("<THEAD><TR><TH>State name"); 379 for (j = 0 ; j < nbColumns ; j++) 380 System.out.print("<TH>"+stateNames[j]); 381 System.out.print("<TH>Transition time"); 382 for (i = 0 ; i < nbRows ; i++) 383 { 384 System.out.print("\n<TR><TD><div align=left><B>"+stateNames[i]+"</B></div>"); 385 for (j = 0 ; j < nbColumns ; j++) 386 System.out.print("<TD><div align=right>"+Float.toString(transitions[j][i])+"</div>"); 387 System.out.print("<TD><div align=right>"+Float.toString(transitionsTime[i])+"</div>"); 388 } 389 System.out.println("\n</TBODY></TABLE>\n"); 390 System.out.println(); 391 } 392 393 394 private long TPCWthinkTime() 397 { 398 double r = rand.nextDouble(); 399 if (r < (double)4.54e-5) 400 return ((long) (r+0.5)); 401 return ((long) ((((double)-7000.0)*Math.log(r))+0.5)); 402 } 403 404 } 405 | Popular Tags |