Coverage Report - org.apache.giraph.utils.TestGraph
 
Classes in this File Line Coverage Branch Coverage Complexity
TestGraph
0%
0/48
0%
0/12
0
 
 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.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  
  * TestGraph class for in-memory testing.
 44  
  *
 45  
  * @param <I> Vertex index type
 46  
  * @param <V> Vertex type
 47  
  * @param <E> Edge type
 48  
  */
 49  
 public class TestGraph<I extends WritableComparable,
 50  
                        V extends Writable,
 51  
                        E extends Writable>
 52  
                        implements Iterable<Vertex<I, V, E>> {
 53  
   /** Vertex value combiner */
 54  
   protected final VertexValueCombiner<V> vertexValueCombiner;
 55  
   /** The vertex values */
 56  
   protected Basic2ObjectMap<I, Vertex<I, V, E>> vertices;
 57  
   /** The configuration */
 58  
   protected ImmutableClassesGiraphConfiguration<I, V, E> conf;
 59  
   /** Callback that makes a decision on whether vertex should be created */
 60  
   private CreateSourceVertexCallback<I> createSourceVertexCallback;
 61  
 
 62  
   /**
 63  
    * Constructor requiring classes
 64  
    *
 65  
    * @param conf Should have vertex and edge classes set.
 66  
    */
 67  0
   public TestGraph(GiraphConfiguration conf) {
 68  0
     this.conf = new ImmutableClassesGiraphConfiguration<>(conf);
 69  0
     createSourceVertexCallback =
 70  
         GiraphConstants.CREATE_EDGE_SOURCE_VERTICES_CALLBACK
 71  0
             .newInstance(this.conf);
 72  0
     vertexValueCombiner = this.conf.createVertexValueCombiner();
 73  0
     vertices = BasicCollectionsUtils.create2ObjectMap(
 74  0
       this.conf.getVertexIdClass()
 75  
     );
 76  0
   }
 77  
 
 78  
   public Collection<Vertex<I, V, E>> getVertices() {
 79  0
     return vertices.values();
 80  
   }
 81  
 
 82  
   public int getVertexCount() {
 83  0
     return vertices.size();
 84  
   }
 85  
 
 86  
   public ImmutableClassesGiraphConfiguration<I, V, E> getConf() {
 87  0
     return conf;
 88  
   }
 89  
 
 90  
   /**
 91  
    * Clear all data
 92  
    *
 93  
    */
 94  
   public void clear() {
 95  0
     vertices.clear();
 96  0
   }
 97  
 
 98  
   /**
 99  
    * Add vertex
 100  
    *
 101  
    * @param vertex Vertex
 102  
    * @return this
 103  
    */
 104  
   public TestGraph<I, V, E> addVertex(Vertex<I, V, E> vertex) {
 105  0
     Vertex<I, V, E> previousVertex = vertices.get(vertex.getId());
 106  0
     if (previousVertex != null) {
 107  0
       vertexValueCombiner.combine(previousVertex.getValue(), vertex.getValue());
 108  0
       for (Edge<I, E> edge : vertex.getEdges()) {
 109  0
         previousVertex.addEdge(edge);
 110  0
       }
 111  
     } else {
 112  0
       vertices.put(vertex.getId(), vertex);
 113  
     }
 114  0
     return this;
 115  
   }
 116  
 
 117  
   /**
 118  
    * Add vertex with given ID
 119  
    *
 120  
    * @param id the index
 121  
    * @param value the value
 122  
    * @param edges all edges
 123  
    * @return this
 124  
    */
 125  
   public TestGraph<I, V, E> addVertex(I id, V value,
 126  
                                       Entry<I, E>... edges) {
 127  0
     addVertex(makeVertex(id, value, edges));
 128  0
     return this;
 129  
   }
 130  
 
 131  
   /**
 132  
    * Set vertex, replace if there was already a vertex with same id added
 133  
    *
 134  
    * @param vertex Vertex
 135  
    * @return this
 136  
    */
 137  
   public TestGraph<I, V, E> setVertex(Vertex<I, V, E> vertex) {
 138  0
     vertices.put(vertex.getId(), vertex);
 139  0
     return this;
 140  
   }
 141  
 
 142  
   /**
 143  
    * Set vertex, replace if there was already a vertex with same id added
 144  
    *
 145  
    * @param id the index
 146  
    * @param value the value
 147  
    * @param edges all edges
 148  
    * @return this
 149  
    */
 150  
   public TestGraph<I, V, E> setVertex(I id, V value, Entry<I, E>... edges) {
 151  0
     setVertex(makeVertex(id, value, edges));
 152  0
     return this;
 153  
   }
 154  
 
 155  
   /**
 156  
    * Add an edge to an existing vertex
 157  
    *`
 158  
    * @param vertexId Edge origin
 159  
    * @param edgePair The edge
 160  
    * @return this
 161  
    */
 162  
   public TestGraph<I, V, E> addEdge(I vertexId, Entry<I, E> edgePair) {
 163  0
     return addEdge(vertexId, edgePair.getKey(), edgePair.getValue());
 164  
   }
 165  
 
 166  
   /**
 167  
    * Add an edge to an existing vertex
 168  
    *
 169  
    * @param vertexId Edge origin
 170  
    * @param toVertex Edge destination
 171  
    * @param edgeValue Edge value
 172  
    * @return this
 173  
    */
 174  
   public TestGraph<I, V, E> addEdge(I vertexId, I toVertex, E edgeValue) {
 175  0
     if (!vertices.containsKey(vertexId)) {
 176  0
       if (createSourceVertexCallback.shouldCreateSourceVertex(vertexId)) {
 177  0
         Vertex<I, V, E> v = conf.createVertex();
 178  0
         v.initialize(vertexId, conf.createVertexValue());
 179  0
         vertices.put(vertexId, v);
 180  
       }
 181  
     }
 182  0
     Vertex<I, V, E> v = vertices.get(vertexId);
 183  0
     if (v != null) {
 184  0
       v.addEdge(EdgeFactory.create(toVertex, edgeValue));
 185  
     }
 186  0
     return this;
 187  
   }
 188  
 
 189  
   /**
 190  
    * An iterator over the vertices
 191  
    *
 192  
    * @return the iterator
 193  
    */
 194  
   @Override
 195  
   public Iterator<Vertex<I, V, E>> iterator() {
 196  0
     return vertices.valueIterator();
 197  
   }
 198  
 
 199  
   /**
 200  
    * Return a given vertex
 201  
    *
 202  
    * @param id the id
 203  
    * @return the value
 204  
    */
 205  
   public Vertex<I, V, E> getVertex(I id) {
 206  0
     return vertices.get(id);
 207  
   }
 208  
 
 209  
   /**
 210  
    * Create edges for given ids
 211  
    *
 212  
    * @param destEdgess ids to which the edges link
 213  
    * @return an iterable containing the edges
 214  
    */
 215  
   protected Iterable<Edge<I, E>>
 216  
   createEdges(Entry<I, E>... destEdgess) {
 217  0
     List<Edge<I, E>> edgesList = Lists.newArrayList();
 218  0
     for (Entry<I, E> e: destEdgess) {
 219  0
       edgesList.add(EdgeFactory.create(e.getKey(), e.getValue()));
 220  
     }
 221  0
     return edgesList;
 222  
   }
 223  
 
 224  
   /**
 225  
    * Create a vertex
 226  
    *
 227  
    * @param id the id of the vertex
 228  
    * @param value the vertex value
 229  
    * @param edges edges to other vertices
 230  
    * @return a new vertex
 231  
    */
 232  
   protected Vertex<I, V, E> makeVertex(I id, V value,
 233  
       Entry<I, E>... edges) {
 234  0
     Vertex<I, V, E> vertex = conf.createVertex();
 235  0
     vertex.initialize(id, value, createEdges(edges));
 236  0
     return vertex;
 237  
   }
 238  
 
 239  
   @Override
 240  
   public String toString() {
 241  0
     return MoreObjects.toStringHelper(this).add(
 242  0
       "vertices", vertices).toString();
 243  
   }
 244  
 }