aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/GUI.java
blob: cd4778f9dc959b437168d8f19d48140350fb653a (plain)
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
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.GUIConsole;
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)
 * 
 * 
 * @author Eduardo Pedroni
 *
 */
public class GUI extends Application {
	
	/* Colours */
	public static final String NEUTRAL_COLOUR = "#FFFFFF";
	public static final String HARD_HIGHLIGHT_COLOUR = "#5496FF";
	public static final String MEDIUM_HIGHLIGHT_COLOUR = "#75BAFF";
	public static final String SOFT_HIGHLIGHT_COLOUR = "#C7DFFF";
	
	public static final String GOOD_SELECTION_COLOUR = "#38C25B";
	public static final String NEUTRAL_SELECTION_COLOUR = "#FFEF73";
	public static final String BAD_SELECTION_COLOUR = "#FF5C5C";
	
	/* Sizes and distances */
    public static final double RESIZE_MARGIN = 5.0;
    public static final double SETTINGS_MIN_WIDTH = 200;
    public static final double CONSOLE_MIN_HEIGHT = 100;
    
	private final JCGP jcgp;
	
	private Stage stage;
	
	private final FunctionSelector functionSelector;
	
	private PopulationPane populationPane;
	
	private GUIConsole console;
	
	private SettingsPane settingsPane;
	
	private boolean running = false;
	
	private final Object printLock = new Object();
	
	private Service<Void> cgpService;
	
	private Runnable consoleFlush;
	
	public static void main(String[] args) {
		launch();
	}
	
	public GUI() {
		jcgp = new JCGP();
		functionSelector = new FunctionSelector(jcgp.getResources().getFunctionSet());
		
		consoleFlush = new Runnable() {
			@Override
			public void run() {
				synchronized(printLock) {
					console.flush();
					printLock.notifyAll();
				}
			}
		};
		
		cgpService = new Service<Void> () {
			@Override
			protected Task<Void> createTask() {
				Task<Void> t = new Task<Void>() {
					@Override
					protected Void call() throws Exception {
						while (!isCancelled() && !jcgp.isFinished()) {
							synchronized (printLock) {
								Platform.runLater(consoleFlush);
								jcgp.nextGeneration();
								printLock.wait();
							}
						}
						if (jcgp.isFinished()) {
							Platform.runLater(new Runnable() {
								@Override
								public void run() {
									runningMode(false);
								}
							});
						}
						return null;
					}
				};
				return t;
			}
		};
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		console = new GUIConsole();
		jcgp.setConsole(console);
		stage = primaryStage;
		/*
		 * Instantiate the various GUI elements here. 
		 * 
		 * 
		 */
		BorderPane leftFrame = new BorderPane();
		BorderPane experimentLayer = new BorderPane();
		
		populationPane = new PopulationPane(this);
		
		settingsPane = new SettingsPane(this);
		settingsPane.maxWidthProperty().bind(experimentLayer.widthProperty());
		
		console.maxHeightProperty().bind(experimentLayer.heightProperty());
		
		HorizontalDragResize.makeDragResizable(settingsPane);
		VerticalDragResize.makeDragResizable(console);
		
		leftFrame.setCenter(populationPane);
		leftFrame.setBottom(console);
		
		experimentLayer.setCenter(leftFrame);
		experimentLayer.setRight(settingsPane);
		
		primaryStage.setTitle("JCGP");
		
		Pane sceneParent = new Pane();
		experimentLayer.prefHeightProperty().bind(sceneParent.heightProperty());
		experimentLayer.prefWidthProperty().bind(sceneParent.widthProperty());
		sceneParent.getChildren().addAll(experimentLayer, functionSelector);
		
		primaryStage.setScene(new Scene(sceneParent));
		primaryStage.setMinWidth(800);
		primaryStage.setMinHeight(600);
		primaryStage.show();
		
		primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				settingsPane.getTestCaseTable().close();
			}
		});
	}

	public void runPause() {
		if (!jcgp.isFinished() && settingsPane.areParametersValid()) {
			if (!running) {
				runningMode(true);
				cgpService.restart();
			} else {
				cgpService.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();
	}
}