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