00001 package edu.ksu.cis.bandera.specification;
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 edu.ksu.cis.bandera.specification.analysis.*;
00036 import edu.ksu.cis.bandera.specification.datastructure.*;
00037 import edu.ksu.cis.bandera.specification.exception.*;
00038 import edu.ksu.cis.bandera.specification.node.*;
00039 import edu.ksu.cis.bandera.specification.pattern.datastructure.*;
00040 import edu.ksu.cis.bandera.annotation.*;
00041 import edu.ksu.cis.bandera.jext.*;
00042 import edu.ksu.cis.bandera.jjjc.*;
00043 import ca.mcgill.sable.soot.*;
00044 import ca.mcgill.sable.soot.jimple.*;
00045 import ca.mcgill.sable.soot.grimp.*;
00046 import java.util.*;
00047
00048 public class Compiler extends DepthFirstAdapter {
00049 private static Grimp grimp = Grimp.v();
00050 private Hashtable predNodeGrimpTable;
00051 private HashSet placeHolders;
00052 private Pattern pattern;
00053
00054 private Value currentValue;
00055 private static final String defaultPrefix = "pred__";
00056 private static final String defaultParam = defaultPrefix+"a";
00057
00058 private Hashtable parameterTable = new Hashtable();
00059 private Hashtable parameterNameTable = new Hashtable();
00060 private AnnotationManager am = CompilationManager.getAnnotationManager();
00061 private Iterator parametersIterator;
00062
00063
00064
00065
00066
00067
00068
00069 public Compiler(Hashtable predNodeGrimpTable, HashSet placeHolders, Pattern pattern, Node node) {
00070 this.predNodeGrimpTable = predNodeGrimpTable;
00071 this.placeHolders = placeHolders;
00072 this.pattern = pattern;
00073 parametersIterator = pattern.getParametersOccurenceOrder().iterator();
00074 node.apply(this);
00075 buildFormulaConditions();
00076 }
00077
00078
00079
00080 private void buildFormulaConditions() {
00081 Value v = null;
00082 QuantifiedVariable qv;
00083 for (Iterator i = placeHolders.iterator(); i.hasNext();) {
00084 StaticFieldRef sfr = (StaticFieldRef) i.next();
00085 SootField sf = sfr.getField();
00086 String fieldName = sf.getName();
00087 String quantifiedVariableName = fieldName.substring(15);
00088 qv = CompilationManager.getQuantifier(quantifiedVariableName);
00089 Value constraint = grimp.newNeExpr(sfr, NullConstant.v());
00090 if (qv.getConstraint() != null) {
00091 constraint = new LogicalAndExpr(constraint, QuantifiedVariableFixer.fix(qv));
00092 }
00093 if (v == null) {
00094 v = constraint;
00095 } else {
00096 v = new LogicalAndExpr(v, constraint);
00097 }
00098 }
00099 if (v != null) {
00100 parameterTable.put(defaultParam, v);
00101 }
00102 }
00103
00104
00105
00106
00107 public void caseABinaryExp(ABinaryExp node) {
00108 node.getLeft().apply(this);
00109 Value leftValue = currentValue;
00110 node.getRight().apply(this);
00111 Value rightValue = currentValue;
00112
00113 PBinOp binOp = node.getBinOp();
00114 if (binOp instanceof AAndBinOp) {
00115 currentValue = new LogicalAndExpr(leftValue, rightValue);
00116 } else if (binOp instanceof AOrBinOp) {
00117 currentValue = new LogicalOrExpr(leftValue, rightValue);
00118 } else {
00119 currentValue = new LogicalOrExpr(new ComplementExpr(leftValue), rightValue);
00120 }
00121 }
00122
00123
00124
00125
00126 public void caseAComplementExp(AComplementExp node) {
00127 node.getExp().apply(this);
00128 currentValue = new ComplementExpr(currentValue);
00129 }
00130
00131
00132
00133
00134 public void caseAExpWord(AExpWord node) {
00135 node.getExp().apply(this);
00136 String id = (String) parametersIterator.next();
00137
00138 String id2 = defaultPrefix + id.toLowerCase();
00139 parameterNameTable.put(id, id2);
00140 parameterTable.put(id2, currentValue);
00141 }
00142
00143
00144
00145
00146 public void caseAPredicateExp(APredicateExp node) {
00147 currentValue = (Value) predNodeGrimpTable.get(node);
00148 }
00149
00150
00151
00152
00153
00154 public String getFormula(String mapping) throws SpecificationException {
00155 if ("LTL".equalsIgnoreCase(mapping)) {
00156 try {
00157
00158
00159
00160
00161
00162 String ltl = "(("+defaultParam+") && (" + pattern.expandMapping("ltl", parameterNameTable) + "))";
00163 String result = "((!("+defaultParam+")) U " + ltl + ") || ([](!("+defaultParam+")))";
00164 return result;
00165 } catch (Exception e) {
00166 throw new SpecificationException(e.getMessage());
00167 }
00168 } else {
00169 throw new SpecificationException("Mapping to " + mapping + " is currently not supported");
00170 }
00171 }
00172
00173
00174
00175
00176 public Hashtable getParameterTable() throws SpecificationException {
00177 return parameterTable;
00178 }
00179 }