Download this file

EmulatorDialog.java    448 lines (391 with data), 17.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package emulation;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jface.dialogs.DialogPage;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import classification.ClassifierMeta;
import classification.ClassifierTypeMeta;
import edu.teco.tacet.track.Range;
public class EmulatorDialog extends TitleAreaDialog {
private static ClassifierTypeMeta[] numericClassifiers = {new ClassifierTypeMeta("Functions", new ClassifierMeta[]{new ClassifierMeta("Gaussian Processes", "Functions", false, true), new ClassifierMeta("Linear Regression", "Functions", false, true), new ClassifierMeta("Multilayer Perceptron", "Functions", true, true), new ClassifierMeta("SMOreg", "Functions", false, true)}),
new ClassifierTypeMeta("Lazy", new ClassifierMeta[]{new ClassifierMeta("IBk", "Lazy", true, true), new ClassifierMeta("KStar", "Lazy", true, true), new ClassifierMeta("LWL", "Lazy", true, true)}),
new ClassifierTypeMeta("Meta", new ClassifierMeta[]{new ClassifierMeta("Additive Regression", "Meta", false, true), new ClassifierMeta("Attribute Selected Classifier", "Meta", true, true), new ClassifierMeta("Bagging", "Meta", true, true), new ClassifierMeta("CV Parameter Selection", "Meta", true, true), /*new ClassifierMeta("FilteredClassifier", "Meta", true, true),*/ new ClassifierMeta("Multi Scheme", "Meta", true, true), /*new ClassifierMeta("Random Committee", "Meta", true, true),*/ new ClassifierMeta("Random Subspace", "Meta", true, true), new ClassifierMeta("Regression by Discretization", "Meta", false, true), new ClassifierMeta("Stacking", "Meta", true, true), new ClassifierMeta("Vote", "Meta", true, true)}),
new ClassifierTypeMeta("Misc", new ClassifierMeta[]{new ClassifierMeta("Input Mapped Classifier", "Misc", true, true), /*new ClassifierMeta("Serialized Classifier", "Misc", true, true)*/}),
new ClassifierTypeMeta("Rules", new ClassifierMeta[]{new ClassifierMeta("Decision Table", "Rules", true, true), new ClassifierMeta("M5 Rules", "Rules", false, true), new ClassifierMeta("ZeroR", "Rules", true, true)}),
new ClassifierTypeMeta("Trees", new ClassifierMeta[]{new ClassifierMeta("Decision Stump", "Trees", true, true), new ClassifierMeta("M5P", "Trees", false, true), new ClassifierMeta("REP Tree", "Trees", true, true)})};
private Font font;
private String selectedClassifier = "";
private List<String> annotationTracks;
private List<String> sensorTracks;
private List<Range> coveredRanges;
private Combo classSelectCombo;
private static Tree classifiersTree;
private int classIdx;
private long startTime;
private long endTime;
private Range currentCoveredRange;
private CheckboxTableViewer annotationTableViewer;
private CheckboxTableViewer sensorTableViewer;
private List<Integer> annotationIdxs;
private List<Integer> sensorIdxs;
private String classifierType;
private String classifierName;
private Text fromTimestamp;
private Text toTimestamp;
private Label coveredRangeLbl;
public EmulatorDialog(Shell parentShell, List<String> sensorTracks, List<String> annotationTracks, List<Range> coveredRanges) {
super(parentShell);
this.sensorTracks = sensorTracks;
this.annotationTracks = annotationTracks;
this.coveredRanges = coveredRanges;
annotationIdxs = new LinkedList<>();
sensorIdxs = new LinkedList<>();
this.setHelpAvailable(false);
}
@Override
public void create() {
super.create();
setTitle("Emulator Settings");
setMessage(
"The emulator emulates sensor data in a selected sensor track based on available sensor data and annotations.",
IMessageProvider.INFORMATION);
}
@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
GridLayout layout = new GridLayout(2, false);
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setLayout(layout);
FontData fontData = parent.getFont().getFontData()[0];
font = new Font(parent.getDisplay(), new FontData(fontData.getName(), fontData
.getHeight(), SWT.BOLD));
createClassTrackSelection(container);
createAttributeTrackSelection(container);
createClassifierSelection(container);
return area;
}
private void createClassifierSelection(Composite container) {
Group selectClassifierGrp = new Group(container, SWT.NONE);
selectClassifierGrp.setFont(font);
selectClassifierGrp.setText("Select Classifier");
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
selectClassifierGrp.setLayoutData(gridData);
selectClassifierGrp.setLayout(new GridLayout(2, false));
Label selectedClassifierLbl = new Label(selectClassifierGrp, SWT.NONE);
selectedClassifierLbl.setText("Selected Classifier:");
final Text selectedClassifierTxt = new Text(selectClassifierGrp, SWT.NONE | SWT.READ_ONLY | SWT.BORDER);
selectedClassifierTxt.setText("");
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
selectedClassifierTxt.setLayoutData(gridData);
classifiersTree = new Tree(selectClassifierGrp, SWT.V_SCROLL | SWT.BORDER);
initClassifiers();
gridData = new GridData();
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
classifiersTree.setLayoutData(gridData);
classifiersTree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
selectedClassifier = "";
TreeItem[] selection = classifiersTree.getSelection();
if (selection[0].getParentItem() instanceof TreeItem) {
selectedClassifier = selection[0].getParentItem().getText()
+ " - " + selection[0].getText();
selectedClassifierTxt.setText(selectedClassifier);
classifierType = selection[0].getParentItem().getText().toLowerCase();
classifierName = selection[0].getText().replace(" ", "");
} else {
classifiersTree.deselectAll();
selectedClassifierTxt.setText("");
}
}
});
}
private void initClassifiers() {
for (int i = 0; i < numericClassifiers.length; i++) {
TreeItem item = new TreeItem(classifiersTree, SWT.NONE);
item.setText(numericClassifiers[i].getName());
for (ClassifierMeta c : numericClassifiers[i].getClassifiers()) {
TreeItem child = new TreeItem(item, SWT.NONE);
child.setText(c.getName());
}
}
TreeItem item = new TreeItem(classifiersTree, SWT.NONE);
item.setText("Openforecast");
TreeItem child = new TreeItem(item, SWT.NONE);
child.setText("Multilinear Regression");
child = new TreeItem(item, SWT.NONE);
child.setText("Best fit");
}
private void createClassTrackSelection(Composite container) {
Group selectClassTrackGrp = new Group(container, SWT.NONE);
selectClassTrackGrp.setFont(font);
selectClassTrackGrp.setText("Select Class Track");
GridData gridData = new GridData();
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
selectClassTrackGrp.setLayoutData(gridData);
selectClassTrackGrp.setLayout(new GridLayout(4, false));
classSelectCombo = new Combo(selectClassTrackGrp, SWT.READ_ONLY);
gridData = new GridData();
gridData.horizontalSpan = 4;
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
classSelectCombo.setLayoutData(gridData);
for(String t : sensorTracks) {
classSelectCombo.add(t);
}
Label from = new Label(selectClassTrackGrp, SWT.NONE);
from.setText("From:");
fromTimestamp = new Text(selectClassTrackGrp, SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
Label to = new Label(selectClassTrackGrp, SWT.NONE);
to.setText("To:");
toTimestamp = new Text(selectClassTrackGrp, SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
coveredRangeLbl = new Label(selectClassTrackGrp, SWT.NONE);
coveredRangeLbl.setText("Classtrack range:");
gridData = new GridData();
gridData.horizontalSpan = 4;
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
coveredRangeLbl.setLayoutData(gridData);
classSelectCombo.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int i = classSelectCombo.getSelectionIndex();
coveredRangeLbl.setText("Classtrack range: " + convertRangeToString(coveredRanges.get(i)));
currentCoveredRange = coveredRanges.get(i);
fromTimestamp.setText(convertTimestampToString(currentCoveredRange.getStart()));
toTimestamp.setText(convertTimestampToString(currentCoveredRange.getEnd()));
}
});
}
private void createAttributeTrackSelection(Composite container) {
Group selectTrackGrp = new Group(container, SWT.NONE);
selectTrackGrp.setFont(font);
selectTrackGrp.setText("Select Attribute Tracks");
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
selectTrackGrp.setLayoutData(gridData);
selectTrackGrp.setLayout(new GridLayout(3, false));
Label annotationTracksLbl = new Label(selectTrackGrp, SWT.NONE);
annotationTracksLbl.setText("Annotation Tracks:");
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
annotationTracksLbl.setLayoutData(gridData);
Button allAnnotationsBtn = new Button(selectTrackGrp, SWT.NONE);
allAnnotationsBtn.setText("All");
Button noneAnnotationsBtn = new Button(selectTrackGrp, SWT.NONE);
noneAnnotationsBtn.setText("None");
annotationTableViewer = CheckboxTableViewer.newCheckList(selectTrackGrp, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.BORDER);
Table annotationTable = annotationTableViewer.getTable();
annotationTable.setLinesVisible(false);
gridData = new GridData();
gridData.horizontalSpan = 3;
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
annotationTable.setLayoutData(gridData);
annotationTableViewer.setContentProvider(new ArrayContentProvider());
annotationTableViewer.setInput(annotationTracks);
allAnnotationsBtn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
annotationTableViewer.setAllChecked(true);
}
});
noneAnnotationsBtn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
annotationTableViewer.setAllChecked(false);
}
});
Label sensorTracksLbl = new Label(selectTrackGrp, SWT.NONE);
sensorTracksLbl.setText("Sensor Tracks:");
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
sensorTracksLbl.setLayoutData(gridData);
Button allSensorsBtn = new Button(selectTrackGrp, SWT.NONE);
allSensorsBtn.setText("All");
Button noneSensorsBtn = new Button(selectTrackGrp, SWT.NONE);
noneSensorsBtn.setText("None");
sensorTableViewer = CheckboxTableViewer.newCheckList(selectTrackGrp, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.BORDER);
Table sensorTable = sensorTableViewer.getTable();
gridData = new GridData();
gridData.horizontalSpan = 3;
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
sensorTable.setLayoutData(gridData);
sensorTableViewer.setContentProvider(new ArrayContentProvider());
sensorTableViewer.setInput(sensorTracks);
allSensorsBtn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
sensorTableViewer.setAllChecked(true);
}
});
noneSensorsBtn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
sensorTableViewer.setAllChecked(false);
}
});
}
@Override
protected boolean isResizable() {
return true;
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
Button ok = getButton(IDialogConstants.OK_ID);
ok.setText("Emulate");
setButtonLayoutData(ok);
Button cancel = getButton(IDialogConstants.CANCEL_ID);
cancel.setText("Cancel");
setButtonLayoutData(cancel);
}
@Override
protected void okPressed() {
for (int i = 0; i < annotationTableViewer.getTable().getItemCount(); i++) {
if (annotationTableViewer.getChecked(annotationTableViewer
.getElementAt(i)))
annotationIdxs.add(i);
}
for (int i = 0; i < sensorTableViewer.getTable().getItemCount(); i++) {
if (sensorTableViewer.getChecked(sensorTableViewer
.getElementAt(i)))
sensorIdxs.add(i);
}
if (validate()) {
classIdx = classSelectCombo.getSelectionIndex();
startTime = convertStringToTimestamp(fromTimestamp.getText());
endTime = convertStringToTimestamp(toTimestamp.getText());
super.okPressed();
} else {
annotationIdxs = new LinkedList<>();
sensorIdxs = new LinkedList<>();
}
}
private boolean validate() {
if (classSelectCombo.getSelectionIndex() == -1) {
this.setMessage("Please select a class track!", DialogPage.WARNING);
return false;
}
if (selectedClassifier.length() == 0) {
this.setMessage("Please select a classifier!", DialogPage.WARNING);
return false;
}
if (annotationIdxs.size() == 0 && sensorIdxs.size() == 0) {
this.setMessage("Please select at least one attribute Track!", DialogPage.WARNING);
return false;
}
if(fromTimestamp.getText().equals("")) {
this.setMessage("Please specify a start timestamp for the emulation training!", DialogPage.WARNING);
return false;
}
if(toTimestamp.getText().equals("")) {
this.setMessage("Please specify an end timestamp for the emulation training!", DialogPage.WARNING);
return false;
}
if(!currentCoveredRange.contains(convertStringToTimestamp(fromTimestamp.getText()))) {
this.setMessage("The covered range must contain the range for emulation training!", DialogPage.WARNING);
return false;
}
if(!currentCoveredRange.contains(convertStringToTimestamp(toTimestamp.getText()))) {
this.setMessage("The covered range must contain the range for emulation training!", DialogPage.WARNING);
return false;
}
return true;
}
public int getClassIdx() {
return classIdx;
}
public String getSelectedClassifier() {
if(classifierName.equals("MultilinearRegression"))
return classifierName;
if(classifierName.equals("Bestfit"))
return classifierName;
return "weka.classifiers." + classifierType + "." + classifierName;
}
public List<Integer> getAnnotationIdxs() {
return annotationIdxs;
}
public List<Integer> getSensorIdxs() {
return sensorIdxs;
}
public Range getRangeToEmulate() {
return new Range(startTime, endTime);
}
private long convertStringToTimestamp(String s) {
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.S");
DateTime dt = fmt.parseDateTime(s);
return dt.getMillis();
}
private String convertTimestampToString(long timestamp) {
DateTime dt = new DateTime(timestamp);
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.S");
return dt.toString(fmt);
}
private String convertRangeToString(Range range) {
String coveredRange = "from ";
coveredRange += convertTimestampToString(range.getStart());
coveredRange += " to ";
coveredRange += convertTimestampToString(range.getEnd());
return coveredRange;
}
}