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.CommandHelpers;
|
---|
14 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
15 | import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel;
|
---|
16 | import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel.MarkovEdge;
|
---|
17 | import de.ugoe.cs.util.console.Command;
|
---|
18 | import de.ugoe.cs.util.console.Console;
|
---|
19 | import edu.uci.ics.jung.algorithms.layout.Layout;
|
---|
20 | import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
|
---|
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 | import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * <p>
|
---|
28 | * Command that visualizes first-order Markov models.
|
---|
29 | * </p>
|
---|
30 | *
|
---|
31 | * @author Steffen Herbold
|
---|
32 | * @version 1.0
|
---|
33 | */
|
---|
34 | public class CMDshowMarkovModel implements Command {
|
---|
35 |
|
---|
36 | /*
|
---|
37 | * (non-Javadoc)
|
---|
38 | *
|
---|
39 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
40 | */
|
---|
41 | @Override
|
---|
42 | public void help() {
|
---|
43 | Console.println("Usage: showMarkovModel <modelName> {<showNodeNames>}");
|
---|
44 | }
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * (non-Javadoc)
|
---|
48 | *
|
---|
49 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
50 | */
|
---|
51 | @Override
|
---|
52 | public void run(List<Object> parameters) {
|
---|
53 | String modelname;
|
---|
54 | boolean showNodeNames = false;
|
---|
55 | try {
|
---|
56 | modelname = (String) parameters.get(0);
|
---|
57 | if (parameters.size() == 2) {
|
---|
58 | showNodeNames = Boolean
|
---|
59 | .parseBoolean((String) parameters.get(1));
|
---|
60 | }
|
---|
61 | } catch (Exception e) {
|
---|
62 | throw new InvalidParameterException();
|
---|
63 | }
|
---|
64 |
|
---|
65 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
66 | .getData(modelname);
|
---|
67 | if (dataObject == null) {
|
---|
68 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | if (!(dataObject instanceof FirstOrderMarkovModel)) {
|
---|
72 | CommandHelpers.objectNotType(modelname, "FirstOrderMarkovModel");
|
---|
73 | return;
|
---|
74 | }
|
---|
75 | FirstOrderMarkovModel mm = (FirstOrderMarkovModel) dataObject;
|
---|
76 |
|
---|
77 | Graph<String, MarkovEdge> graph = mm.getGraph();
|
---|
78 | Layout<String, MarkovEdge> layout = new ISOMLayout<String, MarkovEdge>(
|
---|
79 | graph);
|
---|
80 | layout.setSize(new Dimension(1000, 800)); // sets the initial size
|
---|
81 | // of the space
|
---|
82 | // The BasicVisualizationServer<V,E> is parameterized by the edge
|
---|
83 | // types
|
---|
84 | BasicVisualizationServer<String, MarkovEdge> vv = new BasicVisualizationServer<String, MarkovEdge>(
|
---|
85 | layout);
|
---|
86 | vv.setPreferredSize(new Dimension(1100, 850)); // Sets the viewing
|
---|
87 | // area size
|
---|
88 |
|
---|
89 | if (showNodeNames) {
|
---|
90 | final Rectangle rect = new Rectangle(240, 20);
|
---|
91 |
|
---|
92 | Transformer<String, Shape> vertexShapeTransformer = new Transformer<String, Shape>() {
|
---|
93 | public Shape transform(String s) {
|
---|
94 | return rect;
|
---|
95 | }
|
---|
96 | };
|
---|
97 | vv.getRenderer().getVertexLabelRenderer()
|
---|
98 | .setPosition(Position.CNTR);
|
---|
99 | vv.getRenderContext().setVertexShapeTransformer(
|
---|
100 | vertexShapeTransformer);
|
---|
101 | vv.getRenderContext().setVertexLabelTransformer(
|
---|
102 | new ToStringLabeller<String>());
|
---|
103 | }
|
---|
104 |
|
---|
105 | vv.getRenderContext().setEdgeLabelTransformer(
|
---|
106 | new ToStringLabeller<MarkovEdge>());
|
---|
107 |
|
---|
108 | JFrame frame = new JFrame("Markov Model");
|
---|
109 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
110 | frame.getContentPane().add(vv);
|
---|
111 | frame.pack();
|
---|
112 | frame.setVisible(true);
|
---|
113 | }
|
---|
114 | }
|
---|