1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.block_app.test_setup;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map.Entry;
23
24 import org.apache.giraph.conf.GiraphConfiguration;
25 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
26 import org.apache.giraph.edge.Edge;
27 import org.apache.giraph.edge.EdgeFactory;
28 import org.apache.giraph.function.Function;
29 import org.apache.giraph.function.Supplier;
30 import org.apache.giraph.graph.Vertex;
31 import org.apache.giraph.utils.TestGraph;
32 import org.apache.hadoop.io.DoubleWritable;
33 import org.apache.hadoop.io.FloatWritable;
34 import org.apache.hadoop.io.IntWritable;
35 import org.apache.hadoop.io.LongWritable;
36 import org.apache.hadoop.io.Writable;
37 import org.apache.hadoop.io.WritableComparable;
38 import org.apache.log4j.Logger;
39
40 import com.google.common.base.Preconditions;
41
42
43
44
45
46
47
48
49
50
51 public class NumericTestGraph<I extends WritableComparable,
52 V extends Writable,
53 E extends Writable> {
54
55 private static final Logger LOG = Logger.getLogger(NumericTestGraph.class);
56
57 private final TestGraph<I, V, E> testGraph;
58 private final Function<Number, I> numberToVertexId;
59 private final Function<Number, V> numberToVertexValue;
60 private final Function<Number, E> numberToEdgeValue;
61
62 public NumericTestGraph(TestGraph<I, V, E> testGraph) {
63 this.testGraph = testGraph;
64 numberToVertexId =
65 numericConvForType(testGraph.getConf().getVertexIdClass());
66 numberToVertexValue =
67 numericConvForType(testGraph.getConf().getVertexValueClass());
68 numberToEdgeValue =
69 numericConvForType(testGraph.getConf().getEdgeValueClass());
70 Preconditions.checkState(this.numberToVertexId != null);
71 }
72
73 public NumericTestGraph(GiraphConfiguration conf) {
74 this(new TestGraph<I, V, E>(conf));
75 }
76
77 public ImmutableClassesGiraphConfiguration<I, V, E> getConf() {
78 return testGraph.getConf();
79 }
80
81 public TestGraph<I, V, E> getTestGraph() {
82 return testGraph;
83 }
84
85
86
87
88
89 public Vertex<I, V, E> getVertex(Number vertexId) {
90 return testGraph.getVertex(numberToVertexId(vertexId));
91 }
92
93
94
95
96 public V getValue(Number vertexId) {
97 return testGraph.getVertex(numberToVertexId(vertexId)).getValue();
98 }
99
100
101
102
103 public int getVertexCount() {
104 return testGraph.getVertexCount();
105 }
106
107
108
109
110
111 public void addVertex(Number vertexId) {
112 addVertex(vertexId, (Number) null);
113 }
114
115
116
117
118
119 public void addVertex(Number vertexId, Number vertexValue) {
120 addVertex(vertexId, vertexValue, null);
121 }
122
123
124
125
126
127
128 public void addVertex(Number vertexId, Number vertexValue,
129 Number edgeValue, Number... outEdges) {
130 Vertex<I, V, E> vertex = makeVertex(
131 vertexId, vertexValue, edgeValue, outEdges);
132 testGraph.addVertex(vertex);
133 }
134
135
136
137
138
139 public void addVertex(Number vertexId, V vertexValue) {
140 addVertex(vertexId, vertexValue, null);
141 }
142
143
144
145
146
147
148 public void addVertex(Number vertexId, V vertexValue,
149 Supplier<E> edgeSupplier, Number... outEdges) {
150 Vertex<I, V, E> vertex = makeVertex(
151 vertexId, vertexValue, edgeSupplier, outEdges);
152 testGraph.addVertex(vertex);
153 }
154
155
156
157
158
159
160 public void addEdge(Number fromVertex, Number toVertex) {
161 addEdge(fromVertex, toVertex, (Number) null);
162 }
163
164
165
166
167
168
169 public void addEdge(Number fromVertex, Number toVertex, Number edgeValue) {
170 testGraph.addEdge(
171 numberToVertexId(fromVertex),
172 numberToVertexId(toVertex),
173 numberToEdgeValue(edgeValue));
174 }
175
176
177
178
179
180
181 public void addEdge(Number fromVertex, Number toVertex, E edgeValue) {
182 testGraph.addEdge(
183 numberToVertexId(fromVertex),
184 numberToVertexId(toVertex),
185 edgeValueOrCreate(edgeValue));
186 }
187
188
189
190
191
192
193 public void addSymmetricEdge(Number fromVertex, Number toVertex) {
194 addEdge(fromVertex, toVertex);
195 addEdge(toVertex, fromVertex);
196 }
197
198
199
200
201
202
203 public void addSymmetricEdge(
204 Number fromVertex, Number toVertex, Number edgeValue) {
205 addEdge(fromVertex, toVertex, edgeValue);
206 addEdge(toVertex, fromVertex, edgeValue);
207 }
208
209
210
211
212
213
214 public void addSymmetricEdge(Number vertexId, Number toVertex, E edgeValue) {
215 addEdge(vertexId, toVertex, edgeValue);
216 addEdge(toVertex, vertexId, edgeValue);
217 }
218
219
220
221
222
223
224
225
226 public Vertex<I, V, E> makeVertex(
227 Number vertexId, V vertexValue,
228 Entry<? extends Number, ? extends Number>... edges) {
229 Vertex<I, V, E> vertex = getConf().createVertex();
230 List<Edge<I, E>> edgesList = new ArrayList<>();
231
232 int i = 0;
233 for (Entry<? extends Number, ? extends Number> edge: edges) {
234 edgesList.add(EdgeFactory.create(
235 numberToVertexId(edge.getKey()),
236 numberToEdgeValue(edge.getValue())));
237 i++;
238 }
239 vertex.initialize(
240 numberToVertexId(vertexId),
241 vertexValue != null ?
242 vertexValue : getConf().createVertexValue(),
243 edgesList);
244 return vertex;
245 }
246
247
248
249
250
251
252
253
254 public Vertex<I, V, E> makeVertex(
255 Number vertexId, V vertexValue,
256 Supplier<E> edgeSupplier, Number... edges) {
257 Vertex<I, V, E> vertex = getConf().createVertex();
258
259 List<Edge<I, E>> edgesList = new ArrayList<>();
260 for (Number edge: edges) {
261 edgesList.add(
262 EdgeFactory.create(numberToVertexId.apply(edge),
263 edgeSupplier != null ?
264 edgeSupplier.get() : getConf().createEdgeValue()));
265 }
266
267 vertex.initialize(
268 numberToVertexId.apply(vertexId),
269 vertexValue != null ?
270 vertexValue : getConf().createVertexValue(),
271 edgesList);
272 return vertex;
273 }
274
275
276
277
278
279
280
281
282 public Vertex<I, V, E> makeVertex(
283 Number vertexId, Number value,
284 Number edgeValue, Number... edges) {
285 Vertex<I, V, E> vertex = getConf().createVertex();
286
287 List<Edge<I, E>> edgesList = new ArrayList<>();
288 for (Number edge: edges) {
289 edgesList.add(
290 EdgeFactory.create(numberToVertexId.apply(edge),
291 numberToEdgeValue(edgeValue)));
292 }
293
294 vertex.initialize(
295 numberToVertexId.apply(vertexId),
296 numberToVertexValue(value),
297 edgesList);
298 return vertex;
299 }
300
301 public I numberToVertexId(Number value) {
302 return numberToVertexId.apply(value);
303 }
304
305 public V numberToVertexValue(Number value) {
306 return value != null ?
307 numberToVertexValue.apply(value) : getConf().createVertexValue();
308 }
309
310 public E numberToEdgeValue(Number edgeValue) {
311 return edgeValue != null ?
312 numberToEdgeValue.apply(edgeValue) : getConf().createEdgeValue();
313 }
314
315 public E edgeValueOrCreate(E edgeValue) {
316 return edgeValue != null ? edgeValue : getConf().createEdgeValue();
317 }
318
319
320 public Vertex<I, V, E> createVertex() {
321 return getConf().createVertex();
322 }
323
324 public void initializeVertex(
325 Vertex<I, V, E> v, I id, Supplier<V> valueSupplier,
326 List<Edge<I, E>> edgesList) {
327 v.initialize(
328 id,
329 valueSupplier != null ?
330 valueSupplier.get() : getConf().createVertexValue(),
331 edgesList != null ? edgesList : new ArrayList<Edge<I, E>>());
332 }
333
334 @Override
335 public String toString() {
336 return testGraph.toString();
337 }
338
339
340 private static Function<Number, IntWritable> numberToInt() {
341 return new Function<Number, IntWritable>() {
342 @Override
343 public IntWritable apply(Number input) {
344 return new IntWritable(input.intValue());
345 }
346 };
347 }
348
349 private static Function<Number, LongWritable> numberToLong() {
350 return new Function<Number, LongWritable>() {
351 @Override
352 public LongWritable apply(Number input) {
353 return new LongWritable(input.longValue());
354 }
355 };
356 }
357
358 private static Function<Number, DoubleWritable> numberToDouble() {
359 return new Function<Number, DoubleWritable>() {
360 @Override
361 public DoubleWritable apply(Number input) {
362 return new DoubleWritable(input.doubleValue());
363 }
364 };
365 }
366
367 private static Function<Number, FloatWritable> numberToFloat() {
368 return new Function<Number, FloatWritable>() {
369 @Override
370 public FloatWritable apply(Number input) {
371 return new FloatWritable(input.floatValue());
372 }
373 };
374 }
375
376 private static <T> Function<Number, T> numericConvForType(Class<T> type) {
377 if (type.equals(LongWritable.class)) {
378 return (Function) numberToLong();
379 } else if (type.equals(IntWritable.class)) {
380 return (Function) numberToInt();
381 } else if (type.equals(DoubleWritable.class)) {
382 return (Function) numberToDouble();
383 } else if (type.equals(FloatWritable.class)) {
384 return (Function) numberToFloat();
385 } else {
386 return null;
387 }
388 }
389 }