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.markov.MarkovModel;
|
---|
15 | import de.ugoe.cs.eventbench.markov.MarkovModel.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 | MarkovModel mm = (MarkovModel) 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 |
|
---|
60 | if( showNodeNames ) {
|
---|
61 | final Rectangle rect = new Rectangle(240, 20);
|
---|
62 |
|
---|
63 | Transformer<String, Shape> vertexShapeTransformer =
|
---|
64 | new Transformer<String, Shape>() {
|
---|
65 | public Shape transform(String s) {
|
---|
66 | return rect;
|
---|
67 | }
|
---|
68 | };
|
---|
69 | vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
|
---|
70 | vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
|
---|
71 | vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<MarkovModel.MarkovEdge>());
|
---|
76 |
|
---|
77 | JFrame frame = new JFrame("Markov Model");
|
---|
78 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
79 | frame.getContentPane().add(vv);
|
---|
80 | frame.pack();
|
---|
81 | frame.setVisible(true);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | }
|
---|