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