1 | |
---|
2 | package data; |
---|
3 | |
---|
4 | import GraphType.DirectedDenseGraph; |
---|
5 | import clustering.Edge; |
---|
6 | import edu.uci.ics.jung.graph.DirectedSparseGraph; |
---|
7 | import edu.uci.ics.jung.graph.Graph; |
---|
8 | import edu.uci.ics.jung.graph.SparseGraph; |
---|
9 | import java.io.BufferedReader; |
---|
10 | import java.io.DataInputStream; |
---|
11 | import java.io.FileInputStream; |
---|
12 | import java.io.FileNotFoundException; |
---|
13 | import java.io.IOException; |
---|
14 | import java.io.InputStreamReader; |
---|
15 | import java.util.Arrays; |
---|
16 | import java.util.HashSet; |
---|
17 | import java.util.Set; |
---|
18 | |
---|
19 | |
---|
20 | public class GraphBuilder { |
---|
21 | |
---|
22 | Graph<String,Integer> graph; |
---|
23 | |
---|
24 | public GraphBuilder(boolean directed){ |
---|
25 | if (directed) |
---|
26 | graph = new DirectedDenseGraph<String, Integer>(); |
---|
27 | //graph = new DirectedSparseGraph<String, Integer>(); |
---|
28 | |
---|
29 | else |
---|
30 | graph = new SparseGraph<String, Integer>(); |
---|
31 | } |
---|
32 | |
---|
33 | public Graph<String, Integer> getGraph() { |
---|
34 | return graph; |
---|
35 | } |
---|
36 | |
---|
37 | public void buildGraphFromCVS(String path, int maxreadline){ |
---|
38 | try { |
---|
39 | FileInputStream fstream = new FileInputStream(path); |
---|
40 | DataInputStream in = new DataInputStream(fstream); |
---|
41 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
---|
42 | |
---|
43 | String read; |
---|
44 | int edge=0; |
---|
45 | String[] splitted; |
---|
46 | while((read = br.readLine()) != null){ |
---|
47 | if(!read.trim().isEmpty()){ |
---|
48 | splitted = read.trim().split(","); |
---|
49 | graph.addEdge(edge, splitted[0], splitted[1]); |
---|
50 | edge++; |
---|
51 | } |
---|
52 | } |
---|
53 | br.close(); |
---|
54 | in.close(); |
---|
55 | fstream.close(); |
---|
56 | |
---|
57 | } catch (FileNotFoundException e) { |
---|
58 | System.out.println("<GraphBuilder> Error: file not found!"); |
---|
59 | } catch(IOException e){ |
---|
60 | System.out.println("<GraphBuilder> Error: closing FileInputStream"); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | } |
---|