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 | public class CMDshowMarkovModel implements Command {
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public void help() {
|
---|
29 | Console.println("Usage: showMarkovModel <modelName> {<showNodeNames>}");
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public void run(List<Object> parameters) {
|
---|
34 | String modelname;
|
---|
35 | boolean showNodeNames = false;
|
---|
36 | try {
|
---|
37 | modelname = (String) parameters.get(0);
|
---|
38 | if( parameters.size()==2 ) {
|
---|
39 | showNodeNames = Boolean.parseBoolean((String) parameters.get(1));
|
---|
40 | }
|
---|
41 | } catch (Exception e) {
|
---|
42 | throw new InvalidParameterException();
|
---|
43 | }
|
---|
44 |
|
---|
45 | Object dataObject = GlobalDataContainer.getInstance().getData(modelname);
|
---|
46 | if( dataObject==null ) {
|
---|
47 | Console.printerrln("No model with name " + modelname + "found");
|
---|
48 | } else {
|
---|
49 | FirstOrderMarkovModel mm = (FirstOrderMarkovModel) dataObject;
|
---|
50 |
|
---|
51 | Graph<String, MarkovEdge> graph = mm.getGraph();
|
---|
52 | Layout<String, MarkovEdge> layout = new ISOMLayout<String, MarkovEdge>(graph);
|
---|
53 | layout.setSize(new Dimension(1000,800)); // sets the initial size of the space
|
---|
54 | // The BasicVisualizationServer<V,E> is parameterized by the edge types
|
---|
55 | BasicVisualizationServer<String,MarkovEdge> vv =
|
---|
56 | new BasicVisualizationServer<String,MarkovEdge>(layout);
|
---|
57 | vv.setPreferredSize(new Dimension(1100,850)); //Sets the viewing area size
|
---|
58 |
|
---|
59 | if( showNodeNames ) {
|
---|
60 | final Rectangle rect = new Rectangle(240, 20);
|
---|
61 |
|
---|
62 | Transformer<String, Shape> vertexShapeTransformer =
|
---|
63 | new Transformer<String, Shape>() {
|
---|
64 | public Shape transform(String s) {
|
---|
65 | return rect;
|
---|
66 | }
|
---|
67 | };
|
---|
68 | vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
|
---|
69 | vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
|
---|
70 | vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
|
---|
71 | }
|
---|
72 |
|
---|
73 | vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<MarkovEdge>());
|
---|
74 |
|
---|
75 | JFrame frame = new JFrame("Markov Model");
|
---|
76 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
77 | frame.getContentPane().add(vv);
|
---|
78 | frame.pack();
|
---|
79 | frame.setVisible(true);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|