00001 package edu.ksu.cis.bandera.spin;
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 java.io.*;
00036 import java.util.*;
00037
00038 import edu.ksu.cis.bandera.bir.*;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 public class CaseNode implements TreeNode {
00051
00052 int size = 0;
00053 Case [] data;
00054
00055 public CaseNode() { this(10); }
00056 public CaseNode(int capacity) {
00057 data = new Case[capacity];
00058 }
00059 public CaseNode(Case x) {
00060 this(10);
00061 addElement(x);
00062 }
00063 public void addCase(String cond, TreeNode node) {
00064 addElement(new Case(cond,node));
00065 }
00066 public void addCase(String cond, String expr) {
00067 addElement(new Case(cond,new ExprNode(expr)));
00068 }
00069 public void addElement(Case x) {
00070 if (x == null)
00071 throw new RuntimeException("Element cannot be null");
00072 if (size == data.length)
00073 expand();
00074 data[size++] = x;
00075 }
00076 public void addTrapCase(String cond, String desc, boolean fatal) {
00077 addElement(new Case(cond,new TrapNode(desc,fatal)));
00078 }
00079
00080
00081
00082
00083 public Object clone() {
00084 CaseNode result = new CaseNode(this.data.length);
00085 for (int i = 0; i < this.size(); i++)
00086 result.addElement((Case)this.elementAt(i).clone());
00087 return result;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 public TreeNode compose(TreeNode tree, Case context) {
00101 CaseNode result = (CaseNode) this.clone();
00102 for (int i = 0; i < result.size(); i++) {
00103 Case c = result.elementAt(i);
00104 c.parent = context;
00105 c.node = c.node.compose(tree,c);
00106 }
00107 return result;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 boolean conditionInconsistent(String cond, Case context) {
00124
00125 if (cond.indexOf("==") < 0 || cond.indexOf("&&") >= 0
00126 || cond.indexOf("||") >= 0 || cond.indexOf("!") >= 0)
00127 return false;
00128 while (context != null) {
00129
00130 String var = cond.substring(0,cond.indexOf("=="));
00131 String val = cond.substring(cond.indexOf("=="));
00132 int pos = context.cond.indexOf("==");
00133 if (pos > 0 && context.cond.substring(0,pos).equals(var)
00134 && ! context.cond.substring(pos).equals(val))
00135 return true;
00136 context = context.parent;
00137 }
00138 return false;
00139 }
00140
00141
00142
00143
00144
00145 boolean conditionRedundant(String cond, Case context) {
00146 while (context != null) {
00147 if (context.cond.equals(cond))
00148 return true;
00149 context = context.parent;
00150 }
00151 return false;
00152 }
00153 public Case elementAt(int pos) {
00154 if (pos < 0 || pos >= size)
00155 throw new RuntimeException("Position invalid: " + pos);
00156 return data[pos];
00157 }
00158
00159 void expand() {
00160 Case [] newData = new Case[data.length * 2];
00161 for (int i = 0; i < data.length; i++)
00162 newData[i] = data[i];
00163 data = newData;
00164 }
00165 public Case firstElement() { return elementAt(0); }
00166 public TreeNode getCase(String cond) {
00167 for (int i = 0; i < size(); i++)
00168 if (elementAt(i).cond.equals(cond))
00169 return elementAt(i).node;
00170 throw new RuntimeException("No such case: " + cond);
00171 }
00172
00173
00174
00175
00176
00177 public Vector getLeafCases(Vector leafCases) {
00178 for (int i = 0; i < size(); i++)
00179 if (elementAt(i).node instanceof CaseNode)
00180 elementAt(i).node.getLeafCases(leafCases);
00181 else if (elementAt(i).node instanceof ExprNode)
00182 leafCases.addElement(elementAt(i));
00183
00184 return leafCases;
00185 }
00186
00187
00188
00189
00190 public Vector getLeaves(Vector leaves) {
00191 for (int i = 0; i < size(); i++)
00192 elementAt(i).node.getLeaves(leaves);
00193 return leaves;
00194 }
00195 void indent(int level) {
00196 for (int i = 0; i < level; i++)
00197 System.out.print(" ");
00198 }
00199 public int indexOf(Case x) {
00200 for (int i = 0; i < size; i++)
00201 if (data[i] == x)
00202 return i;
00203 return -1;
00204 }
00205 public void insertElementAt(Case x, int pos) {
00206 if (x == null)
00207 throw new RuntimeException("Element cannot be null");
00208 if (size == data.length)
00209 expand();
00210 if (pos > size || pos < 0)
00211 throw new RuntimeException("Position invalid: " + pos);
00212
00213 for (int i = size; i > pos; i--)
00214 data[i] = data[i-1];
00215 data[pos] = x;
00216 size++;
00217 }
00218 public void print(int level) {
00219 System.out.println();
00220 for (int i = 0; i < size(); i++) {
00221 Case c = elementAt(i);
00222 indent(level);
00223 System.out.print(c.cond + " => ");
00224 c.node.print(level+1);
00225 }
00226 }
00227 public int size() { return size; }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 public TreeNode specialize(ExprNode leaf, Case context) {
00246 CaseNode result = (CaseNode) this.clone();
00247 int numInconsistent = 0;
00248 for (int i = 0; i < result.size(); i++) {
00249 Case c = result.elementAt(i);
00250 if (conditionRedundant(c.cond, context)
00251 || ((i == (result.size() - 1))
00252 && (i == numInconsistent)))
00253 return c.node.specialize(leaf,context);
00254 if (conditionInconsistent(c.cond, context))
00255 numInconsistent++;
00256 c.parent = context;
00257 c.node = c.node.specialize(leaf,c);
00258 }
00259 return result;
00260 }
00261 }