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