00001 package edu.ksu.cis.bandera.abstraction.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 java.util.*;
00037 import edu.ksu.cis.bandera.abstraction.specification.node.*;
00038 import edu.ksu.cis.bandera.abstraction.specification.analysis.*;
00039 public class AbstractionPrinter extends DepthFirstAdapter {
00040 private static final String lineSeparator = System.getProperty("line.separator");
00041 private StringBuffer buffer = new StringBuffer();
00042 private Vector expAbstractDefs = new Vector();
00043 private boolean isFull;
00044
00045
00046
00047
00048 private AbstractionPrinter(boolean isFull) {
00049 this.isFull = isFull;
00050 }
00051
00052
00053
00054
00055 public void caseAAbstractFunction(AAbstractFunction node) {
00056 println(" abstract (" + node.getId().toString().trim() + ")");
00057 println(" begin");
00058 {
00059 Object temp[] = node.getAbstractDef().toArray();
00060 for (int i = 0; i < temp.length; i++) {
00061 ((PAbstractDef) temp[i]).apply(this);
00062 }
00063 }
00064 println(" end");
00065 }
00066
00067
00068
00069
00070 public void caseAAnyAbstractDef(AAnyAbstractDef node) {
00071 if (expAbstractDefs.size() > 0) {
00072 Iterator i = expAbstractDefs.iterator();
00073 String result = (String) i.next();
00074 if (result.startsWith("(")) result = "!" + result;
00075 else result = "!(" + result + ")";
00076 while (i.hasNext()) {
00077 String temp = (String) i.next();
00078 if (temp.startsWith("(")) result = result + " && !" + temp;
00079 else result = result + " && !(" + temp + ")";
00080 }
00081 println(" " + result + " -> " + node.getId().toString().trim() + ";");
00082 } else {
00083 println(" _" + " -> " + node.getId().toString().trim() + ";");
00084 }
00085 }
00086
00087
00088
00089
00090 public void caseAAnyPattern(AAnyPattern node) {
00091 String result = "";
00092 int i = 0;
00093 for (StringTokenizer t = new StringTokenizer(node.getTokenTokenSet().toString(), ",{} "); t.hasMoreTokens(); i++) {
00094 result += (t.nextToken() + ", ");
00095 }
00096 if (i == 1) {
00097 println(" _ -> " + result.substring(0, result.length() - 2) + ";");
00098 } else {
00099 println(" _ -> {" + result.substring(0, result.length() - 2) + "};");
00100 }
00101 }
00102
00103
00104
00105
00106 public void caseADefaultToken(ADefaultToken node) {
00107 println(" DEFAULT = " + node.getId().toString().trim() + ";");
00108 }
00109
00110
00111
00112
00113 public void caseAExpAbstractDef(AExpAbstractDef node) {
00114 String exp = node.getExp().toString().trim();
00115 expAbstractDefs.add(exp);
00116 println(" " + exp + " -> " + node.getId().toString().trim() + ";");
00117 }
00118
00119
00120
00121
00122 public void caseAOne2oneSet(AOne2oneSet node) {
00123 if (node.getIdList() != null) {
00124 String result = "";
00125 for (StringTokenizer t = new StringTokenizer(node.getIdList().toString(), ", "); t.hasMoreTokens();) {
00126 result += (t.nextToken() + ", ");
00127 }
00128 println(" ONE2ONE = {" + result.substring(0, result.length() - 2) + "};");
00129 } else {
00130 println(" ONE2ONE = {};");
00131 }
00132 }
00133
00134
00135
00136
00137 public void caseAOperator(AOperator node) {
00138 println(" operator " + node.getOp() + node.getId());
00139 println(" begin");
00140 {
00141 Object temp[] = node.getPattern().toArray();
00142 for (int i = 0; i < temp.length; i++) {
00143 ((PPattern) temp[i]).apply(this);
00144 }
00145 }
00146 println(" end");
00147 }
00148
00149
00150
00151
00152 public void caseAPatternPattern(APatternPattern node) {
00153 String result = "";
00154 int i = 0;
00155 for (StringTokenizer t = new StringTokenizer(node.getTokenTokenSet().toString(), ",{} "); t.hasMoreTokens();) {
00156 result += (t.nextToken() + ", ");
00157 i++;
00158 }
00159 if (i == 1) {
00160 println(" (" + node.getLId().toString().trim() + ", " + node.getRId().toString().trim() + ") -> " + result.substring(0, result.length() - 2) + ";");
00161 } else {
00162 println(" (" + node.getLId().toString().trim() + ", " + node.getRId().toString().trim() + ") -> {" + result.substring(0, result.length() - 2) + "};");
00163 }
00164 }
00165
00166
00167
00168
00169 public void caseATest(ATest node) {
00170 println(" test " + node.getTOp() + node.getId());
00171 println(" begin");
00172 {
00173 Object temp[] = node.getPattern().toArray();
00174 for (int i = 0; i < temp.length; i++) {
00175 ((PPattern) temp[i]).apply(this);
00176 }
00177 }
00178 println(" end");
00179 }
00180
00181
00182
00183
00184 public void caseATokenSet(ATokenSet node) {
00185 String result = "";
00186 for (StringTokenizer t = new StringTokenizer(node.getIdSet().toString(), ",{} "); t.hasMoreTokens();) {
00187 result += (t.nextToken() + ", ");
00188 }
00189 println(" TOKENS = {" + result.substring(0, result.length() - 2) + "};");
00190 }
00191
00192
00193
00194
00195 public void caseAUnit(AUnit node) {
00196 println("abstraction " + node.getId() + "extends " + node.getType());
00197 println(" begin");
00198 node.getTokenSet().apply(this);
00199 if (isFull) {
00200 if (node.getDefaultToken() != null) {
00201 node.getDefaultToken().apply(this);
00202 }
00203 if (node.getOne2oneSet() != null) {
00204 node.getOne2oneSet().apply(this);
00205 }
00206 }
00207 node.getAbstractFunction().apply(this);
00208
00209 if (isFull) {
00210 Object temp[] = node.getOperatorTest().toArray();
00211 for (int i = 0; i < temp.length; i++) {
00212 ((POperatorTest) temp[i]).apply(this);
00213 }
00214 }
00215 println(" end");
00216 }
00217
00218
00219
00220
00221
00222
00223 public static String print(Start node, boolean isFull) {
00224 if (node != null) {
00225 AbstractionPrinter printer = new AbstractionPrinter(isFull);
00226 node.apply(printer);
00227 return printer.buffer.toString();
00228 }
00229 return null;
00230 }
00231
00232
00233
00234
00235
00236
00237 public static void print(Writer writer, Start Node, boolean isFull) throws IOException {
00238 writer.write(print(Node, isFull));
00239 writer.flush();
00240 }
00241
00242
00243
00244
00245 private void println(String s) {
00246 buffer.append(s + lineSeparator);
00247 }
00248 }