00001 package gov.nasa.arc.ase.ltl;
00002
00003 import gov.nasa.arc.ase.jpf.jvm.ClassPath;
00004 import gov.nasa.arc.ase.jpf.JPFErrorException;
00005 import java.io.BufferedReader;
00006 import java.io.File;
00007 import java.io.FileNotFoundException;
00008 import java.io.FileReader;
00009 import java.io.InputStreamReader;
00010 import java.io.IOException;
00011
00012 import java.io.StringReader;
00013
00014 public class Rewriter {
00015 public static Formula applyRule(Formula expr, Formula rule, Formula rewritten) {
00016 return expr.rewrite(rule, rewritten);
00017 }
00018 public static void main(String[] args) {
00019 int osize = 0;
00020 int rsize = 0;
00021
00022 try {
00023 if(args.length != 0) {
00024 for(int i = 0; i < args.length; i++) {
00025 Formula f = Formula.parse(args[i]);
00026
00027 osize += f.size();
00028 System.out.println(f = rewrite(f));
00029 rsize += f.size();
00030
00031 System.err.println(rsize * 100 / osize + "% (" + osize + " => " + rsize + ")" );
00032 }
00033 } else {
00034 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
00035
00036 while(true) {
00037 try {
00038 String line = in.readLine();
00039
00040 if(line == null) break;
00041 if(line.equals("")) continue;
00042
00043 Formula f = Formula.parse(line);
00044
00045 osize += f.size();
00046 System.out.println(f = rewrite(f));
00047 rsize += f.size();
00048
00049 System.err.println(rsize * 100 / osize + "% (" + osize + " => " + rsize + ")" );
00050 } catch(IOException e) {
00051 System.out.println("error");
00052 break;
00053 }
00054 }
00055 }
00056 } catch(ParseErrorException e) {
00057 System.err.println("parse error: " + e.getMessage());
00058 }
00059 }
00060 public static Formula[] readRules() {
00061 Formula[] rules = new Formula[0];
00062
00063 try {
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 BufferedReader in = new BufferedReader(new StringReader
00086 (RulesClass.getRules()));
00087
00088 while(true) {
00089 String line = in.readLine();
00090 if(line == null) break;
00091 if(line.equals("")) continue;
00092
00093 Formula rule = Formula.parse(line);
00094
00095 Formula[] n = new Formula[rules.length+1];
00096 System.arraycopy(rules, 0, n, 0, rules.length);
00097 n[rules.length] = rule;
00098 rules = n;
00099 }
00100 } catch(IOException e) {
00101 } catch(ParseErrorException e) {
00102 System.err.println("parse error: " + e.getMessage());
00103 throw new JPFErrorException("");
00104
00105 }
00106
00107 return rules;
00108 }
00109 public static Formula rewrite(Formula expr) {
00110 Formula[] rules = readRules();
00111 if(rules == null) return expr;
00112
00113 try {
00114 boolean negated = false;
00115 boolean changed;
00116
00117 do {
00118 Formula old;
00119 changed = false;
00120
00121 do {
00122 old = expr;
00123 for(int i = 0; i < rules.length; i+=2)
00124 expr = applyRule(expr, rules[i], rules[i+1]);
00125 if(old != expr) changed = true;
00126 } while(old != expr);
00127
00128 negated = !negated;
00129 expr = Formula.parse("!" + expr.toString());
00130 } while(changed || negated);
00131
00132 return expr;
00133 } catch(ParseErrorException e) {
00134 return null;
00135 }
00136 }
00137 public static String rewrite(String expr) {
00138 try {
00139 return rewrite(Formula.parse(expr)).toString();
00140 } catch(ParseErrorException e) {
00141 return null;
00142 }
00143 }
00144 }