00001 package edu.ksu.cis.bandera.abstraction;
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.util.*;
00036 import edu.ksu.cis.bandera.abstraction.util.*;
00037 public class ArrayAbstraction extends Abstraction {
00038 private final Abstraction elementAbstraction;
00039 private final IntegralAbstraction indexAbstraction;
00040
00041
00042
00043
00044
00045 private ArrayAbstraction(Abstraction elementAbstraction, IntegralAbstraction indexAbstraction) {
00046 this.elementAbstraction = elementAbstraction;
00047 this.indexAbstraction = indexAbstraction;
00048 }
00049
00050
00051
00052
00053
00054 public boolean equals(Object other) {
00055 if (other instanceof ArrayAbstraction)
00056 return toString().equals(other.toString());
00057 else
00058 return false;
00059 }
00060
00061
00062
00063
00064
00065 public String generateAbstraction(String packageName) {
00066 String lineSeparator = System.getProperty("line.separator");
00067 boolean isConcreteIndex = indexAbstraction instanceof ConcreteIntegralAbstraction;
00068 String indexAbstractionClassName = indexAbstraction.getClass().getName();
00069 StringBuffer buffer = new StringBuffer();
00070 if (packageName == null) packageName = "";
00071
00072 if (elementAbstraction instanceof IntegralAbstraction) {
00073 if ("".equals(packageName)) {
00074 buffer.append("package " + packageName + ";" + lineSeparator + lineSeparator);
00075 }
00076 String className = elementAbstraction.toString() + indexAbstraction + "ArrayAbstraction";
00077 buffer.append("public class " + className + " {" + lineSeparator);
00078 buffer.append(" private final int[] elements;" + lineSeparator);
00079 if (isConcreteIndex) {
00080 buffer.append(" public " + className + "(int length) {" + lineSeparator);
00081 } else {
00082 buffer.append(" public " + className + "() {" + lineSeparator);
00083 buffer.append(" int length = " + AbstractionClassLoader.invokeMethod(indexAbstractionClassName, "getNumOfTokens", new Object[0], null, new Vector()) + ";" + lineSeparator);
00084 }
00085 buffer.append(" elements = new int[length];" + lineSeparator);
00086 buffer.append(" }" + lineSeparator);
00087 buffer.append(" public int lookup(int index) {" + lineSeparator);
00088 buffer.append(" int result;" + lineSeparator);
00089 if (isConcreteIndex) {
00090 buffer.append(" result = elements[index];" + lineSeparator);
00091 } else {
00092 buffer.append(" if (" + indexAbstractionClassName + ".isOne2One(index)) {" + lineSeparator);
00093 buffer.append(" result = elements[index];" + lineSeparator);
00094 buffer.append(" } else {" + lineSeparator);
00095 buffer.append(" result = Abstraction.choose(elements[index]);" + lineSeparator);
00096 buffer.append(" }" + lineSeparator);
00097 }
00098 buffer.append(" return result;" + lineSeparator);
00099 buffer.append(" }" + lineSeparator);
00100 buffer.append(" public void update(int index, int value) {" + lineSeparator);
00101 if (isConcreteIndex) {
00102 buffer.append(" elements[index] = value;" + lineSeparator);
00103 } else {
00104 buffer.append(" if (" + indexAbstractionClassName + ".isOne2One(index)) {" + lineSeparator);
00105 buffer.append(" elements[index] = value;" + lineSeparator);
00106 buffer.append(" } else {" + lineSeparator);
00107 buffer.append(" elements[index] = elements[index] | value;" + lineSeparator);
00108 buffer.append(" }" + lineSeparator);
00109 }
00110 buffer.append(" }" + lineSeparator);
00111 buffer.append("}" + lineSeparator);
00112 } else {
00113 }
00114 return buffer.toString();
00115 }
00116
00117
00118
00119
00120 public final Abstraction getElementAbstraction() {
00121 return elementAbstraction;
00122 }
00123
00124
00125
00126
00127 public final IntegralAbstraction getIndexAbstraction() {
00128 return indexAbstraction;
00129 }
00130
00131
00132
00133
00134 public int hashCode() {
00135 return toString().hashCode();
00136 }
00137
00138
00139
00140
00141 public String toString() {
00142 return elementAbstraction + "[" + indexAbstraction + "]";
00143 }
00144
00145
00146
00147
00148
00149
00150 public static ArrayAbstraction v(Abstraction elementAbstraction, IntegralAbstraction indexAbstraction) {
00151 return new ArrayAbstraction(elementAbstraction, indexAbstraction);
00152 }
00153 }