[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.commands.usage;
|
---|
[24] | 16 |
|
---|
| 17 | import java.awt.Dimension;
|
---|
| 18 | import java.awt.Rectangle;
|
---|
| 19 | import java.awt.Shape;
|
---|
| 20 | import java.util.List;
|
---|
| 21 |
|
---|
| 22 | import javax.swing.JFrame;
|
---|
| 23 |
|
---|
| 24 | import org.apache.commons.collections15.Transformer;
|
---|
| 25 |
|
---|
[922] | 26 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
| 27 | import de.ugoe.cs.autoquest.usageprofiles.Trie;
|
---|
| 28 | import de.ugoe.cs.autoquest.usageprofiles.TrieBasedModel;
|
---|
| 29 | import de.ugoe.cs.autoquest.usageprofiles.Trie.Edge;
|
---|
| 30 | import de.ugoe.cs.autoquest.usageprofiles.Trie.TrieVertex;
|
---|
[24] | 31 | import de.ugoe.cs.util.console.Command;
|
---|
[667] | 32 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
[24] | 33 | import edu.uci.ics.jung.algorithms.layout.Layout;
|
---|
| 34 | import edu.uci.ics.jung.algorithms.layout.TreeLayout;
|
---|
| 35 | import edu.uci.ics.jung.graph.Tree;
|
---|
| 36 | import edu.uci.ics.jung.visualization.BasicVisualizationServer;
|
---|
| 37 | import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
|
---|
| 38 | import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
|
---|
| 39 |
|
---|
[171] | 40 | /**
|
---|
| 41 | * <p>
|
---|
| 42 | * Command that visualizes the {@link Trie} of a {@link TrieBasedModel}.
|
---|
| 43 | * </p>
|
---|
| 44 | *
|
---|
| 45 | * @author Steffen Herbold
|
---|
| 46 | * @version 1.0
|
---|
| 47 | */
|
---|
[24] | 48 | public class CMDshowTrie implements Command {
|
---|
| 49 |
|
---|
[171] | 50 | /*
|
---|
| 51 | * (non-Javadoc)
|
---|
| 52 | *
|
---|
| 53 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 54 | */
|
---|
[24] | 55 | @Override
|
---|
[664] | 56 | public String help() {
|
---|
[726] | 57 | return "showTrie <modelname>";
|
---|
[24] | 58 | }
|
---|
[171] | 59 |
|
---|
| 60 | /*
|
---|
| 61 | * (non-Javadoc)
|
---|
| 62 | *
|
---|
| 63 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 64 | */
|
---|
[24] | 65 | @Override
|
---|
| 66 | public void run(List<Object> parameters) {
|
---|
| 67 | String modelname;
|
---|
| 68 | try {
|
---|
| 69 | modelname = (String) parameters.get(0);
|
---|
| 70 | } catch (Exception e) {
|
---|
[766] | 71 | throw new IllegalArgumentException();
|
---|
[24] | 72 | }
|
---|
[171] | 73 |
|
---|
| 74 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
| 75 | .getData(modelname);
|
---|
| 76 | if (dataObject == null) {
|
---|
[240] | 77 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
[209] | 78 | return;
|
---|
| 79 | }
|
---|
| 80 | if (!(dataObject instanceof TrieBasedModel)) {
|
---|
[240] | 81 | CommandHelpers.objectNotType(modelname, "TrieBasedModel");
|
---|
[209] | 82 | }
|
---|
| 83 | TrieBasedModel model = (TrieBasedModel) dataObject;
|
---|
| 84 | Tree<TrieVertex, Edge> graph = model.getTrieGraph();
|
---|
| 85 | Layout<TrieVertex, Edge> layout = new TreeLayout<TrieVertex, Edge>(
|
---|
| 86 | graph, 60);
|
---|
| 87 | // The BasicVisualizationServer<V,E> is parameterized by the edge
|
---|
| 88 | // types
|
---|
| 89 | BasicVisualizationServer<TrieVertex, Edge> vv = new BasicVisualizationServer<TrieVertex, Edge>(
|
---|
| 90 | layout);
|
---|
| 91 | vv.setPreferredSize(new Dimension(1100, 850)); // Sets the viewing
|
---|
| 92 | // area size
|
---|
[171] | 93 |
|
---|
[209] | 94 | final Rectangle rect = new Rectangle(40, 20);
|
---|
[171] | 95 |
|
---|
[209] | 96 | Transformer<TrieVertex, Shape> vertexShapeTransformer = new Transformer<TrieVertex, Shape>() {
|
---|
| 97 | public Shape transform(TrieVertex s) {
|
---|
| 98 | return rect;
|
---|
| 99 | }
|
---|
| 100 | };
|
---|
| 101 | vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
|
---|
| 102 | vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
|
---|
| 103 | vv.getRenderContext().setVertexLabelTransformer(
|
---|
| 104 | new ToStringLabeller<TrieVertex>());
|
---|
[171] | 105 |
|
---|
[209] | 106 | JFrame frame = new JFrame("Trie");
|
---|
| 107 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
| 108 | frame.getContentPane().add(vv);
|
---|
| 109 | frame.pack();
|
---|
| 110 | frame.setVisible(true);
|
---|
[24] | 111 | }
|
---|
| 112 |
|
---|
| 113 | }
|
---|