00001 package edu.ksu.cis.bandera.pdgslicer.dependency;
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
00036
00037 import edu.ksu.cis.bandera.bui.*;
00038 import edu.ksu.cis.bandera.pdgslicer.datastructure.*;
00039 import ca.mcgill.sable.soot.jimple.*;
00040 import edu.ksu.cis.bandera.jext.*;
00041 import edu.ksu.cis.bandera.jjjc.symboltable.*;
00042 import ca.mcgill.sable.soot.*;
00043 import edu.ksu.cis.bandera.specification.assertion.*;
00044 import edu.ksu.cis.bandera.specification.assertion.datastructure.*;
00045 import edu.ksu.cis.bandera.specification.predicate.*;
00046 import edu.ksu.cis.bandera.specification.predicate.datastructure.*;
00047 import edu.ksu.cis.bandera.specification.datastructure.*;
00048 import java.util.*;
00049 import edu.ksu.cis.bandera.jjjc.*;
00050 import javax.swing.JOptionPane;
00051 public class SliceInterestEnv {
00052 private Dependencies dependFrame;
00053 private CriterionViewer criterionViewer;
00054 private Hashtable importedAssertion = new Hashtable();
00055 private Hashtable importedAssertionSet = new Hashtable();
00056 private HashSet predicates = new HashSet();
00057 private HashSet quantifierSliceInterests = new HashSet();
00058
00059
00060
00061 public SliceInterestEnv() {
00062 }
00063
00064
00065
00066
00067 Vector getSliceInterests() {
00068 if (!runSpecCompiler())
00069 return null;
00070 Vector v = (Vector) AssertionSliceInterestCollector.collect(new Vector());
00071 v.addAll(PredicateSliceInterestCollector.collect(predicates));
00072 v.addAll(quantifierSliceInterests);
00073
00074
00075 return v;
00076 }
00077
00078
00079
00080 public void initAssertions() throws Exception {
00081 for (Iterator i = BUI.property.getImportedAssertion().iterator(); i.hasNext();) {
00082 Name name = (Name) i.next();
00083 Assertion as = AssertionSet.getAssertion(name);
00084 importedAssertion.put(name, as);
00085 }
00086
00087 for (Iterator i = BUI.property.getImportedAssertionSet().iterator(); i.hasNext();) {
00088 Name name = (Name) i.next();
00089 AssertionSet as = AssertionSet.getAssertionSet(name);
00090 importedAssertionSet.put(name, as);
00091 }
00092 }
00093
00094
00095
00096
00097
00098 public Assertion resolveAssertionName(Name name) throws Exception {
00099 Assertion a = (Assertion) importedAssertion.get(name);
00100 if (a != null)
00101 return a;
00102
00103 if (!name.isSimpleName()) {
00104 try {
00105 return AssertionSet.getAssertion(name);
00106 } catch (Exception e) {
00107 throw new Exception("Can't resolve assertion with name '" + name + "'");
00108 }
00109 }
00110
00111 Vector v = new Vector();
00112
00113 for (Enumeration e = importedAssertionSet.elements(); e.hasMoreElements();) {
00114 AssertionSet as = (AssertionSet) e.nextElement();
00115 if (as.getAssertionTable().get(new Name(as.getName(), name)) != null) {
00116 v.add(as);
00117 }
00118 }
00119
00120 if (v.size() > 1)
00121 throw new Exception("Ambiguous assertion with name '" + name + "'");
00122 if (v.size() < 1)
00123 throw new Exception("Can't resolve assertion with name '" + name + "'");
00124
00125 AssertionSet as = (AssertionSet) v.firstElement();
00126 a = (Assertion) as.getAssertionTable().get(new Name(as.getName(), name));
00127 return a;
00128 }
00129
00130
00131
00132 private boolean runSpecCompiler() {
00133 try {
00134 Property property = BUI.property;
00135 Hashtable assertionTable = new Hashtable();
00136 initAssertions();
00137 for (Iterator i = property.getActivatedAssertionProperties().iterator(); i.hasNext();) {
00138 AssertionProperty ap = (AssertionProperty) i.next();
00139 for (Iterator j = ap.getAssertions().iterator(); j.hasNext();) {
00140 assertionTable.put(resolveAssertionName((Name) j.next()), new HashSet());
00141 }
00142 }
00143 int numOfQuantifiers = 0;
00144 HashSet v = new HashSet();
00145 TemporalLogicProperty tlp = property.getActivatedTemporalLogicProperty();
00146 if (tlp != null) {
00147 tlp.validate(property.getImportedType(), property.getImportedPackage(), property.getImportedAssertion(), property.getImportedAssertionSet(), property.getImportedPredicate(), property.getImportedPredicateSet());
00148 if (tlp.getExceptions().size() > 0) {
00149 StringBuffer buffer = new StringBuffer();
00150 for (Iterator i = tlp.getExceptions().iterator(); i.hasNext();) {
00151 buffer.append(i.next() + "\n");
00152 }
00153 JOptionPane.showMessageDialog(criterionViewer, buffer.toString(), "Error", JOptionPane.ERROR_MESSAGE);
00154 dependFrame.success = false;
00155 return false;
00156 }
00157 Hashtable qTable = tlp.getQuantifiersTable();
00158 numOfQuantifiers = qTable.size();
00159 for (Enumeration e = qTable.elements(); e.hasMoreElements();) {
00160 v.add((QuantifiedVariable) e.nextElement());
00161 }
00162 }
00163 if ((assertionTable.size() > 0) || (numOfQuantifiers > 0)) {
00164 CompilationManager.setQuantifiers(tlp.getQuantifiedVariables());
00165 CompilationManager.setModifiedMethodTable(AssertionProcessor.process(assertionTable));
00166 if (!dependFrame.reRunJJJC()) {
00167 dependFrame.success = false;
00168 return false;
00169 }
00170 SootClass mainClass = CompilationManager.getMainSootClass();
00171 for (Iterator i = CompilationManager.getQuantifiers().iterator(); i.hasNext();) {
00172 QuantifiedVariable qv = (QuantifiedVariable) i.next();
00173 String name = "quantification$" + qv.getName();
00174 SootField sf = CompilationManager.getFieldForQuantifier(name);
00175 mainClass.addField(sf);
00176 SootClass sc = sf.getDeclaringClass();
00177 quantifierSliceInterests.add(new SliceField(sc, sf));
00178 }
00179 CompilationManager.setModifiedMethodTable(new Hashtable());
00180 }
00181
00182
00183 if (tlp != null) {
00184 StringBuffer sb = new StringBuffer();
00185 tlp.validate(property.getImportedType(), property.getImportedPackage(), property.getImportedAssertion(), property.getImportedAssertionSet(), property.getImportedPredicate(), property.getImportedPredicateSet());
00186 Hashtable pTable = tlp.getPredicatesTable();
00187 Hashtable pqTable = tlp.getPredicateQuantifierTable();
00188 Hashtable qphTable = new Hashtable();
00189 SootClass mainClass = CompilationManager.getMainSootClass();
00190 Jimple jimple = Jimple.v();
00191 HashSet visitedP = new HashSet();
00192 for (Enumeration e = pTable.keys(); e.hasMoreElements();) {
00193 Object key = e.nextElement();
00194 Predicate p = (Predicate) pTable.get(key);
00195 if (p.getExceptions().size() > 0) {
00196 if (visitedP.contains(p)) continue;
00197 visitedP.add(p);
00198 sb.append("In '" + p + "':\n");
00199 for (Iterator i = p.getExceptions().iterator(); i.hasNext();) {
00200 sb.append("* " + ((Exception) i.next()).getMessage() + "\n");
00201 }
00202 continue;
00203 }
00204 Vector qvs = (Vector) pqTable.get(key);
00205 Value phThis = null;
00206 Hashtable phParams = new Hashtable();
00207
00208 for (int i = 0; i < qvs.size(); i++) {
00209 QuantifiedVariable q = (QuantifiedVariable) qvs.elementAt(i);
00210 Value cph = null;
00211 if (q != null) {
00212 if (qphTable.get(q) == null) {
00213 String name = "quantification$" + q.getName();
00214 cph = jimple.newStaticFieldRef(mainClass.getField(name));
00215 qphTable.put(q, cph);
00216 } else {
00217 cph = (Value) qphTable.get(q);
00218 }
00219 if ((i == 0) && (!p.isStatic())) {
00220 phThis = cph;
00221 } else {
00222 phParams.put(q.getName(), cph);
00223 }
00224 }
00225 }
00226 Value value = PredicateProcessor.process(phThis, phParams, p);
00227 if (!(p instanceof ExpressionPredicate)) {
00228 if (value instanceof LocationTestExpr) {
00229 LocationTestExpr lte = (LocationTestExpr) value;
00230 } else {
00231 LocationTestExpr lte = (LocationTestExpr) ((LogicalAndExpr) value).getOp1();
00232 }
00233 }
00234 predicates.add(p);
00235 }
00236
00237 String s = sb.toString();
00238 if (s.length() > 0)
00239 throw new RuntimeException(s.substring(0, s.length() - 1));
00240 }
00241 } catch (Exception e) {
00242 CompilationManager.setModifiedMethodTable(new Hashtable());
00243 CompilationManager.setQuantifiers(new Vector());
00244 JOptionPane.showMessageDialog(criterionViewer, e.getMessage(), "Bandera SL Phase --- Error", JOptionPane.ERROR_MESSAGE);
00245 e.printStackTrace();
00246 dependFrame.success = false;
00247 return false;
00248 }
00249 dependFrame.success = true;
00250 return true;
00251 }
00252
00253
00254
00255
00256
00257 void setCriterionViewer(CriterionViewer cv) {
00258 criterionViewer = cv;
00259 }
00260
00261
00262
00263
00264
00265 void setDependFrame(Dependencies dpnd) {
00266 dependFrame = dpnd;
00267 }
00268 }