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