1 package edu.rice.rubbos.client; 2 3 import java.io.BufferedReader ; 4 import java.io.FileReader ; 5 import java.io.FileNotFoundException ; 6 import java.io.IOException ; 7 import java.lang.NumberFormatException ; 8 import java.util.StringTokenizer ; 9 import java.util.NoSuchElementException ; 10 import java.util.Random ; 11 import java.util.Stack ; 12 13 65 66 public class TransitionTable 67 { 68 private int nbColumns; 69 private int nbRows; 70 private float transitions[][]; 71 private int transitionsTime[]; 72 private String tableName = null; 73 private Random rand = new Random (); 74 private Stack previousStates = new Stack (); 75 private int currentState = 0; 76 private Stats stats; 77 private boolean useTPCWThinkTime; 78 private static String [] stateNames; 79 80 83 public TransitionTable(int columns, int rows, Stats statistics, boolean UseTPCWThinkTime) 84 { 85 nbColumns = columns; 86 nbRows = rows; 87 stats = statistics; 88 transitions = new float[nbColumns][nbRows]; 89 transitionsTime = new int[nbRows]; 90 useTPCWThinkTime = UseTPCWThinkTime; 91 } 92 93 94 99 public String getTableName() 100 { 101 return tableName; 102 } 103 104 105 108 public void resetToInitialState() 109 { 110 currentState = 0; 111 stats.incrementCount(currentState); 112 } 113 114 115 120 public int getCurrentState() 121 { 122 return currentState; 123 } 124 125 126 131 public int getPreviousState() 132 { 133 if (previousStates.empty()) 134 return -1; 135 else 136 { 137 Integer state = (Integer )previousStates.peek(); 138 return state.intValue(); 139 } 140 } 141 142 143 148 public int backToPreviousState() 149 { 150 if (previousStates.empty()) 151 return -1; 152 else 153 { 154 Integer state = (Integer )previousStates.pop(); 155 currentState = state.intValue(); 156 return currentState; 157 } 158 } 159 160 161 166 public boolean isEndOfSession() 167 { 168 return currentState == (nbRows-1); 169 } 170 171 172 177 public String getCurrentStateName() 178 { 179 return stateNames[currentState]; 180 } 181 182 183 188 public static String getStateName(int state) 189 { 190 return stateNames[state]; 191 } 192 193 194 199 public int nextState() 200 { 201 int beforeStep = currentState; 202 float step = rand.nextFloat(); 203 float cumul = 0; 204 int i; 205 206 for (i = 0 ; i < nbRows ; i++) 207 { 208 cumul = cumul + transitions[currentState][i]; 209 if (step < cumul) 210 { 211 currentState = i; 212 break; 213 } 214 } 215 if (currentState == nbRows-2) 217 { 218 if (previousStates.empty()) 219 System.out.println("Error detected: Trying to go back but no previous state is available (currentState:"+currentState+", beforeStep:"+beforeStep); 220 else 221 { stats.incrementCount(currentState); try 225 { 226 if (useTPCWThinkTime) 227 Thread.currentThread().sleep(TPCWthinkTime()); 228 else 229 Thread.currentThread().sleep((long)((float)transitionsTime[currentState]*ClientEmulator.getSlowDownFactor())); 230 } 231 catch (java.lang.InterruptedException ie) 232 { 233 System.err.println("Thread "+Thread.currentThread().getName()+" has been interrupted."); 234 } 235 Integer previous = (Integer )previousStates.pop(); 236 currentState = previous.intValue(); 237 return currentState; 240 } 241 } 242 else 243 { if (!isEndOfSession()) 245 { if (transitions[currentState][nbRows-2] == 0) 247 previousStates.removeAllElements(); 248 else previousStates.push(new Integer (beforeStep)); 250 } 252 } 253 stats.incrementCount(currentState); 254 try 255 { 256 if (useTPCWThinkTime) 257 Thread.currentThread().sleep(TPCWthinkTime()); 258 else 259 Thread.currentThread().sleep((long)((float)transitionsTime[currentState]*ClientEmulator.getSlowDownFactor())); 260 } 261 catch (java.lang.InterruptedException ie) 262 { 263 System.err.println("Thread "+Thread.currentThread().getName()+" has been interrupted."); 264 } 265 return currentState; 266 } 267 268 269 276 public boolean ReadExcelTextFile(String filename) 277 { 278 BufferedReader reader; 279 int i = 0; 280 int j = 0; 281 282 try 284 { 285 reader = new BufferedReader (new FileReader (filename)); 286 } 287 catch (FileNotFoundException f) 288 { 289 System.err.println("File "+filename+" not found."); 290 return false; 291 } 292 293 try 295 { 296 StringTokenizer st = new StringTokenizer (reader.readLine(), "\t"); 298 String s = st.nextToken(); tableName = st.nextToken(); 300 reader.readLine(); reader.readLine(); reader.readLine(); 305 stateNames = new String [nbRows]; 306 for (i = 0 ; i < nbRows ; i++) 308 { 309 st = new StringTokenizer (reader.readLine(), "\t"); 310 stateNames[i] = st.nextToken(); 311 for (j = 0 ; j < nbColumns ; j++) 312 { 313 Float f = new Float (st.nextToken()); 314 transitions[j][i] = f.floatValue(); 315 } 316 Integer t = new Integer (st.nextToken()); 318 transitionsTime[i] = 1; } 320 reader.close(); 321 } 322 catch (IOException ioe) 323 { 324 System.err.println("An error occured while reading "+filename+". ("+ioe.getMessage()+")"); 325 return false; 326 } 327 catch (NoSuchElementException nsu) 328 { 329 System.err.println("File format error in file "+filename+" when reading line "+i+", column "+j+". ("+nsu.getMessage()+")"); 330 return false; 331 } 332 catch (NumberFormatException ne) 333 { 334 System.err.println("Number format error in file "+filename+" when reading line "+i+", column "+j+". ("+ne.getMessage()+")"); 335 return false; 336 } 337 return true; 339 } 340 341 342 347 protected void displayMatrix(String title) 348 { 349 int i,j; 350 351 System.out.println("\n<h3><br>### "+title+" Transition table ###</h3>\n"); 352 System.out.println("Transition set: '"+tableName+"'<br>\n"); 353 System.out.println("<TABLE border=\"1\" summary=\"transition table\"><TBODY>\n"); 354 System.out.println("<THEAD><TR><TH>State name"); 355 for (j = 0 ; j < nbColumns ; j++) 356 System.out.print("<TH>"+stateNames[j]); 357 System.out.print("<TH>Transition time"); 358 for (i = 0 ; i < nbRows ; i++) 359 { 360 System.out.print("\n<TR><TD><div align=left><B>"+stateNames[i]+"</B></div>"); 361 for (j = 0 ; j < nbColumns ; j++) 362 System.out.print("<TD><div align=right>"+Float.toString(transitions[j][i])+"</div>"); 363 System.out.print("<TD><div align=right>"+Float.toString(transitionsTime[i])+"</div>"); 364 } 365 System.out.println("\n</TBODY></TABLE>\n"); 366 System.out.println(); 367 } 368 369 private long TPCWthinkTime() 372 { 373 double r = rand.nextDouble(); 374 if (r < (double)4.54e-5) 375 return ((long) (r+0.5)); 376 return ((long) ((((double)-7000.0)*Math.log(r))+0.5)); 377 } 378 379 } 380 | Popular Tags |