1 | package de.ugoe.cs.eventbench.swt;
|
---|
2 |
|
---|
3 | import org.eclipse.swt.widgets.Dialog;
|
---|
4 | import org.eclipse.swt.widgets.Display;
|
---|
5 | import org.eclipse.swt.widgets.MessageBox;
|
---|
6 | import org.eclipse.swt.widgets.Shell;
|
---|
7 | import org.eclipse.swt.widgets.Button;
|
---|
8 | import org.eclipse.swt.SWT;
|
---|
9 | import org.eclipse.swt.widgets.Group;
|
---|
10 | import org.eclipse.swt.widgets.Label;
|
---|
11 | import org.eclipse.swt.widgets.Text;
|
---|
12 | import org.eclipse.swt.widgets.Spinner;
|
---|
13 | import org.eclipse.swt.layout.GridLayout;
|
---|
14 | import org.eclipse.swt.layout.GridData;
|
---|
15 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
16 | import org.eclipse.swt.events.SelectionEvent;
|
---|
17 |
|
---|
18 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
19 |
|
---|
20 | public class TrainModelDialog extends Dialog {
|
---|
21 |
|
---|
22 | protected Object result;
|
---|
23 | protected Shell shlTrainUsageModel;
|
---|
24 |
|
---|
25 | private Button btnFirstorderMarkovModel;
|
---|
26 | private Button btnHighorderMarkovModel;
|
---|
27 | private Button btnPredictionByPartial;
|
---|
28 | private Button btnDeterministicFiniteAutomaton;
|
---|
29 |
|
---|
30 | private Spinner minOrderSpinner;
|
---|
31 | private Spinner maxOrderSpinner;
|
---|
32 | private Text modelnameText;
|
---|
33 |
|
---|
34 | private String[] sequenceNames;
|
---|
35 | private Text probEscapeText;
|
---|
36 | private Label lblEscapeProbability;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Create the dialog.
|
---|
40 | * @param parent
|
---|
41 | * @param style
|
---|
42 | */
|
---|
43 | public TrainModelDialog(Shell parent, int style) {
|
---|
44 | super(parent, style);
|
---|
45 | setText("SWT Dialog");
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Open the dialog.
|
---|
50 | * @return the result
|
---|
51 | */
|
---|
52 | public Object open() {
|
---|
53 | createContents();
|
---|
54 | shlTrainUsageModel.open();
|
---|
55 | shlTrainUsageModel.layout();
|
---|
56 | Display display = getParent().getDisplay();
|
---|
57 | while (!shlTrainUsageModel.isDisposed()) {
|
---|
58 | if (!display.readAndDispatch()) {
|
---|
59 | display.sleep();
|
---|
60 | }
|
---|
61 | }
|
---|
62 | return result;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Create contents of the dialog.
|
---|
67 | */
|
---|
68 | private void createContents() {
|
---|
69 | shlTrainUsageModel = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MIN | SWT.APPLICATION_MODAL);
|
---|
70 | shlTrainUsageModel.setSize(219, 330);
|
---|
71 | shlTrainUsageModel.setText("Train Usage Model");
|
---|
72 | shlTrainUsageModel.setLayout(new GridLayout(2, false));
|
---|
73 |
|
---|
74 | Group grpGeneralInformation = new Group(shlTrainUsageModel, SWT.NONE);
|
---|
75 | grpGeneralInformation.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
|
---|
76 | grpGeneralInformation.setText("Name");
|
---|
77 | grpGeneralInformation.setLayout(new GridLayout(1, false));
|
---|
78 |
|
---|
79 | modelnameText = new Text(grpGeneralInformation, SWT.BORDER);
|
---|
80 | modelnameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
|
---|
81 |
|
---|
82 | Group grpType = new Group(shlTrainUsageModel, SWT.NONE);
|
---|
83 | grpType.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
|
---|
84 | grpType.setText("Type");
|
---|
85 | grpType.setLayout(new GridLayout(2, false));
|
---|
86 |
|
---|
87 | btnFirstorderMarkovModel = new Button(grpType, SWT.RADIO);
|
---|
88 | btnFirstorderMarkovModel.addSelectionListener(new SelectionAdapter() {
|
---|
89 | @Override
|
---|
90 | public void widgetSelected(SelectionEvent e) {
|
---|
91 | minOrderSpinner.setEnabled(false);
|
---|
92 | maxOrderSpinner.setEnabled(false);
|
---|
93 | probEscapeText.setEnabled(false);
|
---|
94 | }
|
---|
95 | });
|
---|
96 | btnFirstorderMarkovModel.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 2, 1));
|
---|
97 | btnFirstorderMarkovModel.setText("First-Order Markov Model");
|
---|
98 | btnFirstorderMarkovModel.setSelection(true);
|
---|
99 |
|
---|
100 | btnHighorderMarkovModel = new Button(grpType, SWT.RADIO);
|
---|
101 | btnHighorderMarkovModel.addSelectionListener(new SelectionAdapter() {
|
---|
102 | @Override
|
---|
103 | public void widgetSelected(SelectionEvent e) {
|
---|
104 | minOrderSpinner.setEnabled(false);
|
---|
105 | maxOrderSpinner.setEnabled(true);
|
---|
106 | probEscapeText.setEnabled(false);
|
---|
107 | }
|
---|
108 | });
|
---|
109 | btnHighorderMarkovModel.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 2, 1));
|
---|
110 | btnHighorderMarkovModel.setText("High-Order Markov Model");
|
---|
111 |
|
---|
112 | btnPredictionByPartial = new Button(grpType, SWT.RADIO);
|
---|
113 | btnPredictionByPartial.addSelectionListener(new SelectionAdapter() {
|
---|
114 | @Override
|
---|
115 | public void widgetSelected(SelectionEvent e) {
|
---|
116 | minOrderSpinner.setEnabled(true);
|
---|
117 | maxOrderSpinner.setEnabled(true);
|
---|
118 | probEscapeText.setEnabled(true);
|
---|
119 | }
|
---|
120 | });
|
---|
121 | btnPredictionByPartial.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, false, 2, 1));
|
---|
122 | btnPredictionByPartial.setText("Prediction by Partial Match");
|
---|
123 |
|
---|
124 | lblEscapeProbability = new Label(grpType, SWT.NONE);
|
---|
125 | lblEscapeProbability.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
|
---|
126 | lblEscapeProbability.setText("Escape Probability:");
|
---|
127 |
|
---|
128 | probEscapeText = new Text(grpType, SWT.BORDER);
|
---|
129 | probEscapeText.setText("0.1");
|
---|
130 | probEscapeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
---|
131 | probEscapeText.setEnabled(false);
|
---|
132 |
|
---|
133 | btnDeterministicFiniteAutomaton = new Button(grpType, SWT.RADIO);
|
---|
134 | btnDeterministicFiniteAutomaton.addSelectionListener(new SelectionAdapter() {
|
---|
135 | @Override
|
---|
136 | public void widgetSelected(SelectionEvent e) {
|
---|
137 | minOrderSpinner.setEnabled(false);
|
---|
138 | maxOrderSpinner.setEnabled(false);
|
---|
139 | probEscapeText.setEnabled(false);
|
---|
140 | }
|
---|
141 | });
|
---|
142 | btnDeterministicFiniteAutomaton.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 2, 1));
|
---|
143 | btnDeterministicFiniteAutomaton.setText("Deterministic Finite Automaton");
|
---|
144 |
|
---|
145 | Group grpModelProperties = new Group(shlTrainUsageModel, SWT.NONE);
|
---|
146 | grpModelProperties.setLayout(new GridLayout(4, false));
|
---|
147 | grpModelProperties.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 2, 1));
|
---|
148 | grpModelProperties.setText("Order");
|
---|
149 |
|
---|
150 | Label lblMin = new Label(grpModelProperties, SWT.NONE);
|
---|
151 | lblMin.setText("Min.");
|
---|
152 |
|
---|
153 | minOrderSpinner = new Spinner(grpModelProperties, SWT.BORDER);
|
---|
154 | minOrderSpinner.setEnabled(false);
|
---|
155 |
|
---|
156 | Label lblMax = new Label(grpModelProperties, SWT.NONE);
|
---|
157 | lblMax.setText("Max.");
|
---|
158 |
|
---|
159 | maxOrderSpinner = new Spinner(grpModelProperties, SWT.BORDER);
|
---|
160 | maxOrderSpinner.setEnabled(false);
|
---|
161 | maxOrderSpinner.setMinimum(2);
|
---|
162 |
|
---|
163 | Button btnTrain = new Button(shlTrainUsageModel, SWT.NONE);
|
---|
164 | btnTrain.addSelectionListener(new SelectionAdapter() {
|
---|
165 | @Override
|
---|
166 | public void widgetSelected(SelectionEvent e) {
|
---|
167 | String command = "";
|
---|
168 | String modelname = modelnameText.getText();
|
---|
169 | if( modelname.equals("")) {
|
---|
170 | MessageBox messageBox = new MessageBox(shlTrainUsageModel, SWT.ERROR);
|
---|
171 | messageBox.setText("Error");
|
---|
172 | messageBox.setMessage("No modelname defined!");
|
---|
173 | messageBox.open();
|
---|
174 | return;
|
---|
175 | }
|
---|
176 | if( btnFirstorderMarkovModel.getSelection() ) {
|
---|
177 | command = "trainMarkovModel " + modelname + " " + sequenceNames[0];
|
---|
178 |
|
---|
179 | }
|
---|
180 | else if( btnHighorderMarkovModel.getSelection() ) {
|
---|
181 | int modelOrder = maxOrderSpinner.getSelection();
|
---|
182 | command = "trainMarkovModel " + modelname + " " + sequenceNames[0] + " " + modelOrder;
|
---|
183 | }
|
---|
184 | else if( btnPredictionByPartial.getSelection() ) {
|
---|
185 | int minOrder = minOrderSpinner.getSelection();
|
---|
186 | int maxOrder = maxOrderSpinner.getSelection();
|
---|
187 | if( minOrder>maxOrder ) {
|
---|
188 | MessageBox messageBox = new MessageBox(shlTrainUsageModel, SWT.ERROR);
|
---|
189 | messageBox.setText("Error");
|
---|
190 | messageBox.setMessage("Min. Order must be smaller than or equal to max. order!");
|
---|
191 | messageBox.open();
|
---|
192 | return;
|
---|
193 | }
|
---|
194 | double probEscape = Double.parseDouble(probEscapeText.getText());
|
---|
195 | if( probEscape<0.0 || probEscape>1.0) {
|
---|
196 | MessageBox messageBox = new MessageBox(shlTrainUsageModel, SWT.ERROR);
|
---|
197 | messageBox.setText("Error");
|
---|
198 | messageBox.setMessage("Escape probability must be in [0,1]!");
|
---|
199 | messageBox.open();
|
---|
200 | return;
|
---|
201 | }
|
---|
202 | command = "trainPPM " + modelname + " " + sequenceNames[0] + " " + probEscape + " " + maxOrder + " " + minOrder;
|
---|
203 | }
|
---|
204 | else if( btnDeterministicFiniteAutomaton.getSelection() ) {
|
---|
205 | command = "trainDFA " + modelname + " " + sequenceNames[0];
|
---|
206 | }
|
---|
207 | CommandExecuter.getInstance().exec(command);
|
---|
208 | for( int i=1; i<sequenceNames.length; i++ ) {
|
---|
209 | command = "updateModel " + sequenceNames[i];
|
---|
210 | CommandExecuter.getInstance().exec(command);
|
---|
211 | }
|
---|
212 | shlTrainUsageModel.dispose();
|
---|
213 | }
|
---|
214 | });
|
---|
215 | btnTrain.setText("Train!");
|
---|
216 |
|
---|
217 | Button btnAbort = new Button(shlTrainUsageModel, SWT.NONE);
|
---|
218 | btnAbort.addSelectionListener(new SelectionAdapter() {
|
---|
219 | @Override
|
---|
220 | public void widgetSelected(SelectionEvent e) {
|
---|
221 | shlTrainUsageModel.dispose();
|
---|
222 | }
|
---|
223 | });
|
---|
224 | btnAbort.setText("Abort");
|
---|
225 |
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void setSequenceNames(String[] sequenceNames) {
|
---|
229 | this.sequenceNames = sequenceNames;
|
---|
230 | }
|
---|
231 | }
|
---|