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.io;
20  
21  import com.google.common.collect.Maps;
22  import org.apache.giraph.conf.GiraphConfiguration;
23  import org.apache.giraph.edge.ByteArrayEdges;
24  import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
25  import org.apache.giraph.io.formats.IntIntNullTextVertexInputFormat;
26  import org.apache.giraph.io.formats.IntNullTextEdgeInputFormat;
27  import org.apache.giraph.utils.ComputationCountEdges;
28  import org.apache.giraph.utils.IntIntNullNoOpComputation;
29  import org.apache.giraph.utils.InternalVertexRunner;
30  import org.junit.Test;
31  
32  import java.util.Map;
33  
34  import static org.junit.Assert.assertEquals;
35  import static org.junit.Assert.assertTrue;
36  
37  /**
38   * Test createSourceVertex configuration option
39   */
40  public class TestCreateSourceVertex {
41    @Test
42    public void testPositiveCreateSourceVertex() throws Exception {
43      String [] vertices = new String[] {
44          "1 0",
45          "2 0",
46          "3 0",
47          "4 0",
48      };
49      String [] edges = new String[] {
50          "1 2",
51          "1 5",
52          "2 4",
53          "2 1",
54          "3 4",
55          "4 1",
56          "4 5",
57          "6 2",
58          "7 8",
59          "4 8",
60      };
61  
62      GiraphConfiguration conf = getConf();
63      conf.setCreateSourceVertex(false);
64  
65      Iterable<String> results = InternalVertexRunner.run(conf, vertices, edges);
66      Map<Integer, Integer> values = parseResults(results);
67  
68      // Check that only vertices from vertex input are present in output graph
69      assertEquals(4, values.size());
70      // Check that the ids of vertices in output graph exactly match vertex input
71      assertTrue(values.containsKey(1));
72      assertTrue(values.containsKey(2));
73      assertTrue(values.containsKey(3));
74      assertTrue(values.containsKey(4));
75  
76      conf.setComputationClass(ComputationCountEdges.class);
77      results = InternalVertexRunner.run(conf, vertices, edges);
78      values = parseResults(results);
79  
80      // Check the number of edges of each vertex
81      assertEquals(2, (int) values.get(1));
82      assertEquals(2, (int) values.get(2));
83      assertEquals(1, (int) values.get(3));
84      assertEquals(3, (int) values.get(4));
85    }
86  
87    @Test
88    public void testNegativeCreateSourceVertex() throws Exception {
89      String [] vertices = new String[] {
90          "1 0",
91          "2 0",
92          "3 0",
93          "4 0",
94      };
95      String [] edges = new String[] {
96          "1 2",
97          "1 5",
98          "2 4",
99          "2 1",
100         "3 4",
101         "4 1",
102         "4 5",
103         "6 2",
104         "7 8",
105         "4 8",
106     };
107 
108     GiraphConfiguration conf = getConf();
109 
110     Iterable<String> results = InternalVertexRunner.run(conf, vertices, edges);
111     Map<Integer, Integer> values = parseResults(results);
112 
113     // Check that only vertices from vertex input are present in output graph
114     assertEquals(6, values.size());
115     // Check that the ids of vertices in output graph exactly match vertex input
116     assertTrue(values.containsKey(1));
117     assertTrue(values.containsKey(2));
118     assertTrue(values.containsKey(3));
119     assertTrue(values.containsKey(4));
120     assertTrue(values.containsKey(6));
121     assertTrue(values.containsKey(7));
122 
123     conf.setComputationClass(ComputationCountEdges.class);
124     results = InternalVertexRunner.run(conf, vertices, edges);
125     values = parseResults(results);
126 
127     // Check the number of edges of each vertex
128     assertEquals(2, (int) values.get(1));
129     assertEquals(2, (int) values.get(2));
130     assertEquals(1, (int) values.get(3));
131     assertEquals(3, (int) values.get(4));
132     assertEquals(1, (int) values.get(6));
133     assertEquals(1, (int) values.get(7));
134   }
135 
136   private GiraphConfiguration getConf() {
137     GiraphConfiguration conf = new GiraphConfiguration();
138     conf.setComputationClass(IntIntNullNoOpComputation.class);
139     conf.setOutEdgesClass(ByteArrayEdges.class);
140     conf.setVertexInputFormatClass(IntIntNullTextVertexInputFormat.class);
141     conf.setEdgeInputFormatClass(IntNullTextEdgeInputFormat.class);
142     conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
143     return conf;
144   }
145 
146   private static Map<Integer, Integer> parseResults(Iterable<String> results) {
147     Map<Integer, Integer> values = Maps.newHashMap();
148     for (String line : results) {
149       String[] tokens = line.split("\\s+");
150       int id = Integer.valueOf(tokens[0]);
151       int value = Integer.valueOf(tokens[1]);
152       values.put(id, value);
153     }
154     return values;
155   }
156 }