1 | package de.ugoe.cs.eventbench.commands;
|
---|
2 |
|
---|
3 | import java.awt.Dimension;
|
---|
4 | import java.awt.Rectangle;
|
---|
5 | import java.awt.Shape;
|
---|
6 | import java.security.InvalidParameterException;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.JFrame;
|
---|
10 |
|
---|
11 | import org.apache.commons.collections15.Transformer;
|
---|
12 |
|
---|
13 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
14 | import de.ugoe.cs.eventbench.models.Trie;
|
---|
15 | import de.ugoe.cs.eventbench.models.Trie.Edge;
|
---|
16 | import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
|
---|
17 | import de.ugoe.cs.eventbench.models.TrieBasedModel;
|
---|
18 | import de.ugoe.cs.util.console.Command;
|
---|
19 | import de.ugoe.cs.util.console.Console;
|
---|
20 | import edu.uci.ics.jung.algorithms.layout.Layout;
|
---|
21 | import edu.uci.ics.jung.algorithms.layout.TreeLayout;
|
---|
22 | import edu.uci.ics.jung.graph.Tree;
|
---|
23 | import edu.uci.ics.jung.visualization.BasicVisualizationServer;
|
---|
24 | import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
|
---|
25 | import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * <p>
|
---|
29 | * Command that visualizes the {@link Trie} of a {@link TrieBasedModel}.
|
---|
30 | * </p>
|
---|
31 | *
|
---|
32 | * @author Steffen Herbold
|
---|
33 | * @version 1.0
|
---|
34 | */
|
---|
35 | public class CMDshowTrie implements Command {
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * (non-Javadoc)
|
---|
39 | *
|
---|
40 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
41 | */
|
---|
42 | @Override
|
---|
43 | public void help() {
|
---|
44 | Console.println("Usage: showTrie <modelName>");
|
---|
45 | }
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * (non-Javadoc)
|
---|
49 | *
|
---|
50 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
51 | */
|
---|
52 | @Override
|
---|
53 | public void run(List<Object> parameters) {
|
---|
54 | String modelname;
|
---|
55 | try {
|
---|
56 | modelname = (String) parameters.get(0);
|
---|
57 | } catch (Exception e) {
|
---|
58 | throw new InvalidParameterException();
|
---|
59 | }
|
---|
60 |
|
---|
61 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
62 | .getData(modelname);
|
---|
63 | if (dataObject == null) {
|
---|
64 | Console.printerrln("Object " + modelname + " not found in storage.");
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | if (!(dataObject instanceof TrieBasedModel)) {
|
---|
68 | Console.printerr("Object " + modelname
|
---|
69 | + " not of type TrieBasedModel.");
|
---|
70 | }
|
---|
71 | TrieBasedModel model = (TrieBasedModel) dataObject;
|
---|
72 | Tree<TrieVertex, Edge> graph = model.getTrieGraph();
|
---|
73 | Layout<TrieVertex, Edge> layout = new TreeLayout<TrieVertex, Edge>(
|
---|
74 | graph, 60);
|
---|
75 | // The BasicVisualizationServer<V,E> is parameterized by the edge
|
---|
76 | // types
|
---|
77 | BasicVisualizationServer<TrieVertex, Edge> vv = new BasicVisualizationServer<TrieVertex, Edge>(
|
---|
78 | layout);
|
---|
79 | vv.setPreferredSize(new Dimension(1100, 850)); // Sets the viewing
|
---|
80 | // area size
|
---|
81 |
|
---|
82 | final Rectangle rect = new Rectangle(40, 20);
|
---|
83 |
|
---|
84 | Transformer<TrieVertex, Shape> vertexShapeTransformer = new Transformer<TrieVertex, Shape>() {
|
---|
85 | public Shape transform(TrieVertex s) {
|
---|
86 | return rect;
|
---|
87 | }
|
---|
88 | };
|
---|
89 | vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
|
---|
90 | vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
|
---|
91 | vv.getRenderContext().setVertexLabelTransformer(
|
---|
92 | new ToStringLabeller<TrieVertex>());
|
---|
93 |
|
---|
94 | JFrame frame = new JFrame("Trie");
|
---|
95 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
96 | frame.getContentPane().add(vv);
|
---|
97 | frame.pack();
|
---|
98 | frame.setVisible(true);
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|