[24] | 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 |
|
---|
[240] | 13 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
[24] | 14 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
[171] | 15 | import de.ugoe.cs.eventbench.models.Trie;
|
---|
[24] | 16 | import de.ugoe.cs.eventbench.models.Trie.Edge;
|
---|
| 17 | import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
|
---|
| 18 | import de.ugoe.cs.eventbench.models.TrieBasedModel;
|
---|
| 19 | import de.ugoe.cs.util.console.Command;
|
---|
| 20 | import de.ugoe.cs.util.console.Console;
|
---|
| 21 | import edu.uci.ics.jung.algorithms.layout.Layout;
|
---|
| 22 | import edu.uci.ics.jung.algorithms.layout.TreeLayout;
|
---|
| 23 | import edu.uci.ics.jung.graph.Tree;
|
---|
| 24 | import edu.uci.ics.jung.visualization.BasicVisualizationServer;
|
---|
| 25 | import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
|
---|
| 26 | import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
|
---|
| 27 |
|
---|
[171] | 28 | /**
|
---|
| 29 | * <p>
|
---|
| 30 | * Command that visualizes the {@link Trie} of a {@link TrieBasedModel}.
|
---|
| 31 | * </p>
|
---|
| 32 | *
|
---|
| 33 | * @author Steffen Herbold
|
---|
| 34 | * @version 1.0
|
---|
| 35 | */
|
---|
[24] | 36 | public class CMDshowTrie implements Command {
|
---|
| 37 |
|
---|
[171] | 38 | /*
|
---|
| 39 | * (non-Javadoc)
|
---|
| 40 | *
|
---|
| 41 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 42 | */
|
---|
[24] | 43 | @Override
|
---|
| 44 | public void help() {
|
---|
| 45 | Console.println("Usage: showTrie <modelName>");
|
---|
| 46 | }
|
---|
[171] | 47 |
|
---|
| 48 | /*
|
---|
| 49 | * (non-Javadoc)
|
---|
| 50 | *
|
---|
| 51 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 52 | */
|
---|
[24] | 53 | @Override
|
---|
| 54 | public void run(List<Object> parameters) {
|
---|
| 55 | String modelname;
|
---|
| 56 | try {
|
---|
| 57 | modelname = (String) parameters.get(0);
|
---|
| 58 | } catch (Exception e) {
|
---|
| 59 | throw new InvalidParameterException();
|
---|
| 60 | }
|
---|
[171] | 61 |
|
---|
| 62 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
| 63 | .getData(modelname);
|
---|
| 64 | if (dataObject == null) {
|
---|
[240] | 65 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
[209] | 66 | return;
|
---|
| 67 | }
|
---|
| 68 | if (!(dataObject instanceof TrieBasedModel)) {
|
---|
[240] | 69 | CommandHelpers.objectNotType(modelname, "TrieBasedModel");
|
---|
[209] | 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
|
---|
[171] | 81 |
|
---|
[209] | 82 | final Rectangle rect = new Rectangle(40, 20);
|
---|
[171] | 83 |
|
---|
[209] | 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>());
|
---|
[171] | 93 |
|
---|
[209] | 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);
|
---|
[24] | 99 | }
|
---|
| 100 |
|
---|
| 101 | }
|
---|