1 | |
---|
2 | package clustering; |
---|
3 | |
---|
4 | import data.GraphBuilder; |
---|
5 | import edu.uci.ics.jung.graph.DirectedSparseGraph; |
---|
6 | import edu.uci.ics.jung.graph.Graph; |
---|
7 | import edu.uci.ics.jung.graph.SparseGraph; |
---|
8 | import java.awt.event.ActionEvent; |
---|
9 | import java.awt.event.ActionListener; |
---|
10 | import java.util.Collection; |
---|
11 | import java.util.List; |
---|
12 | import view.Viewer; |
---|
13 | |
---|
14 | public class GraphClusterer implements ActionListener{ |
---|
15 | |
---|
16 | private List<VertexScore<String>> global_ranking; |
---|
17 | private int currentElement; |
---|
18 | private Viewer viewGraph; |
---|
19 | private Graph<String, Edge<String>> graph; |
---|
20 | private boolean graph_output; |
---|
21 | private boolean isGraphDirected; |
---|
22 | private int minvolume; |
---|
23 | LocalSpectral<String,Edge<String>> clusterer; |
---|
24 | |
---|
25 | public GraphClusterer(String path, boolean isGraphDirected,int min_volume) { |
---|
26 | global_ranking = null; |
---|
27 | this.currentElement = 0; |
---|
28 | GraphBuilder builder = new GraphBuilder(isGraphDirected); |
---|
29 | builder.buildGraphFromARFF(path, 1000000); |
---|
30 | graph = builder.getGraph(); |
---|
31 | viewGraph = new Viewer(graph, this); |
---|
32 | graph_output = true; |
---|
33 | this.isGraphDirected = isGraphDirected; |
---|
34 | this.minvolume = min_volume; |
---|
35 | this.clusterer = new LocalSpectral(graph); |
---|
36 | } |
---|
37 | |
---|
38 | public void actionPerformed(ActionEvent ae) { |
---|
39 | System.out.println("Clicked!"); |
---|
40 | this.visualNextCluster(); |
---|
41 | } |
---|
42 | |
---|
43 | public void clusterize(boolean graph_output){ |
---|
44 | |
---|
45 | global_ranking = clusterer.getGlobalRank(); |
---|
46 | this.graph_output = graph_output; |
---|
47 | |
---|
48 | System.out.println("GLOBAL RANKING"); |
---|
49 | for(VertexScore<String> v : global_ranking) |
---|
50 | System.out.println(v.toString()); |
---|
51 | |
---|
52 | if (graph_output) |
---|
53 | viewGraph.viewGraphRank(global_ranking,null); |
---|
54 | } |
---|
55 | |
---|
56 | public void visualNextCluster(){ |
---|
57 | if(this.currentElement > global_ranking.size()-1) |
---|
58 | return; |
---|
59 | |
---|
60 | List<String> cut = clusterer.clusterPageRankPriors(global_ranking.get(currentElement).getVertex(),this.minvolume); |
---|
61 | Graph cut_graph; |
---|
62 | if (isGraphDirected) |
---|
63 | cut_graph = new DirectedSparseGraph<String, Edge<String>>(); |
---|
64 | else |
---|
65 | cut_graph = new SparseGraph<String, Edge<String>>(); |
---|
66 | for(String vertex : cut){ |
---|
67 | Collection<Edge<String>> out_edges = graph.getOutEdges(vertex); |
---|
68 | for(Edge<String> edge : out_edges){ |
---|
69 | String out_node = graph.getOpposite(vertex, edge); |
---|
70 | if (cut.contains(out_node)){ |
---|
71 | cut_graph.addEdge(edge, edge.getVertex1(),edge.getVertex2()); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | viewGraph.setGraph(cut_graph); |
---|
76 | float vertex_rank = (float) global_ranking.get(this.currentElement).getScore(); |
---|
77 | viewGraph.setText("Page Rank Value: "+vertex_rank); |
---|
78 | if (graph_output) |
---|
79 | viewGraph.viewGraphRank(global_ranking, global_ranking.get(this.currentElement).getVertex()); |
---|
80 | |
---|
81 | this.currentElement++; |
---|
82 | |
---|
83 | } |
---|
84 | } |
---|