This project has retired. For details please refer to its Attic page.
ConnectedComponentsComputationTestInMemory xref
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.examples;
20  
21  import org.apache.giraph.combiner.MinimumIntMessageCombiner;
22  import org.apache.giraph.conf.GiraphConfiguration;
23  import org.apache.giraph.edge.ByteArrayEdges;
24  import org.apache.giraph.graph.Vertex;
25  import org.apache.giraph.utils.InternalVertexRunner;
26  import org.apache.giraph.utils.TestGraph;
27  import org.apache.hadoop.io.IntWritable;
28  import org.apache.hadoop.io.NullWritable;
29  import org.junit.Test;
30  
31  import com.google.common.collect.HashMultimap;
32  import com.google.common.collect.SetMultimap;
33  
34  import java.util.AbstractMap.SimpleEntry;
35  import java.util.Map.Entry;
36  import java.util.Set;
37  
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.assertTrue;
40  
41  /**
42   *  Tests for {@link ConnectedComponentsComputation}
43   */
44  public class ConnectedComponentsComputationTestInMemory {
45    public static Entry<IntWritable, NullWritable>[] makeEdges(int... args){
46      Entry<IntWritable, NullWritable> result[] =
47        new Entry[args.length];
48      for (int i = 0; i < args.length; i++){
49        result[i] = new SimpleEntry<IntWritable, NullWritable>(
50            new IntWritable(args[i]), NullWritable.get());
51      }
52      return result;
53    }
54    /**
55     * A local integration test on toy data
56     */
57    @Test
58    public void testToyData() throws Exception {
59      GiraphConfiguration conf = new GiraphConfiguration();
60      conf.setComputationClass(ConnectedComponentsComputation.class);
61      conf.setOutEdgesClass(ByteArrayEdges.class);
62      conf.setMessageCombinerClass(MinimumIntMessageCombiner.class);
63  
64      TestGraph<IntWritable, IntWritable, NullWritable> graph =
65        new TestGraph<IntWritable, IntWritable, NullWritable>(conf);
66      // a small graph with three components
67      graph.addVertex(new IntWritable(1), new IntWritable(1), makeEdges(2, 3))
68        .addVertex(new IntWritable(2), new IntWritable(2), makeEdges(1, 4, 5))
69        .addVertex(new IntWritable(3), new IntWritable(3), makeEdges(1, 4))
70        .addVertex(new IntWritable(4), new IntWritable(4),
71            makeEdges(2, 3, 5, 13))
72        .addVertex(new IntWritable(5), new IntWritable(5),
73            makeEdges(2, 4, 12, 13))
74        .addVertex(new IntWritable(12), new IntWritable(12), makeEdges(5, 13))
75        .addVertex(new IntWritable(13), new IntWritable(13), makeEdges(4, 5, 12))
76        .addVertex(new IntWritable(6), new IntWritable(6), makeEdges(7, 8))
77        .addVertex(new IntWritable(7), new IntWritable(7), makeEdges(6, 10, 11))
78        .addVertex(new IntWritable(8), new IntWritable(8), makeEdges(6, 10))
79        .addVertex(new IntWritable(10), new IntWritable(10), makeEdges(7, 8, 11))
80        .addVertex(new IntWritable(11), new IntWritable(11), makeEdges(7, 10))
81        .addVertex(new IntWritable(9), new IntWritable(9));
82  
83      // run internally
84      TestGraph<IntWritable, IntWritable, NullWritable> results =
85          InternalVertexRunner.runWithInMemoryOutput(conf, graph);
86  
87      SetMultimap<Integer, Integer> components = parseResults(results);
88  
89      Set<Integer> componentIDs = components.keySet();
90      assertEquals(3, componentIDs.size());
91      assertTrue(componentIDs.contains(1));
92      assertTrue(componentIDs.contains(6));
93      assertTrue(componentIDs.contains(9));
94  
95      Set<Integer> componentOne = components.get(1);
96      assertEquals(7, componentOne.size());
97      assertTrue(componentOne.contains(1));
98      assertTrue(componentOne.contains(2));
99      assertTrue(componentOne.contains(3));
100     assertTrue(componentOne.contains(4));
101     assertTrue(componentOne.contains(5));
102     assertTrue(componentOne.contains(12));
103     assertTrue(componentOne.contains(13));
104 
105     Set<Integer> componentTwo = components.get(6);
106     assertEquals(5, componentTwo.size());
107     assertTrue(componentTwo.contains(6));
108     assertTrue(componentTwo.contains(7));
109     assertTrue(componentTwo.contains(8));
110     assertTrue(componentTwo.contains(10));
111     assertTrue(componentTwo.contains(11));
112 
113     Set<Integer> componentThree = components.get(9);
114     assertEquals(1, componentThree.size());
115     assertTrue(componentThree.contains(9));
116   }
117 
118   private SetMultimap<Integer, Integer> parseResults(
119     TestGraph<IntWritable, IntWritable, NullWritable> results) {
120     SetMultimap<Integer, Integer> components = HashMultimap.create();
121     for (Vertex<IntWritable, IntWritable, NullWritable> vertex : results) {
122       int component = vertex.getValue().get();
123       components.put(component, vertex.getId().get());
124     }
125     return components;
126   }
127 }