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