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
448
449
450
451
452
453
454
455
456
457
|
package jcgp.gui;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import jcgp.JCGP;
import jcgp.backend.modules.problem.TestCaseProblem.TestCase;
import jcgp.gui.console.ConsolePane;
import jcgp.gui.dragresize.HorizontalDragResize;
import jcgp.gui.dragresize.VerticalDragResize;
import jcgp.gui.population.FunctionSelector;
import jcgp.gui.population.GUINode;
import jcgp.gui.population.PopulationPane;
import jcgp.gui.settings.SettingsPane;
/**
* Main class for the graphical user interface (GUI).
* <br><br>
* This class declares the main method used when running the GUI.
* In addition, all main GUI panes are declared and instantiated here.
* <br><br>
* The user interface is divided into 3 main components: the node grid
* ({@link PopulationPane}), the control pane ({@link SettingsPane}) and
* the console ({@link ConsolePane}). Click on any of the links in
* brackets to see more information about each interface component.
* <br><br>
* This class also contains the instance of JCGP responsible for
* running the experiments in GUI mode. JCGP's execution must be delegated
* to a separate thread so that the GUI remains unblocked. This is done using
* a JavaFX {@code Service} which calls {@code nextGeneration()} in a loop
* until it is interrupted by the main JavaFX thread.
* <br>
* This service also handles flushing the console in a thread safe way. This
* is done by synchronizing the {@code nextGeneration()} and {@code flush()}
* method calls on a lock object.
*
* @author Eduardo Pedroni
*
*/
public class GUI extends Application {
/* Colours */
/**
* A string containing the hexadecimal colour used for representing neutrality.
*/
public static final String NEUTRAL_COLOUR = "#FFFFFF";
/**
* A string containing the hexadecimal colour used for representing a hard highlight.
* A "hard" select, for instance, happens when an output path is locked on the chromosome
* pane.
*/
public static final String HARD_HIGHLIGHT_COLOUR = "#5496FF";
/**
* A string containing the hexadecimal colour used for a medium highlight.
* One example of such a selection is the colour applied to a node
* when it is hovered over.
*/
public static final String MEDIUM_HIGHLIGHT_COLOUR = "#75BAFF";
/**
* A string containing the hexadecimal colour used for a soft highlight.
* When hovering over a node, its connections are soft-selected.
*/
public static final String SOFT_HIGHLIGHT_COLOUR = "#C7DFFF";
/**
* A string containing the hexadecimal colour used for representing a good selection.
* Ideally a shade of green, used for instance when a manual connection is valid.
*/
public static final String GOOD_SELECTION_COLOUR = "#38C25B";
/**
* A string containing the hexadecimal colour used for representing a neutral selection.
* Ideally a shade of yellow, used for instance when a manual connection is already the
* current connection.
*/
public static final String NEUTRAL_SELECTION_COLOUR = "#FFEF73";
/**
* A string containing the hexadecimal colour used for representing a bad selection.
* Ideally a shade of red, use for instance when a manual connection is not valid.
*/
public static final String BAD_SELECTION_COLOUR = "#FF5C5C";
/* Sizes and distances */
/**
* The width or height of the area that can be clicked on
* to drag-resize a pane.
*/
public static final double RESIZE_MARGIN = 5.0;
/**
* The minimum width of the settings pane, to prevent it
* from being resized beyond visibility.
*/
public static final double SETTINGS_MIN_WIDTH = 200;
/**
* The minimum width of the console pane, to prevent it
* from being resized beyond visibility.
*/
public static final double CONSOLE_MIN_HEIGHT = 100;
/*
* Actual GUI elements
*/
private Stage stage;
private PopulationPane populationPane;
private ConsolePane console;
private SettingsPane settingsPane;
private final FunctionSelector functionSelector;
/*
* Flow control objects
*/
private boolean running = false;
private final Object printLock = new Object();
private Service<Void> jcgpService;
private Runnable consoleFlush;
/*
* The experiment itself
*/
private final JCGP jcgp;
/**
* Start JCGP with the user interface.
*
* @param args no arguments are used.
*/
public static void main(String[] args) {
// not much to do, simply launch the JavaFX application
launch();
}
/**
* Makes a new instance of GUI. This initialises the JCGP experiment and
* instantiates the function selector. It also creates the console flush task
* and the service responsible for running the JCGP experiment.
*/
public GUI() {
jcgp = new JCGP();
functionSelector = new FunctionSelector(jcgp.getResources().getFunctionSet());
/*
* This task flushes the console in a thread-safe way.
* The problem is that this task is executed using Platform.runLater()
* to ensure that the flush itself happens on the JavaFX thread. However,
* runLater() is not guaranteed to run anytime soon. If the time taken for
* jcgp to perform a single generation is shorter than the time taken for
* this task to be executed by the platform, consoleFlush tasks will be
* scheduled faster than they can be executed and the console will eventually
* freeze.
*
* This is addressed by synchronizing the flushes with each nextGeneration() call.
*/
consoleFlush = new Runnable() {
@Override
public void run() {
/*
* Try to acquire printlock - wait here until jcgpService relinquishes it
* by calling wait(). This means that it is finished with the current generation
* and will wait for the console to be flushed to move on.
* It might be the case that the service has already released the lock by waiting
* on it; it makes no difference. In that case this will acquire the lock
* immediately and proceed to flush the console.
*/
synchronized(printLock) {
/*
* The lock is acquired, at this point we are certain that jcgpService
* cannot execute; it is currently waiting to be notified about the lock.
* No additional consoleFlush tasks can be scheduled with runLater() because
* the service is waiting. We can now take our time to flush the console.
*/
console.flush();
/*
* Once the console finishes flushing, we notify jcgpService to perform the
* next generation.
*/
printLock.notifyAll();
}
}
};
/*
* This service runs on a separate thread and performs
* the experiment, including console prints, in a thread-safe
* way. It is synchronized with consoleFlush.
*/
jcgpService = new Service<Void> () {
@Override
protected Task<Void> createTask() {
Task<Void> t = new Task<Void>() {
@Override
protected Void call() throws Exception {
/*
* Only execute if the experiment isn't finished
* and the service hasn't been cancelled.
*/
while (!isCancelled() && !jcgp.isFinished()) {
/*
* Attempt to acquire the printlock.
* Successfully doing so means no printing
* is currently taking place and we are free
* to schedule a print task.
* This lock acquisition should never block. It should
* not be possible to execute this statement without
* having been notified by consoleFlush.
*/
synchronized (printLock) {
/*
* Lock has been acquired, schedule a print
* task ahead of time. The actual print messages
* haven't been send to the console yet, that happens
* during nextGeneration(), but since we have the lock
* consoleFlush() will sit and wait for us to release it
* whenever we are finished queueing prints.
*/
Platform.runLater(consoleFlush);
/*
* Perform the actual generation. Here zero or more
* strings might be sent to the console buffer.
*/
jcgp.nextGeneration();
/*
* The generation is complete, relinquish the lock.
* By this point chances are the platform is already trying
* to execute the consoleFlush task that we scheduled. If it
* hasn't already started though, it doesn't matter; we will
* wait for a notification on the lock, which will only come
* when printing is complete.
*/
printLock.wait();
/*
* We have been notified. This means all buffered messages have
* been successfully flushed to the actual console control and
* we are now ready to perform another generation (or break out
* of the loop if the loop conditions are no longer met).
*/
}
/*
* We no longer own the lock, but neither does consoleFlush.
* The synchrony cycle has returned to its initial state, and we
* are free to acquire the lock again.
*/
}
/*
* Something happened to break the while loop -
* either the experiment finished or the user pressed
* pause.
*/
if (jcgp.isFinished()) {
// the experiment has finished, switch to pause mode
Platform.runLater(new Runnable() {
@Override
public void run() {
runningMode(false);
}
});
}
return null;
}
};
return t;
}
};
}
@Override
public void start(Stage primaryStage) throws Exception {
/*
* This method gets called when the application launches. Once it
* returns, the application falls into the main loop which handles
* events, so all elements must be constructed here.
*/
// make the console and set it so it is used for JCGP prints
console = new ConsolePane();
jcgp.setConsole(console);
// store reference to the stage
stage = primaryStage;
/*
* The experiment layer contains all of the experiment-related panes.
* The only element that sits higher than this is the function selector.
*/
BorderPane experimentLayer = new BorderPane();
/*
* The left frame encapsulates the population pane and the console.
* It goes into the center position of the experiment layer, next to the settings pane.
*/
BorderPane leftFrame = new BorderPane();
/*
* The population pane is a TabPane containing a tab for each chromosome.
*/
populationPane = new PopulationPane(this);
/*
* The settings pane is a big class containing the entire control pane
*/
settingsPane = new SettingsPane(this);
// make control pane and console resizable
HorizontalDragResize.makeDragResizable(settingsPane);
VerticalDragResize.makeDragResizable(console);
// prevent resizables from growing larger than the experiment layer
settingsPane.maxWidthProperty().bind(experimentLayer.widthProperty());
console.maxHeightProperty().bind(experimentLayer.heightProperty());
// put console and population pane in the main frame
leftFrame.setCenter(populationPane);
leftFrame.setBottom(console);
// set the main frame and the control pane in the experiment layer
experimentLayer.setCenter(leftFrame);
experimentLayer.setRight(settingsPane);
/*
* Now we deal with the stage.
*/
primaryStage.setTitle("JCGP");
// this pane holds the entire scene, that is its sole job.
Pane sceneParent = new Pane();
// the experiment layer should fill the entire scene parent
experimentLayer.prefHeightProperty().bind(sceneParent.heightProperty());
experimentLayer.prefWidthProperty().bind(sceneParent.widthProperty());
// the function selector goes over the experiment layer so it doesn't get covered by other panes
sceneParent.getChildren().addAll(experimentLayer, functionSelector);
// set the scene, minimum sizes, show
primaryStage.setScene(new Scene(sceneParent));
primaryStage.setMinWidth(800);
primaryStage.setMinHeight(600);
primaryStage.show();
// when the main stage closes, close the test case table as well
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
if (settingsPane.getTestCaseTable() != null) {
settingsPane.getTestCaseTable().close();
}
}
});
}
/**
*
*/
public void runPause() {
if (!jcgp.isFinished() && settingsPane.areParametersValid()) {
if (!running) {
runningMode(true);
jcgpService.restart();
} else {
jcgpService.cancel();
runningMode(false);
}
}
}
public void step() {
if (!running && !jcgp.isFinished() && settingsPane.areParametersValid()) {
if (settingsPane.isResetRequired()) {
reset();
}
populationPane.unlockOutputs();
jcgp.nextGeneration();
console.flush();
populationPane.updateGenes();
populationPane.relockOutputs();
settingsPane.revalidateParameters();
settingsPane.updateControls(false, jcgp.isFinished());
}
}
public void reset() {
if (!running && settingsPane.areParametersValid()) {
setEvaluating(false);
jcgp.reset();
settingsPane.applyParameters();
reDraw();
}
}
public void reDraw() {
populationPane.remakeTabs();
settingsPane.revalidateParameters();
settingsPane.updateControls(false, jcgp.isFinished());
console.flush();
}
private void runningMode(boolean value) {
if (value) {
populationPane.unlockOutputs();
if (settingsPane.isResetRequired()) {
reset();
}
} else {
populationPane.updateGenes();
populationPane.relockOutputs();
settingsPane.revalidateParameters();
}
populationPane.setDisable(value);
settingsPane.updateControls(value, jcgp.isFinished());
running = value;
}
public void updateFunctionSelector() {
functionSelector.remakeFunctions(jcgp.getResources().getFunctionSet());
}
public boolean isWorking() {
return running;
}
public void bringFunctionSelector(MouseEvent event, GUINode node) {
functionSelector.relocateAndShow(event, node);
}
public JCGP getExperiment() {
return jcgp;
}
public void evaluateTestCase(TestCase<Object> testCase) {
populationPane.evaluateTestCase(testCase);
}
public void hideGeneValues() {
populationPane.hideValues();
}
public void setEvaluating(boolean value) {
populationPane.setEvaluating(value);
}
public Stage getStage() {
return stage;
}
public void flushConsole() {
console.flush();
}
public int getChromosomeIndex() {
return populationPane.getSelectionModel().getSelectedIndex();
}
}
|