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