View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.utils;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map.Entry;
25  
26  import org.apache.giraph.conf.GiraphConfiguration;
27  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
28  import org.apache.giraph.edge.Edge;
29  import org.apache.giraph.edge.EdgeFactory;
30  import org.apache.giraph.graph.Vertex;
31  import org.apache.giraph.graph.VertexValueCombiner;
32  import org.apache.hadoop.io.Writable;
33  import org.apache.hadoop.io.WritableComparable;
34  
35  import com.google.common.base.Objects;
36  import com.google.common.collect.Lists;
37  import com.google.common.collect.Maps;
38  
39  /**
40   * TestGraph class for in-memory testing.
41   *
42   * @param <I> Vertex index type
43   * @param <V> Vertex type
44   * @param <E> Edge type
45   */
46  public class TestGraph<I extends WritableComparable,
47                         V extends Writable,
48                         E extends Writable>
49                         implements Iterable<Vertex<I, V, E>> {
50    /** Vertex value combiner */
51    protected final VertexValueCombiner<V> vertexValueCombiner;
52    /** The vertex values */
53    protected HashMap<I, Vertex<I, V, E>> vertices = Maps.newHashMap();
54    /** The configuration */
55    protected ImmutableClassesGiraphConfiguration<I, V, E> conf;
56  
57    /**
58     * Constructor requiring classes
59     *
60     * @param conf Should have vertex and edge classes set.
61     */
62    public TestGraph(GiraphConfiguration conf) {
63      this.conf = new ImmutableClassesGiraphConfiguration(conf);
64      vertexValueCombiner = this.conf.createVertexValueCombiner();
65    }
66  
67    public HashMap<I, Vertex<I, V, E>> getVertices() {
68      return vertices;
69    }
70  
71    public ImmutableClassesGiraphConfiguration<I, V, E> getConf() {
72      return conf;
73    }
74  
75    /**
76     * Clear all data
77     *
78     */
79    public void clear() {
80      vertices.clear();
81    }
82  
83    /**
84     * Add vertex
85     *
86     * @param vertex Vertex
87     * @return this
88     */
89    public TestGraph<I, V, E> addVertex(Vertex<I, V, E> vertex) {
90      Vertex<I, V, E> previousVertex = vertices.get(vertex.getId());
91      if (previousVertex != null) {
92        vertexValueCombiner.combine(previousVertex.getValue(), vertex.getValue());
93        for (Edge<I, E> edge : vertex.getEdges()) {
94          previousVertex.addEdge(edge);
95        }
96      } else {
97        vertices.put(vertex.getId(), vertex);
98      }
99      return this;
100   }
101 
102   /**
103    * Add vertex with given ID
104    *
105    * @param id the index
106    * @param value the value
107    * @param edges all edges
108    * @return this
109    */
110   public TestGraph<I, V, E> addVertex(I id, V value,
111                                          Entry<I, E>... edges) {
112     addVertex(makeVertex(id, value, edges));
113     return this;
114   }
115 
116   /**
117    * Set vertex, replace if there was already a vertex with same id added
118    *
119    * @param vertex Vertex
120    * @return this
121    */
122   public TestGraph<I, V, E> setVertex(Vertex<I, V, E> vertex) {
123     vertices.put(vertex.getId(), vertex);
124     return this;
125   }
126 
127   /**
128    * Set vertex, replace if there was already a vertex with same id added
129    *
130    * @param id the index
131    * @param value the value
132    * @param edges all edges
133    * @return this
134    */
135   public TestGraph<I, V, E> setVertex(I id, V value, Entry<I, E>... edges) {
136     setVertex(makeVertex(id, value, edges));
137     return this;
138   }
139 
140   /**
141    * Add an edge to an existing vertex
142    *
143    * @param vertexId Edge origin
144    * @param edgePair The edge
145    * @return this
146    */
147   public TestGraph<I, V, E> addEdge(I vertexId, Entry<I, E> edgePair) {
148     if (!vertices.containsKey(vertexId)) {
149       Vertex<I, V, E> v = conf.createVertex();
150       v.initialize(vertexId, conf.createVertexValue());
151       vertices.put(vertexId, v);
152     }
153     vertices.get(vertexId)
154       .addEdge(EdgeFactory.create(edgePair.getKey(),
155                                                edgePair.getValue()));
156     return this;
157   }
158 
159   /**
160    * Add an edge to an existing vertex
161    *
162    * @param vertexId Edge origin
163    * @param toVertex Edge destination
164    * @param edgeValue Edge value
165    * @return this
166    */
167   public TestGraph<I, V, E> addEdge(I vertexId, I toVertex, E edgeValue) {
168     if (!vertices.containsKey(vertexId)) {
169       Vertex<I, V, E> v = conf.createVertex();
170       v.initialize(vertexId, conf.createVertexValue());
171       vertices.put(vertexId, v);
172     }
173     vertices.get(vertexId)
174       .addEdge(EdgeFactory.create(toVertex, edgeValue));
175     return this;
176   }
177   /**
178    * An iterator over the ids
179    *
180    * @return the iterator
181    */
182   public Iterator<I> idIterator() {
183     return vertices.keySet().iterator();
184   }
185 
186   /**
187    * An iterator over the vertices
188    *
189    * @return the iterator
190    */
191   @Override
192   public Iterator<Vertex<I, V, E>> iterator() {
193     return vertices.values().iterator();
194   }
195 
196   /**
197    * Return a given vertex
198    *
199    * @param id the id
200    * @return the value
201    */
202   public Vertex<I, V, E> getVertex(I id) {
203     return vertices.get(id);
204   }
205 
206   /**
207    * Create edges for given ids
208    *
209    * @param destEdgess ids to which the edges link
210    * @return an iterable containing the edges
211    */
212   protected Iterable<Edge<I, E>>
213   createEdges(Entry<I, E>... destEdgess) {
214     List<Edge<I, E>> edgesList = Lists.newArrayList();
215     for (Entry<I, E> e: destEdgess) {
216       edgesList.add(EdgeFactory.create(e.getKey(), e.getValue()));
217     }
218     return edgesList;
219   }
220 
221   /**
222    * Create a vertex
223    *
224    * @param id the id of the vertex
225    * @param value the vertex value
226    * @param edges edges to other vertices
227    * @return a new vertex
228    */
229   protected Vertex<I, V, E> makeVertex(I id, V value,
230       Entry<I, E>... edges) {
231     Vertex<I, V, E> vertex = conf.createVertex();
232     vertex.initialize(id, value, createEdges(edges));
233     return vertex;
234   }
235 
236   @Override
237   public String toString() {
238     return Objects.toStringHelper(this).add("vertices", vertices).toString();
239   }
240 }