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