1 | package weka.clusterers.forMetisMQI.util; |
---|
2 | |
---|
3 | import java.awt.Color; |
---|
4 | import java.awt.Dimension; |
---|
5 | import java.awt.Paint; |
---|
6 | import java.util.Collection; |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.HashSet; |
---|
9 | import java.util.Iterator; |
---|
10 | import java.util.Map; |
---|
11 | import java.util.Set; |
---|
12 | import java.util.Map.Entry; |
---|
13 | |
---|
14 | import javax.swing.JPanel; |
---|
15 | |
---|
16 | import org.apache.commons.collections15.Transformer; |
---|
17 | |
---|
18 | import weka.clusterers.forMetisMQI.graph.Edge; |
---|
19 | import weka.clusterers.forMetisMQI.graph.Node; |
---|
20 | import weka.clusterers.forMetisMQI.graph.UndirectedGraph; |
---|
21 | import edu.uci.ics.jung.algorithms.layout.FRLayout; |
---|
22 | import edu.uci.ics.jung.algorithms.layout.KKLayout; |
---|
23 | import edu.uci.ics.jung.algorithms.layout.Layout; |
---|
24 | import edu.uci.ics.jung.graph.Graph; |
---|
25 | import edu.uci.ics.jung.visualization.BasicVisualizationServer; |
---|
26 | import edu.uci.ics.jung.visualization.decorators.ToStringLabeller; |
---|
27 | |
---|
28 | public class Util { |
---|
29 | |
---|
30 | public static JPanel panelCluster(Graph<Node, Edge> g, Collection<Node> cluster) { |
---|
31 | Set<Node> setNodes = new HashSet<Node>(); |
---|
32 | setNodes.addAll(cluster); |
---|
33 | Set<Set<Node>> clusters = new HashSet<Set<Node>>(); |
---|
34 | clusters.add(setNodes); |
---|
35 | return panelClusters(g, clusters); |
---|
36 | } |
---|
37 | |
---|
38 | public static JPanel panelClusters(Graph<Node, Edge> g, Set<Set<Node>> clusters) { |
---|
39 | return panelClusters(g,clusters,new ToStringLabeller<Node>()); |
---|
40 | } |
---|
41 | |
---|
42 | public static JPanel panelClusters(Graph<Node, Edge> g, Set<Set<Node>> clusters, Transformer<Node,String> transformer) { |
---|
43 | Layout<Node, Edge> layout = new KKLayout<Node, Edge>(g); |
---|
44 | layout.setSize(new Dimension(800, 600)); // sets the initial size of the space |
---|
45 | // The BasicVisualizationServer<V,E> is parameterized by the edge types |
---|
46 | BasicVisualizationServer<Node, Edge> vv = new BasicVisualizationServer<Node, Edge>( |
---|
47 | layout); |
---|
48 | |
---|
49 | class VertexPaintTransformer implements Transformer<Node, Paint> { |
---|
50 | Set<Set<Node>> clusters = null; |
---|
51 | Map<Set<Node>, Color> clustersColor = null; |
---|
52 | |
---|
53 | public Set<Node> getCluster(Node node) { |
---|
54 | Iterator<Set<Node>> clusterIterator = clusters.iterator(); |
---|
55 | while (clusterIterator.hasNext()) { |
---|
56 | Set<Node> cluster = clusterIterator.next(); |
---|
57 | if (cluster.contains(node)) |
---|
58 | return cluster; |
---|
59 | } |
---|
60 | return null; |
---|
61 | } |
---|
62 | |
---|
63 | public VertexPaintTransformer(Set<Set<Node>> clusters) { |
---|
64 | this.clusters = clusters; |
---|
65 | clustersColor = new HashMap<Set<Node>, Color>(clusters.size()); |
---|
66 | Iterator<Set<Node>> clusterIterator = clusters.iterator(); |
---|
67 | while (clusterIterator.hasNext()) { |
---|
68 | Set<Node> cluster = clusterIterator.next(); |
---|
69 | clustersColor.put(cluster, new Color(Random.instance() |
---|
70 | .nextInt(256), Random.instance().nextInt(256), |
---|
71 | Random.instance().nextInt(256))); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | public Paint transform(Node i) { |
---|
76 | Set<Node> cluster = getCluster(i); |
---|
77 | if (cluster == null) |
---|
78 | return Color.RED; |
---|
79 | else |
---|
80 | return clustersColor.get(getCluster(i)); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | Transformer<Node, Paint> vertexPaint = new VertexPaintTransformer( |
---|
85 | clusters); |
---|
86 | vv.setPreferredSize(new Dimension(800, 600)); // Sets the viewing area |
---|
87 | // size |
---|
88 | vv.getRenderContext().setVertexLabelTransformer(transformer); |
---|
89 | vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint); |
---|
90 | return vv; |
---|
91 | } |
---|
92 | |
---|
93 | public static JPanel panelGraph(Graph<Node, Edge> g){ |
---|
94 | Layout<Node, Edge> layout = new FRLayout<Node, Edge>(g); |
---|
95 | layout.setSize(new Dimension(800,600)); // sets the initial size of the space |
---|
96 | // The BasicVisualizationServer<V,E> is parameterized by the edge types |
---|
97 | BasicVisualizationServer<Node,Edge> vv = |
---|
98 | new BasicVisualizationServer<Node,Edge>(layout); |
---|
99 | vv.setPreferredSize(new Dimension(800,600)); //Sets the viewing area size |
---|
100 | vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); |
---|
101 | vv.getRenderContext().setEdgeLabelTransformer( |
---|
102 | new Transformer<Edge, String>() { |
---|
103 | public String transform(Edge e) { |
---|
104 | return ""; |
---|
105 | } |
---|
106 | }); |
---|
107 | return vv; |
---|
108 | } |
---|
109 | |
---|
110 | public static JPanel panelFlowGraph(Graph<Node, Edge> g, Map<Edge, Number> edgeFlowMap){ |
---|
111 | class EdgeTransformer implements Transformer<Edge,String> { |
---|
112 | Map<Edge,Number> edgeFlowMap = null; |
---|
113 | public String transform(Edge edge){ |
---|
114 | return edgeFlowMap.get(edge) + "/" + edge.getCapacity(); |
---|
115 | } |
---|
116 | public EdgeTransformer(Map<Edge,Number> edgeFlowMap) { |
---|
117 | this.edgeFlowMap = edgeFlowMap; |
---|
118 | } |
---|
119 | } |
---|
120 | Layout<Node, Edge> layout = new FRLayout<Node, Edge>(g); |
---|
121 | layout.setSize(new Dimension(800,600)); // sets the initial size of the space |
---|
122 | // The BasicVisualizationServer<V,E> is parameterized by the edge types |
---|
123 | BasicVisualizationServer<Node,Edge> vv = |
---|
124 | new BasicVisualizationServer<Node,Edge>(layout); |
---|
125 | vv.setPreferredSize(new Dimension(800,600)); //Sets the viewing area size |
---|
126 | vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); |
---|
127 | vv.getRenderContext().setEdgeLabelTransformer(new EdgeTransformer(edgeFlowMap)); |
---|
128 | return vv; |
---|
129 | } |
---|
130 | |
---|
131 | static public JPanel panelContractedGraph(CoarserGraphElement cge, Collection<Node> cluster) { |
---|
132 | Set<Node> setNodes = new HashSet<Node>(); |
---|
133 | setNodes.addAll(cluster); |
---|
134 | Set<Set<Node>> clusters = new HashSet<Set<Node>>(); |
---|
135 | clusters.add(setNodes); |
---|
136 | return panelClusters(cge.getContracted(), clusters, new ContractedVertexTransformer(cge)); |
---|
137 | } |
---|
138 | |
---|
139 | static public JPanel panelContractedGraph(CoarserGraphElement cge) { |
---|
140 | Layout<Node, Edge> layout = new KKLayout<Node, Edge>(cge.getContracted()); |
---|
141 | layout.setSize(new Dimension(800,600)); // sets the initial size of the space |
---|
142 | // The BasicVisualizationServer<V,E> is parameterized by the edge types |
---|
143 | BasicVisualizationServer<Node,Edge> vv = |
---|
144 | new BasicVisualizationServer<Node,Edge>(layout); |
---|
145 | vv.setPreferredSize(new Dimension(800,600)); //Sets the viewing area size |
---|
146 | vv.getRenderContext().setVertexLabelTransformer(new ContractedVertexTransformer(cge)); |
---|
147 | return vv; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | /** |
---|
152 | * Generates a small graph with 100 nodes and two big components. |
---|
153 | * For testing purpose. |
---|
154 | * @return the generated graph |
---|
155 | */ |
---|
156 | public UndirectedGraph generateGraph(){ |
---|
157 | UndirectedGraph g = new UndirectedGraph(); |
---|
158 | for (int i = 0; i < 50; i++) { |
---|
159 | g.addVertex(new Node(Integer.toString(i))); |
---|
160 | } |
---|
161 | for (int j = 0; j < 120; j++) { |
---|
162 | g.addEdge(new Edge(Integer.toString(j), 1, 1), new Node(Integer |
---|
163 | .toString(Random.instance().nextInt(50))), new Node(Integer |
---|
164 | .toString(Random.instance().nextInt(50)))); |
---|
165 | } |
---|
166 | for (int i = 50; i < 100; i++) { |
---|
167 | g.addVertex(new Node(Integer.toString(i))); |
---|
168 | } |
---|
169 | for (int j = 120; j < 240; j++) { |
---|
170 | g.addEdge(new Edge(Integer.toString(j), 1, 1), new Node(Integer |
---|
171 | .toString(50 + Random.instance().nextInt(50))), new Node( |
---|
172 | Integer.toString(50 + Random.instance().nextInt(50)))); |
---|
173 | } |
---|
174 | for (int j = 240; j < 250; j++) { |
---|
175 | g.addEdge(new Edge(Integer.toString(j), 1, 1), new Node(Integer |
---|
176 | .toString(50 + Random.instance().nextInt(50))), new Node( |
---|
177 | Integer.toString(Random.instance().nextInt(50)))); |
---|
178 | } |
---|
179 | return g; |
---|
180 | } |
---|
181 | } |
---|