KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > sampledata > OrganicObjectGraphManager


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.performance.sampledata;
5
6 import org.apache.commons.lang.SerializationUtils;
7
8 import java.io.DataOutputStream JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileNotFoundException JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.lang.reflect.Constructor JavaDoc;
14 import java.util.LinkedList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Random JavaDoc;
17
18 public class OrganicObjectGraphManager {
19
20   private static final String JavaDoc BASE_DIR = "tests.base";
21   private static final String JavaDoc CLASS_TYPE = "OrganicObjectGraphNode";
22   private static final String JavaDoc[] TYPE_NAMES = new String JavaDoc[] { "int", "String", "short", "double" };
23   private static Class JavaDoc[] cachedNodeTypes;
24   private static final int BOUND = 50;
25   private static Random JavaDoc random = new Random JavaDoc(0);
26   private static StringBuffer JavaDoc buffer;
27   private static int sequenceNumber = -1;
28
29   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
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 JavaDoc file) throws FileNotFoundException JavaDoc {
50     SerializationUtils.serialize(graph, new FileOutputStream JavaDoc(file));
51   }
52
53   public static OrganicObjectGraph deserializeGraph(File JavaDoc file) throws FileNotFoundException JavaDoc {
54     return (OrganicObjectGraph) SerializationUtils.deserialize(new FileInputStream JavaDoc(file));
55   }
56
57   public static synchronized OrganicObjectGraph createOrganicGraph(int elements, String JavaDoc envKey) throws Exception JavaDoc {
58     Class JavaDoc[] nodeTypes = getNodeTypes();
59     int size = nodeTypes.length;
60     OrganicObjectGraph[] leafNodes = new OrganicObjectGraph[elements];
61     Constructor JavaDoc constructor = nodeTypes[getRandom(size)].getConstructor(new Class JavaDoc[] { Integer.TYPE, String JavaDoc.class });
62     Object JavaDoc[] args = new Object JavaDoc[] { new Integer JavaDoc(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   /**
79    * @NOTTHREADSAFE this method must be called by a process other than the one which created the graphs in the fist
80    * place
81    */

82   public static boolean validate(OrganicObjectGraph[] graphs, int size, int mutations) throws Exception JavaDoc {
83     random = new Random JavaDoc(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 JavaDoc[] getNodeTypes() {
102     List JavaDoc types = new LinkedList JavaDoc();
103     int count = 0;
104     if (cachedNodeTypes != null) {
105       return cachedNodeTypes;
106     }
107     String JavaDoc thisClassName = OrganicObjectGraph.class.getName();
108     String JavaDoc[] parts = thisClassName.split("\\.");
109     String JavaDoc thisClassSimpleName = parts[parts.length - 1];
110     String JavaDoc 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 JavaDoc e) {
116       // HACK!
117
}
118     cachedNodeTypes = (Class JavaDoc[]) types.toArray(new Class JavaDoc[0]);
119     return cachedNodeTypes;
120   }
121
122   private static void generateGraphTypes(int[] types, int id) {
123     String JavaDoc 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 JavaDoc className) {
181     String JavaDoc 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       // out("System.out.println(" + fieldName + " + \"=\" + obj." + fieldName + ");");
190
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 JavaDoc printTypesArray(int[] types) {
201     String JavaDoc 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 JavaDoc str) {
209     if (buffer == null) buffer = new StringBuffer JavaDoc();
210     buffer.append(str + "\n");
211   }
212
213   private static void writeFile(String JavaDoc name) {
214     String JavaDoc[] pathParts = OrganicObjectGraphManager.class.getName().split("\\.");
215     String JavaDoc path = "";
216     for (int i = 0; i < pathParts.length - 1; i++) {
217       path += pathParts[i] + File.separator;
218     }
219     File JavaDoc file = new File JavaDoc(BASE_DIR + File.separator + path + name + ".java");
220     try {
221       DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
222       out.writeBytes(buffer.toString());
223       out.close();
224     } catch (Exception JavaDoc e) {
225       throw new RuntimeException JavaDoc(e);
226     }
227     buffer = null;
228   }
229
230   static int sequence;
231
232   private static int getRandom(int bound) {
233     return new Long JavaDoc(Math.round(Math.floor(bound * random.nextDouble()))).intValue();
234   }
235 }
236
Popular Tags