package jcgp.gui; import javafx.application.Application; import javafx.application.Platform; import javafx.concurrent.Service; import javafx.concurrent.Task; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; import jcgp.JCGP; import jcgp.backend.resources.Resources; import jcgp.gui.console.GUIConsole; import jcgp.gui.dragresize.HorizontalDragResize; import jcgp.gui.dragresize.VerticalDragResize; import jcgp.gui.population.FunctionSelector; import jcgp.gui.population.PopulationPane; import jcgp.gui.settings.SettingsPane; public class GUI extends Application { /* Colours */ /** NEUTRAL_COLOUR is the colour elements should be when no interaction is occurring * (no hovering, clicking, dragging or any other form of interaction). */ 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_WIDTH = 200; public static final double CONSOLE_HEIGHT = 100; public static final double WRAP_WIDTH = 90; public static final JCGP jcgp = new JCGP(); public static final Resources resources = jcgp.getResources(); public static final FunctionSelector functionSelector = new FunctionSelector(resources.getFunctionSet()); private PopulationPane populationPane; private GUIConsole console = new GUIConsole(); private SettingsPane settingsPane; private boolean evolving = false; private Object printLock = new Object(); private Service cgpService = new Service () { @Override protected Task createTask() { Task t = new Task() { @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; } }; private Runnable consoleFlush = new Runnable() { @Override public void run() { synchronized(printLock) { console.flush(); printLock.notifyAll(); } } }; public static void main(String[] args) { // jcgp = new JCGP(); // resources = jcgp.getResources(); // // functionSelector = launch(); } @Override public void start(Stage primaryStage) throws Exception { jcgp.setConsole(console); /* * Instantiate the various GUI elements here. * * */ BorderPane leftFrame = new BorderPane(); populationPane = new PopulationPane(jcgp); settingsPane = new SettingsPane(jcgp, this); HorizontalDragResize.makeDragResizable(settingsPane); VerticalDragResize.makeDragResizable(console); leftFrame.setCenter(populationPane); leftFrame.setBottom(console); BorderPane experimentLayer = new BorderPane(); 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(); } public void runPause() { if (!jcgp.isFinished() && settingsPane.areParametersValid()) { if (!evolving) { runningMode(true); cgpService.restart(); } else { cgpService.cancel(); runningMode(false); } } } public void step() { if (!evolving && !jcgp.isFinished() && settingsPane.areParametersValid()) { if (settingsPane.isResetRequired()) { reset(); } populationPane.unlockOutputs(); jcgp.nextGeneration(); console.flush(); populationPane.updateGenes(); populationPane.relockOutputs(); } } public void reset() { if (!evolving && settingsPane.areParametersValid()) { settingsPane.applyParameters(); jcgp.reset(); populationPane.remakeTabs(jcgp.getPopulation(), jcgp.getResources()); settingsPane.revalidateParameters(); settingsPane.updateControls(false, jcgp.isFinished()); console.flush(); } } private void runningMode(boolean value) { populationPane.setDisable(value); settingsPane.updateControls(value, jcgp.isFinished()); if (value) { populationPane.unlockOutputs(); if (settingsPane.isResetRequired()) { reset(); } } else { populationPane.updateGenes(); populationPane.relockOutputs(); } evolving = value; } public static void updateFunctionSelector() { functionSelector.remakeFunctions(resources.getFunctionSet()); } }