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 java.io.*;
00036 import edu.ksu.cis.bandera.specification.pattern.*;
00037 import edu.ksu.cis.bandera.specification.pattern.datastructure.*;
00038 import java.util.*;
00039 import edu.ksu.cis.bandera.specification.ast.*;
00040 import edu.ksu.cis.bandera.specification.node.*;
00041 import edu.ksu.cis.bandera.specification.analysis.*;
00042 import edu.ksu.cis.bandera.specification.exception.*;
00043 import edu.ksu.cis.bandera.specification.datastructure.*;
00044 import edu.ksu.cis.bandera.specification.lexer.*;
00045 import edu.ksu.cis.bandera.specification.parser.*;
00046 import edu.ksu.cis.bandera.jjjc.symboltable.*;
00047
00048 public class SpecificationSaverLoader extends DepthFirstAdapter {
00049 private Property property = new Property();
00050 private TemporalLogicProperty currentTLP;
00051 private AssertionProperty currentAP;
00052 private static Vector exceptions;
00053 private PImport importContext;
00054
00055
00056
00057
00058 public void caseAAssert(AAssert node) {
00059 currentAP = new AssertionProperty(node.getId().toString().trim());
00060 node.getNames().apply(this);
00061 property.addAssertionProperty(currentAP);
00062 }
00063
00064
00065
00066
00067 public void caseAAssertionImport(AAssertionImport node) {
00068 importContext = node;
00069 super.caseAAssertionImport(node);
00070 }
00071
00072
00073
00074
00075 public void caseANameNames(ANameNames node) {
00076 currentAP.addAssertion(new Name(node.getName().toString()));
00077 }
00078
00079
00080
00081
00082 public void caseANamesNames(ANamesNames node) {
00083 node.getNames().apply(this);
00084 currentAP.addAssertion(new Name(node.getName().toString()));
00085 }
00086
00087
00088
00089
00090 public void caseAOnDemandImportName(AOnDemandImportName node) {
00091 if (importContext instanceof ATypeImport) {
00092 property.importPackage(new Name(node.getName().toString()));
00093 } else if (importContext instanceof AAssertionImport) {
00094 property.importAssertionSet(new Name(node.getName().toString()));
00095 } else if (importContext instanceof APredicateImport) {
00096 property.importPredicateSet(new Name(node.getName().toString()));
00097 }
00098 }
00099
00100
00101
00102
00103 public void caseAPredicateImport(APredicateImport node) {
00104 importContext = node;
00105 super.caseAPredicateImport(node);
00106 }
00107
00108
00109
00110
00111 public void caseASpecificImportName(ASpecificImportName node) {
00112 if (importContext instanceof ATypeImport) {
00113 property.importType(new Name(node.getName().toString()));
00114 } else if (importContext instanceof AAssertionImport) {
00115 property.importAssertion(new Name(node.getName().toString()));
00116 } else if (importContext instanceof APredicateImport) {
00117 property.importPredicate(new Name(node.getName().toString()));
00118 }
00119 }
00120
00121
00122
00123
00124 public void caseATl(ATl node) {
00125 currentTLP = new TemporalLogicProperty(node.getId().toString().trim());
00126 String q = "";
00127 {
00128 Object temp[] = node.getQtl().toArray();
00129 for (int i = 0; i < temp.length; i++) {
00130 q += ((PQtl) temp[i]).toString().trim();
00131 }
00132 }
00133
00134 String format = "";
00135 Vector parameters = new Vector();
00136 {
00137 Object temp[] = node.getWord().toArray();
00138 for (int i = 0; i < temp.length; i++) {
00139 String word = ((PWord) temp[i]).toString().trim();
00140 if (word.startsWith("{")) {
00141 parameters.addElement(word.substring(1, word.length() - 2));
00142 format += "{p} ";
00143 } else {
00144 format += (word + " ");
00145 }
00146 }
00147 }
00148
00149 try {
00150 format = Reformatter.format(format);
00151 Pattern pattern = PatternSaverLoader.getPatternWithFormat(format);
00152 if (pattern == null) {
00153 format = "\"" + format.substring(12);
00154 pattern = PatternSaverLoader.getPatternWithFormat(format);
00155 if (pattern == null) {
00156 exceptions.add(new SpecificationException("Can't find matching pattern for '" + node + "'"));
00157 return;
00158 }
00159 }
00160 currentTLP.setQuantifier(q);
00161 currentTLP.setPatternName(pattern.getName());
00162 currentTLP.setPatternScope(pattern.getScope());
00163 int length = parameters.size();
00164 int i = 0;
00165 Iterator k = parameters.iterator();
00166 for (Iterator j = pattern.getParametersOccurenceOrder().iterator(); i < length; i++) {
00167 currentTLP.putProposition((String) j.next(), (String) k.next());
00168 }
00169 } catch (Exception e) {
00170 exceptions.add(new SpecificationException(e.getMessage()));
00171 }
00172 property.addTemporalLogicProperty(currentTLP);
00173 }
00174 public void caseATypeImport(ATypeImport node) {
00175 importContext = node;
00176 super.caseATypeImport(node);
00177 }
00178
00179
00180
00181
00182 public static Vector getExceptions() {
00183 return exceptions;
00184 }
00185
00186
00187
00188
00189
00190 public static Property load(String filename) throws SpecificationException {
00191 exceptions = new Vector();
00192 try {
00193 FileReader fr = new FileReader(filename);
00194 Node node = new Parser(new Lexer(new PushbackReader(fr))).parse();
00195 node.apply(new Simplifier());
00196 SpecificationSaverLoader ssl = new SpecificationSaverLoader();
00197 node.apply(ssl);
00198 ssl.property.setFilename(filename);
00199 fr.close();
00200 return ssl.property;
00201 } catch (Exception e) {
00202 exceptions.addElement(e);
00203 }
00204 throw new SpecificationException("Errors occurred when loading property specification");
00205 }
00206
00207
00208
00209
00210 public static void save(Property property) throws SpecificationException {
00211 String newline = System.getProperty("line.separator");
00212 StringBuffer buffer = new StringBuffer();
00213
00214 for (Iterator i = property.getImportedType().iterator(); i.hasNext();) {
00215 buffer.append("import " + i.next() + ";" + newline);
00216 }
00217 for (Iterator i = property.getImportedPackage().iterator(); i.hasNext();) {
00218 buffer.append("import " + i.next() + ".*;" + newline);
00219 }
00220 for (Iterator i = property.getImportedAssertion().iterator(); i.hasNext();) {
00221 buffer.append("import assertion " + i.next() + ";" + newline);
00222 }
00223 for (Iterator i = property.getImportedAssertionSet().iterator(); i.hasNext();) {
00224 buffer.append("import assertion " + i.next() + ".*;" + newline);
00225 }
00226 for (Iterator i = property.getImportedPredicate().iterator(); i.hasNext();) {
00227 buffer.append("import predicate " + i.next() + ";" + newline);
00228 }
00229 for (Iterator i = property.getImportedPredicateSet().iterator(); i.hasNext();) {
00230 buffer.append("import predicate " + i.next() + ".*;" + newline);
00231 }
00232
00233 buffer.append(newline);
00234
00235 for (Enumeration e = property.getAssertionProperties().elements(); e.hasMoreElements();) {
00236 AssertionProperty ap = (AssertionProperty) e.nextElement();
00237 buffer.append(ap.getStringRepresentation() + newline + newline);
00238 }
00239
00240 for (Enumeration e = property.getTemporalLogicProperties().elements(); e.hasMoreElements();) {
00241 TemporalLogicProperty tlp = (TemporalLogicProperty) e.nextElement();
00242 buffer.append(tlp.getStringRepresentation() + newline + newline);
00243 }
00244
00245 try {
00246 FileWriter fw = new FileWriter(property.getFilename());
00247 String s = buffer.toString();
00248 fw.write(s, 0, s.length());
00249 fw.close();
00250 } catch (Exception e) {
00251 throw new SpecificationException(e.getMessage());
00252 }
00253 }
00254 }