1 | |
---|
2 | package data; |
---|
3 | |
---|
4 | import clustering.Edge; |
---|
5 | import clustering.VertexString; |
---|
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 | |
---|
20 | public class GraphBuilder { |
---|
21 | |
---|
22 | Graph<String,Edge<String>> graph; |
---|
23 | |
---|
24 | public GraphBuilder(){ |
---|
25 | graph = new SparseGraph<String, Edge<String>>(); |
---|
26 | |
---|
27 | } |
---|
28 | |
---|
29 | public Graph<String, Edge<String>> getGraph() { |
---|
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)); |
---|
49 | graph.addEdge(new Edge<String>(splitted[0], splitted[1]), 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 | } |
---|