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