source: branches/localSpectral/src/data/GraphBuilder.java @ 31

Last change on this file since 31 was 27, checked in by toshi, 14 years ago

creato il branch local spectral

File size: 1.8 KB
Line 
1
2package data;
3
4import edu.uci.ics.jung.graph.Graph;
5import edu.uci.ics.jung.graph.SparseGraph;
6import java.io.BufferedInputStream;
7import java.io.BufferedReader;
8import java.io.DataInputStream;
9import java.io.FileInputStream;
10import java.io.FileNotFoundException;
11import java.io.IOException;
12import java.io.InputStreamReader;
13import java.util.Arrays;
14import java.util.HashSet;
15import java.util.Set;
16
17
18public 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}
Note: See TracBrowser for help on using the repository browser.