This project has retired. For details please refer to its
Attic page.
TestGraph xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.giraph.utils;
20
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map.Entry;
25
26 import com.google.common.base.MoreObjects;
27 import org.apache.giraph.conf.GiraphConfiguration;
28 import org.apache.giraph.conf.GiraphConstants;
29 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
30 import org.apache.giraph.edge.CreateSourceVertexCallback;
31 import org.apache.giraph.edge.Edge;
32 import org.apache.giraph.edge.EdgeFactory;
33 import org.apache.giraph.graph.Vertex;
34 import org.apache.giraph.graph.VertexValueCombiner;
35 import org.apache.giraph.types.ops.collections.Basic2ObjectMap;
36 import org.apache.giraph.types.ops.collections.BasicCollectionsUtils;
37 import org.apache.hadoop.io.Writable;
38 import org.apache.hadoop.io.WritableComparable;
39
40 import com.google.common.collect.Lists;
41
42
43
44
45
46
47
48
49 public class TestGraph<I extends WritableComparable,
50 V extends Writable,
51 E extends Writable>
52 implements Iterable<Vertex<I, V, E>> {
53
54 protected final VertexValueCombiner<V> vertexValueCombiner;
55
56 protected Basic2ObjectMap<I, Vertex<I, V, E>> vertices;
57
58 protected ImmutableClassesGiraphConfiguration<I, V, E> conf;
59
60 private CreateSourceVertexCallback<I> createSourceVertexCallback;
61
62
63
64
65
66
67 public TestGraph(GiraphConfiguration conf) {
68 this.conf = new ImmutableClassesGiraphConfiguration<>(conf);
69 createSourceVertexCallback =
70 GiraphConstants.CREATE_EDGE_SOURCE_VERTICES_CALLBACK
71 .newInstance(this.conf);
72 vertexValueCombiner = this.conf.createVertexValueCombiner();
73 vertices = BasicCollectionsUtils.create2ObjectMap(
74 this.conf.getVertexIdClass()
75 );
76 }
77
78 public Collection<Vertex<I, V, E>> getVertices() {
79 return vertices.values();
80 }
81
82 public int getVertexCount() {
83 return vertices.size();
84 }
85
86 public ImmutableClassesGiraphConfiguration<I, V, E> getConf() {
87 return conf;
88 }
89
90
91
92
93
94 public void clear() {
95 vertices.clear();
96 }
97
98
99
100
101
102
103
104 public TestGraph<I, V, E> addVertex(Vertex<I, V, E> vertex) {
105 Vertex<I, V, E> previousVertex = vertices.get(vertex.getId());
106 if (previousVertex != null) {
107 vertexValueCombiner.combine(previousVertex.getValue(), vertex.getValue());
108 for (Edge<I, E> edge : vertex.getEdges()) {
109 previousVertex.addEdge(edge);
110 }
111 } else {
112 vertices.put(vertex.getId(), vertex);
113 }
114 return this;
115 }
116
117
118
119
120
121
122
123
124
125 public TestGraph<I, V, E> addVertex(I id, V value,
126 Entry<I, E>... edges) {
127 addVertex(makeVertex(id, value, edges));
128 return this;
129 }
130
131
132
133
134
135
136
137 public TestGraph<I, V, E> setVertex(Vertex<I, V, E> vertex) {
138 vertices.put(vertex.getId(), vertex);
139 return this;
140 }
141
142
143
144
145
146
147
148
149
150 public TestGraph<I, V, E> setVertex(I id, V value, Entry<I, E>... edges) {
151 setVertex(makeVertex(id, value, edges));
152 return this;
153 }
154
155
156
157
158
159
160
161
162 public TestGraph<I, V, E> addEdge(I vertexId, Entry<I, E> edgePair) {
163 return addEdge(vertexId, edgePair.getKey(), edgePair.getValue());
164 }
165
166
167
168
169
170
171
172
173
174 public TestGraph<I, V, E> addEdge(I vertexId, I toVertex, E edgeValue) {
175 if (!vertices.containsKey(vertexId)) {
176 if (createSourceVertexCallback.shouldCreateSourceVertex(vertexId)) {
177 Vertex<I, V, E> v = conf.createVertex();
178 v.initialize(vertexId, conf.createVertexValue());
179 vertices.put(vertexId, v);
180 }
181 }
182 Vertex<I, V, E> v = vertices.get(vertexId);
183 if (v != null) {
184 v.addEdge(EdgeFactory.create(toVertex, edgeValue));
185 }
186 return this;
187 }
188
189
190
191
192
193
194 @Override
195 public Iterator<Vertex<I, V, E>> iterator() {
196 return vertices.valueIterator();
197 }
198
199
200
201
202
203
204
205 public Vertex<I, V, E> getVertex(I id) {
206 return vertices.get(id);
207 }
208
209
210
211
212
213
214
215 protected Iterable<Edge<I, E>>
216 createEdges(Entry<I, E>... destEdgess) {
217 List<Edge<I, E>> edgesList = Lists.newArrayList();
218 for (Entry<I, E> e: destEdgess) {
219 edgesList.add(EdgeFactory.create(e.getKey(), e.getValue()));
220 }
221 return edgesList;
222 }
223
224
225
226
227
228
229
230
231
232 protected Vertex<I, V, E> makeVertex(I id, V value,
233 Entry<I, E>... edges) {
234 Vertex<I, V, E> vertex = conf.createVertex();
235 vertex.initialize(id, value, createEdges(edges));
236 return vertex;
237 }
238
239 @Override
240 public String toString() {
241 return MoreObjects.toStringHelper(this).add(
242 "vertices", vertices).toString();
243 }
244 }