Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

SpecificationSaverLoader.java

00001 package edu.ksu.cis.bandera.specification;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 2000   Robby (robby@cis.ksu.edu)                    *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
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  * @param node edu.ksu.cis.bandera.specification.node.AAssert
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  * @param node edu.ksu.cis.bandera.specification.node.AAssertionImport
00066  */
00067 public void caseAAssertionImport(AAssertionImport node) {
00068     importContext = node;
00069     super.caseAAssertionImport(node);
00070 }
00071 /**
00072  * 
00073  * @param node edu.ksu.cis.bandera.specification.node.ANameNames
00074  */
00075 public void caseANameNames(ANameNames node) {
00076     currentAP.addAssertion(new Name(node.getName().toString()));
00077 }
00078 /**
00079  * 
00080  * @param node edu.ksu.cis.bandera.specification.node.ANamesNames
00081  */
00082 public void caseANamesNames(ANamesNames node) {
00083     node.getNames().apply(this);
00084     currentAP.addAssertion(new Name(node.getName().toString()));
00085 }
00086 /**
00087  * 
00088  * @param node edu.ksu.cis.bandera.specification.node.AOnDemandImportName
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  * @param node edu.ksu.cis.bandera.specification.node.APredicateImport
00102  */
00103 public void caseAPredicateImport(APredicateImport node) {
00104     importContext = node;
00105     super.caseAPredicateImport(node);
00106 }
00107 /**
00108  * 
00109  * @param node edu.ksu.cis.bandera.specification.node.ASpecificImportName
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  * @param node edu.ksu.cis.bandera.specification.node.ATl
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  * @return java.util.Vector
00181  */
00182 public static Vector getExceptions() {
00183     return exceptions;
00184 }
00185 /**
00186  * 
00187  * @return edu.ksu.cis.bandera.specification.datastructure.Property
00188  * @param filename java.lang.String
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  * @param property edu.ksu.cis.bandera.specification.datastructure.Property
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 }

Generated at Thu Feb 7 06:55:24 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001