00001 package edu.ksu.cis.bandera.bir;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import ca.mcgill.sable.util.*;
00036
00037 import java.util.*;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 public class BirTrace {
00086
00087 boolean verified;
00088 boolean completed;
00089 boolean limitviolation;
00090 boolean outofmemory;
00091 boolean depthexceeded;
00092 boolean vectorexceeded;
00093 TransSystem system;
00094 TransVector transVector = new TransVector();
00095 Vector choiceVector = new Vector();
00096 String trapName;
00097 Transformation currentTrans;
00098 Transformation trapTrans;
00099 int trapActionNum;
00100 int numActions = 0;
00101 BirState [] state;
00102 public BirTrace(TransSystem system) {
00103 this.system = system;
00104 this.completed = false;
00105 this.verified = false;
00106 this.limitviolation = false;
00107 this.outofmemory = false;
00108 this.depthexceeded = false;
00109 this.vectorexceeded = false;
00110 }
00111 public BirTrace(TransSystem system, java.util.List l) {
00112 this(system);
00113 processTrail(l);
00114 }
00115
00116
00117
00118
00119 public void addTrans(Transformation trans) {
00120 if (currentTrans != null)
00121 numActions += currentTrans.getActions().size();
00122 currentTrans = trans;
00123 transVector.addElement(trans);
00124 choiceVector.addElement(new int[trans.getActions().size()+1]);
00125 }
00126
00127
00128
00129
00130 public void done() {
00131 int actionCount = 0;
00132 Vector stateVector = new Vector();
00133 BirState currentState =
00134 BirState.initialState(system,transVector,choiceVector);
00135 stateVector.addElement(currentState.copy());
00136 for (int i = 0; i < transVector.size(); i++) {
00137 Transformation trans = transVector.elementAt(i);
00138 int choices [] = (int []) choiceVector.elementAt(i);
00139 ActionVector actions = trans.getActions();
00140 for (int j = 0; j < actions.size(); j++) {
00141 actionCount++;
00142 if (trapTrans == trans && trapActionNum <= j + 1)
00143 break;
00144 int choiceNum = choices[j + 1];
00145 Action action = actions.elementAt(j);
00146
00147 currentState.applyAction(action,choiceNum);
00148 stateVector.addElement(currentState.copy());
00149 }
00150 currentState.completeTrans(trans);
00151 }
00152 state = new BirState[actionCount + 1];
00153 for (int i = 0; i < stateVector.size(); i++)
00154 state[i] = (BirState) stateVector.elementAt(i);
00155
00156
00157 if (stateVector.size() <= actionCount)
00158 state[stateVector.size()] = state[stateVector.size() - 1];
00159 numActions = actionCount;
00160 }
00161 public String evalExpr(Expr expr, int stateIndex) {
00162 return state[stateIndex].exprValue(expr);
00163 }
00164 public int getNumActions() { return numActions; }
00165 public BirState getState(int stateIndex) {
00166 return state[stateIndex];
00167 }
00168 public TransSystem getTransSystem() { return system; }
00169 public TransVector getTransVector() { return transVector; }
00170 public Action getTrapAction() {
00171 if (trapTrans == null)
00172 return null;
00173 if (trapActionNum > 0)
00174 return trapTrans.getActions().elementAt(trapActionNum - 1);
00175 else
00176 return trapTrans.getActions().elementAt(0);
00177 }
00178 public String getTrapName() { return trapName; }
00179 public Transformation getTrapTrans() { return trapTrans; }
00180 public boolean isComplete() { return completed; }
00181 public boolean isDepthExceeded() { return depthexceeded; }
00182 public boolean isLimitViolation() { return limitviolation; }
00183 public boolean isOutOfMemory() { return outofmemory; }
00184 public boolean isVectorExceeded() { return vectorexceeded; }
00185 public boolean isVerified() { return verified; }
00186 public void print(boolean showStates) {
00187 if (! completed)
00188 System.out.println("CHECKER FAILED");
00189 else if (verified)
00190 System.out.println("VERIFIED");
00191 else {
00192 int actionCount = 0;
00193 System.out.println("BIR TRACE:");
00194 if (showStates)
00195 state[0].print();
00196 for (int i = 0; i < transVector.size(); i++) {
00197 Transformation trans = transVector.elementAt(i);
00198 Location fromLoc = trans.getFromLoc();
00199 System.out.println("TRANS " +
00200 fromLoc.getThread().getName() + ": " +
00201 fromLoc.getLabel());
00202 BirPrinter.printTrans(trans);
00203 actionCount += trans.getActions().size();
00204 if (showStates && actionCount < state.length)
00205 state[actionCount].print();
00206 }
00207 if (trapName != null)
00208 System.out.println("TRAP " + trapName);
00209 }
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 private void processTrail(java.util.List l)
00230 {
00231 if (l == null || l.size() == 0)
00232 {
00233 setVerified(true); return;
00234 }
00235
00236 for (java.util.Iterator i = l.iterator(); i.hasNext(); )
00237 {
00238 String line = (String) i.next();
00239 if (!line.startsWith("BIR")) continue;
00240
00241 try {
00242 StringTokenizer st = new StringTokenizer(line.substring(4));
00243 if (line.startsWith("BIR:"))
00244 {
00245
00246 int locId = Integer.parseInt(st.nextToken());
00247 int transNum = Integer.parseInt(st.nextToken());
00248 int actionNum = Integer.parseInt(st.nextToken());
00249 String status = st.nextToken();
00250 Location loc = system.getLocation(locId);
00251 Transformation trans = loc.getOutTrans().elementAt(transNum);
00252 if (status.equals("OK")) {
00253
00254 if (actionNum == 1) addTrans(trans);
00255 } else
00256 setTrap(status, trans, actionNum);
00257 } else if (line.startsWith("BIR?"))
00258 {
00259
00260 int actionNum = Integer.parseInt(st.nextToken());
00261 int choiceNum = Integer.parseInt(st.nextToken());
00262 setChoice(actionNum, choiceNum);
00263 }
00264 } catch (Exception e)
00265 {
00266 throw new RuntimeException("Invalid BIR traces!");
00267 }
00268 }
00269 done();
00270 setVerified(false);
00271 }
00272
00273
00274
00275
00276 public void setChoice(int actionNum, int choiceNum) {
00277
00278 int actionChoices [] = (int []) choiceVector.lastElement();
00279 actionChoices[actionNum - 1] = choiceNum;
00280 }
00281 public void setDepthExceeded() {
00282 this.depthexceeded = true;
00283 this.verified = false;
00284 this.completed = true;
00285 }
00286 public void setOutOfMemory() {
00287 this.outofmemory = true;
00288 this.verified = false;
00289 this.completed = true;
00290 }
00291
00292
00293
00294
00295 public void setTrap(String trapName, Transformation trapTrans,
00296 int actionNum) {
00297 this.trapName = trapName;
00298 limitviolation = trapName.endsWith("LimitException");
00299 this.trapTrans = trapTrans;
00300 this.trapActionNum = actionNum;
00301 }
00302 public void setVectorExceeded() {
00303 this.vectorexceeded = true;
00304 this.verified = false;
00305 this.completed = true;
00306 }
00307 public void setVerified(boolean verified) {
00308 this.verified = verified;
00309 this.completed = true;
00310 }
00311 }