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.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.control.TabPane.TabClosingPolicy; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import jcgp.JCGP; import jcgp.JCGP.Resources; import jcgp.gui.console.GUIConsole; 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"; // 89AAD6 public static final String MEDIUM_HIGHLIGHT_COLOUR = "#75BAFF"; public static final String SOFT_HIGHLIGHT_COLOUR = "#C7DFFF"; // BDFFC2 public static final String GOOD_SELECTION_COLOUR = "#38C25B"; // FBFFB8 public static final String NEUTRAL_SELECTION_COLOUR = "#EDEB72"; // FF9C9C public static final String BAD_SELECTION_COLOUR = "#F53D3D"; /* 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; private static JCGP cgp; public static Resources resources; private BorderPane leftPane; private BorderPane window; private ChromosomePane[] chromosomes; private TabPane chromosomeTabs; private GUIConsole console = new GUIConsole(); private SettingsPane settings; private Service cgpService; private boolean evolving = false; private Object printLock = new Object(); private Runnable consoleFlush = new Runnable() { @Override public void run() { synchronized(printLock) { console.flush(); printLock.notifyAll(); } } }; public static void main(String[] args) { cgp = new JCGP(); resources = cgp.getResources(); launch(); } @Override public void start(Stage primaryStage) throws Exception { resources.setConsole(console); cgpService = new Service () { @Override protected Task createTask() { Task t = new Task() { @Override protected Void call() throws Exception { while (!isCancelled()) { Platform.runLater(consoleFlush); synchronized (printLock) { cgp.nextGeneration(); printLock.wait(); } } return null; } }; return t; } }; /* * Instantiate the various GUI elements here. * * */ leftPane = new BorderPane(); chromosomeTabs = new TabPane(); chromosomeTabs.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE); makeChromosomeTabPane(); settings = new SettingsPane(cgp, this); leftPane.setCenter(chromosomeTabs); leftPane.setBottom(console); window = new BorderPane(); window.setCenter(leftPane); window.setRight(settings); primaryStage.setTitle("JCGP"); primaryStage.setScene(new Scene(window)); primaryStage.show(); } /** * */ private void makeChromosomeTabPane() { chromosomeTabs.getTabs().clear(); chromosomes = new ChromosomePane[cgp.getResources().getInt("popSize")]; Tab tab; for (int i = 0; i < chromosomes.length; i++) { chromosomes[i] = new ChromosomePane(cgp.getPopulation().getChromosome(i), resources); tab = new Tab("Chr " + i); tab.setContent(chromosomes[i]); chromosomeTabs.getTabs().add(tab); } } private void updateNodeGrids() { for (int i = 0; i < chromosomes.length; i++) { chromosomes[i].update(); } } private void unlockOutputs() { for (int i = 0; i < chromosomes.length; i++) { chromosomes[i].unlockOutputs(); } } private void relockOutputs() { for (int i = 0; i < chromosomes.length; i++) { chromosomes[i].relockOutputs(); } } public void disableChromosomePanes(boolean value) { chromosomeTabs.setDisable(value); } public void playPause() { if (!evolving) { settings.disableSettings(true); disableChromosomePanes(true); unlockOutputs(); evolving = true; cgpService.restart(); } else { settings.disableSettings(false); disableChromosomePanes(false); updateNodeGrids(); evolving = false; cgpService.cancel(); relockOutputs(); } } public void step() { if (!evolving) { Thread t = new Thread(new Task() { @Override protected Void call() throws Exception { unlockOutputs(); cgp.nextGeneration(); console.flush(); updateNodeGrids(); relockOutputs(); return null; } }); t.start(); } } public void resetCGP() { cgp.remakePopulation(); makeChromosomeTabPane(); resources.set("currentGen", 1); resources.set("currentRun", 1); } }