1 4 package com.tctest.performance.sampledata; 5 6 import org.apache.commons.lang.SerializationUtils; 7 8 import java.io.DataOutputStream ; 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileNotFoundException ; 12 import java.io.FileOutputStream ; 13 import java.lang.reflect.Constructor ; 14 import java.util.LinkedList ; 15 import java.util.List ; 16 import java.util.Random ; 17 18 public class OrganicObjectGraphManager { 19 20 private static final String BASE_DIR = "tests.base"; 21 private static final String CLASS_TYPE = "OrganicObjectGraphNode"; 22 private static final String [] TYPE_NAMES = new String [] { "int", "String", "short", "double" }; 23 private static Class [] cachedNodeTypes; 24 private static final int BOUND = 50; 25 private static Random random = new Random (0); 26 private static StringBuffer buffer; 27 private static int sequenceNumber = -1; 28 29 public static void main(String [] args) throws Exception { 30 if (args[0] == null) { 31 System.err.println("Please supply the number of node variations as the first command line argument"); 32 return; 33 } 34 int variations = Integer.parseInt(args[0]); 35 int id = 0; 36 for (int i = 0; i < variations; i++) { 37 int size = 0; 38 do { 39 size = getRandom(BOUND); 40 } while (size == 0); 41 int[] types = new int[size]; 42 for (int j = 0; j < size; j++) { 43 types[j] = getRandom(TYPE_NAMES.length); 44 } 45 generateGraphTypes(types, id++); 46 } 47 } 48 49 public static void serializeGraph(OrganicObjectGraph graph, File file) throws FileNotFoundException { 50 SerializationUtils.serialize(graph, new FileOutputStream (file)); 51 } 52 53 public static OrganicObjectGraph deserializeGraph(File file) throws FileNotFoundException { 54 return (OrganicObjectGraph) SerializationUtils.deserialize(new FileInputStream (file)); 55 } 56 57 public static synchronized OrganicObjectGraph createOrganicGraph(int elements, String envKey) throws Exception { 58 Class [] nodeTypes = getNodeTypes(); 59 int size = nodeTypes.length; 60 OrganicObjectGraph[] leafNodes = new OrganicObjectGraph[elements]; 61 Constructor constructor = nodeTypes[getRandom(size)].getConstructor(new Class [] { Integer.TYPE, String .class }); 62 Object [] args = new Object [] { new Integer (nextSequenceNumber()), envKey }; 63 OrganicObjectGraph rootNode = (OrganicObjectGraph) constructor.newInstance(args); 64 leafNodes[0] = rootNode; 65 int boundMarker = 1; 66 OrganicObjectGraph currentNode; 67 OrganicObjectGraph leafNode; 68 while (boundMarker < leafNodes.length) { 69 currentNode = (OrganicObjectGraph) nodeTypes[getRandom(size)].newInstance(); 70 leafNode = leafNodes[getRandom(boundMarker)]; 71 currentNode.setParent(leafNode); 72 leafNode.addReference(currentNode); 73 leafNodes[boundMarker++] = currentNode; 74 } 75 return rootNode; 76 } 77 78 82 public static boolean validate(OrganicObjectGraph[] graphs, int size, int mutations) throws Exception { 83 random = new Random (0); 84 OrganicObjectGraph[] controlGraphs = new OrganicObjectGraph[graphs.length]; 85 for (int i = 0; i < graphs.length; i++) { 86 System.out.println("verify: " + graphs[i].envKey() + " #" + graphs[i].sequenceNumber()); 87 controlGraphs[i] = OrganicObjectGraphManager.createOrganicGraph(size, null); 88 for (int j = 0; j < graphs[i].changeIterationCount(); j++) { 89 controlGraphs[i].mutateRandom(mutations); 90 } 91 if (!controlGraphs[i].equals(graphs[i])) return false; 92 System.out.println(" graph[" + i + "] passed"); 93 } 94 return true; 95 } 96 97 private static int nextSequenceNumber() { 98 return ++sequenceNumber; 99 } 100 101 private static Class [] getNodeTypes() { 102 List types = new LinkedList (); 103 int count = 0; 104 if (cachedNodeTypes != null) { 105 return cachedNodeTypes; 106 } 107 String thisClassName = OrganicObjectGraph.class.getName(); 108 String [] parts = thisClassName.split("\\."); 109 String thisClassSimpleName = parts[parts.length - 1]; 110 String packages = thisClassName.substring(0, thisClassName.length() - thisClassSimpleName.length()); 111 try { 112 while (true) { 113 types.add(Class.forName(packages + CLASS_TYPE + "_" + count++)); 114 } 115 } catch (ClassNotFoundException e) { 116 } 118 cachedNodeTypes = (Class []) types.toArray(new Class [0]); 119 return cachedNodeTypes; 120 } 121 122 private static void generateGraphTypes(int[] types, int id) { 123 String className = CLASS_TYPE + "_" + id; 124 out("package com.tctest.performance.sampledata;"); 125 out(""); 126 out("public final class " + className + " extends OrganicObjectGraph {"); 127 out(""); 128 out(" private int size = " + types.length + ";"); 129 out(" private int[] types = new int[] { " + printTypesArray(types) + " };"); 130 out(""); 131 for (int i = 0; i < types.length; i++) { 132 out(" private " + TYPE_NAMES[types[i]] + " f" + i + ";"); 133 } 134 out(""); 135 out(" public " + className + "(int sequenceNumber, String envKey) {"); 136 out(" super(sequenceNumber, envKey);"); 137 out(" }"); 138 out(""); 139 out(" public " + className + "() {"); 140 out(" super();"); 141 out(" }"); 142 out(""); 143 out(" protected int getSize() {"); 144 out(" return size;"); 145 out(" }"); 146 out(""); 147 out(" protected int getType(int index) {"); 148 out(" return types[index];"); 149 out(" }"); 150 out(""); 151 printSetValueMethods(types); 152 printEqualsMethod(types, className); 153 out("}"); 154 writeFile(className); 155 } 156 157 private static void printSetValueMethods(int[] types) { 158 boolean[] setValueCreated = new boolean[TYPE_NAMES.length]; 159 for (int i = 0; i < types.length; i++) { 160 int type = types[i]; 161 if (!setValueCreated[type]) { 162 setValueCreated[type] = true; 163 out(" protected void setValue(int index, " + TYPE_NAMES[type] + " value) {"); 164 out(" switch (index) {"); 165 for (int j = 0; j < types.length; j++) { 166 if (type == types[j]) { 167 out(" case " + j + ":"); 168 out(" f" + j + " = value;"); 169 } 170 } 171 out(" default:"); 172 out(" break;"); 173 out(" }"); 174 out(" }"); 175 out(""); 176 } 177 } 178 } 179 180 private static void printEqualsMethod(int[] types, String className) { 181 String fieldName; 182 out(" public boolean equals(Object rawObj) {"); 183 out(" if (!(rawObj instanceof " + className 184 + ")) { System.out.println(\"not instanceof\"); System.out.println(rawObj.getClass().getName() + \"=" 185 + className + "\"); return false; }"); 186 out(" " + className + " obj = (" + className + ") rawObj;"); 187 for (int i = 0; i < types.length; i++) { 188 fieldName = "f" + i; 189 if (TYPE_NAMES[types[i]].equals("String")) { 191 out(" if (!(\"\" + " + fieldName + ").equals(\"\" + obj." + fieldName + ")) return false;"); 192 } else { 193 out(" if (" + fieldName + " != obj." + fieldName + ") return false;"); 194 } 195 } 196 out(" return super.equals(obj);"); 197 out(" }"); 198 } 199 200 private static String printTypesArray(int[] types) { 201 String out = String.valueOf(types[0]); 202 for (int i = 1; i < types.length; i++) { 203 out += ", " + types[i]; 204 } 205 return out; 206 } 207 208 private static void out(String str) { 209 if (buffer == null) buffer = new StringBuffer (); 210 buffer.append(str + "\n"); 211 } 212 213 private static void writeFile(String name) { 214 String [] pathParts = OrganicObjectGraphManager.class.getName().split("\\."); 215 String path = ""; 216 for (int i = 0; i < pathParts.length - 1; i++) { 217 path += pathParts[i] + File.separator; 218 } 219 File file = new File (BASE_DIR + File.separator + path + name + ".java"); 220 try { 221 DataOutputStream out = new DataOutputStream (new FileOutputStream (file)); 222 out.writeBytes(buffer.toString()); 223 out.close(); 224 } catch (Exception e) { 225 throw new RuntimeException (e); 226 } 227 buffer = null; 228 } 229 230 static int sequence; 231 232 private static int getRandom(int bound) { 233 return new Long (Math.round(Math.floor(bound * random.nextDouble()))).intValue(); 234 } 235 } 236 | Popular Tags |