| [27] | 1 | |
|---|
| 2 | package data; |
|---|
| 3 | |
|---|
| [36] | 4 | import GraphType.DirectedDenseGraph; |
|---|
| [32] | 5 | import clustering.Edge; |
|---|
| [34] | 6 | import edu.uci.ics.jung.graph.DirectedSparseGraph; |
|---|
| [27] | 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 | |
|---|
| [32] | 20 | public class GraphBuilder { |
|---|
| [27] | 21 | |
|---|
| [36] | 22 | Graph<String,Integer> graph; |
|---|
| [27] | 23 | |
|---|
| [34] | 24 | public GraphBuilder(boolean directed){ |
|---|
| 25 | if (directed) |
|---|
| [36] | 26 | graph = new DirectedDenseGraph<String, Integer>(); |
|---|
| 27 | //graph = new DirectedSparseGraph<String, Integer>(); |
|---|
| [27] | 28 | |
|---|
| [34] | 29 | else |
|---|
| [36] | 30 | graph = new SparseGraph<String, Integer>(); |
|---|
| [27] | 31 | } |
|---|
| 32 | |
|---|
| [36] | 33 | public Graph<String, Integer> getGraph() { |
|---|
| [27] | 34 | return graph; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| [36] | 37 | public void buildGraphFromCVS(String path, int maxreadline){ |
|---|
| [27] | 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; |
|---|
| [36] | 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]); |
|---|
| [27] | 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 | } |
|---|