[27] | 1 | |
---|
| 2 | package data; |
---|
| 3 | |
---|
[32] | 4 | import clustering.Edge; |
---|
| 5 | import clustering.VertexString; |
---|
[27] | 6 | import edu.uci.ics.jung.graph.Graph; |
---|
| 7 | import edu.uci.ics.jung.graph.SparseGraph; |
---|
| 8 | import java.io.BufferedInputStream; |
---|
| 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 | |
---|
[32] | 22 | Graph<String,Edge<String>> graph; |
---|
[27] | 23 | |
---|
| 24 | public GraphBuilder(){ |
---|
[32] | 25 | graph = new SparseGraph<String, Edge<String>>(); |
---|
[27] | 26 | |
---|
| 27 | } |
---|
| 28 | |
---|
[32] | 29 | public Graph<String, Edge<String>> getGraph() { |
---|
[27] | 30 | return graph; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | public void buildGraphFromARFF(String path, int maxreadline){ |
---|
| 34 | try { |
---|
| 35 | FileInputStream fstream = new FileInputStream(path); |
---|
| 36 | DataInputStream in = new DataInputStream(fstream); |
---|
| 37 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
---|
| 38 | |
---|
| 39 | Set<String> vertex = new HashSet<String>(); |
---|
| 40 | |
---|
| 41 | String read; |
---|
| 42 | int edge=0; |
---|
| 43 | int count=0; |
---|
| 44 | while(((read = br.readLine()) != null) && (count < maxreadline)){ |
---|
| 45 | count++; |
---|
| 46 | if(!(read.contains("@DATA") || read.contains("@RELATION") || read.contains("@ATTRIBUTE") || read.trim().isEmpty())){ |
---|
| 47 | String[] splitted = read.trim().split(","); |
---|
| 48 | vertex.addAll(Arrays.asList(splitted)); |
---|
[32] | 49 | graph.addEdge(new Edge<String>(splitted[0], splitted[1]), 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 | } |
---|