diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-04 17:14:44 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-04 17:14:44 +0100 |
commit | c7969623b44f375e30fa3f15dcd7581609276a0f (patch) | |
tree | d1743df21e685fed93b7a12dd91a524e44fa42bd /src/jcgp/gui | |
parent | 0dbf126fc524bc029d9f5803d849b7c8f43fe389 (diff) |
Parameter validation refactored and fully functional.
Diffstat (limited to 'src/jcgp/gui')
-rw-r--r-- | src/jcgp/gui/GUI.java | 126 | ||||
-rw-r--r-- | src/jcgp/gui/console/GUIConsole.java | 7 | ||||
-rw-r--r-- | src/jcgp/gui/settings/SettingsPane.java | 109 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIBooleanParameter.java | 43 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIDoubleParameter.java | 41 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIIntegerParameter.java | 42 | ||||
-rw-r--r-- | src/jcgp/gui/settings/parameters/GUIParameter.java | 18 |
7 files changed, 240 insertions, 146 deletions
diff --git a/src/jcgp/gui/GUI.java b/src/jcgp/gui/GUI.java index 0902b21..f7aa25d 100644 --- a/src/jcgp/gui/GUI.java +++ b/src/jcgp/gui/GUI.java @@ -53,12 +53,38 @@ public class GUI extends Application { private GUIConsole console = new GUIConsole(); private SettingsPane settings; - private Service<Void> cgpService; - private boolean evolving = false; private Object printLock = new Object(); + private Service<Void> cgpService = new Service<Void> () { + @Override + protected Task<Void> createTask() { + Task<Void> t = new Task<Void>() { + @Override + protected Void call() throws Exception { + while (!isCancelled() && !cgp.isFinished()) { + synchronized (printLock) { + Platform.runLater(consoleFlush); + cgp.nextGeneration(); + printLock.wait(); + } + } + if (cgp.isFinished()) { + Platform.runLater(new Runnable() { + @Override + public void run() { + runningMode(false); + } + }); + } + return null; + } + }; + return t; + } + }; + private Runnable consoleFlush = new Runnable() { @Override public void run() { @@ -80,27 +106,6 @@ public class GUI extends Application { public void start(Stage primaryStage) throws Exception { resources.setConsole(console); - cgpService = new Service<Void> () { - @Override - protected Task<Void> createTask() { - - Task<Void> t = new Task<Void>() { - @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. * @@ -167,45 +172,68 @@ public class GUI extends Application { chromosomeTabs.setDisable(value); } - public void disable(boolean value) { - chromosomeTabs.setDisable(value); - settings.disableSettings(value); - } - - public void playPause() { - if (!evolving) { - disable(true); - unlockOutputs(); - evolving = true; - cgpService.restart(); - } else { - disable(false); - updateNodeGrids(); - evolving = false; - cgpService.cancel(); - relockOutputs(); + public void runPause() { + if (!cgp.isFinished() && settings.areParametersValid()) { + if (!evolving) { + runningMode(true); + cgpService.restart(); + } else { + cgpService.cancel(); + runningMode(false); + } } } public void step() { - if (!evolving) { - Thread t = new Thread(new Task<Void>() { + if (!evolving && !cgp.isFinished() && settings.areParametersValid()) { + if (settings.isResetRequired()) { + reset(); + } + unlockOutputs(); + Task<Void> task = new Task<Void>() { @Override protected Void call() throws Exception { - unlockOutputs(); cgp.nextGeneration(); - console.flush(); - updateNodeGrids(); - relockOutputs(); + Platform.runLater(consoleFlush); return null; } - }); + }; + Thread t = new Thread(task); t.start(); + try { + t.join(); + } catch (InterruptedException e) { + // nothing + } finally { + updateNodeGrids(); + relockOutputs(); + } } } public void reset() { - cgp.reset(); - makeChromosomeTabPane(); + if (!evolving && settings.areParametersValid()) { + settings.applyParameters(); + cgp.reset(); + makeChromosomeTabPane(); + settings.revalidateParameters(); + } + } + + private void runningMode(boolean value) { + chromosomeTabs.setDisable(value); + settings.disableSettings(value); + if (value) { + unlockOutputs(); + settings.getRunButton().setText("Pause"); + if (settings.isResetRequired()) { + reset(); + } + } else { + updateNodeGrids(); + relockOutputs(); + settings.getRunButton().setText("Run"); + } + evolving = value; } } diff --git a/src/jcgp/gui/console/GUIConsole.java b/src/jcgp/gui/console/GUIConsole.java index 8fdb1a5..d8625f5 100644 --- a/src/jcgp/gui/console/GUIConsole.java +++ b/src/jcgp/gui/console/GUIConsole.java @@ -10,15 +10,14 @@ import jcgp.gui.GUI; public class GUIConsole extends AnchorPane implements Console { private boolean dragging; - private TextArea textArea = new TextArea("Welcome to JCGP!\n"); + private TextArea textArea = new TextArea("Welcome to JCGP!"); private StringBuffer printBuffer = new StringBuffer(); public GUIConsole() { super(); textArea.setEditable(false); - textArea.setWrapText(true); - + setResizeListeners(); AnchorPane.setTopAnchor(textArea, GUI.RESIZE_MARGIN); @@ -91,7 +90,7 @@ public class GUIConsole extends AnchorPane implements Console { @Override public void println(String s) { - printBuffer.append(s + "\n"); + printBuffer.append("\n" + s); } @Override diff --git a/src/jcgp/gui/settings/SettingsPane.java b/src/jcgp/gui/settings/SettingsPane.java index 41b1bba..cb066b7 100644 --- a/src/jcgp/gui/settings/SettingsPane.java +++ b/src/jcgp/gui/settings/SettingsPane.java @@ -32,17 +32,16 @@ public class SettingsPane extends AnchorPane { private VBox mainContainer; private VBox bpPane, eaPane, mutatorPane, ffPane, nfPane; - private ArrayList<GUIParameter> parameters = new ArrayList<GUIParameter>(); - private GUI gui; + private Button runPause; + private ArrayList<GUIParameter> parameters = new ArrayList<GUIParameter>(); + private boolean dragging = false; public SettingsPane(JCGP cgp, GUI gui) { super(); - - this.gui = gui; - + setResizeListeners(); mainContainer = new VBox(8); @@ -85,22 +84,22 @@ public class SettingsPane extends AnchorPane { bpPane.getChildren().add(header); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("rows"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("columns"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("inputs"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("outputs"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("levelsBack"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("rows"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("columns"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("inputs"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("outputs"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("levelsBack"), this)); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("popSize"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("generations"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("runs"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("popSize"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("generations"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("runs"), this)); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentGen"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentRun"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentGen"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("currentRun"), this)); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("maxFitness"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("seed"))); - parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("report"))); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("maxFitness"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("seed"), this)); + parameters.add(GUIParameter.create((IntegerParameter) cgp.getResources().getParameter("report"), this)); bpPane.getChildren().addAll(parameters); @@ -238,43 +237,41 @@ public class SettingsPane extends AnchorPane { controls.setFillWidth(true); final HBox flowButtons = new HBox(2); - final Button play = new Button("Run"); - - play.setOnAction(new EventHandler<ActionEvent>() { + runPause = new Button("Run"); + runPause.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { - if (play.getText() == "Run") { - if (isResetRequired()) { - resetExperiment(); - } - play.setText("Pause"); - } else { - play.setText("Run"); - } - gui.playPause(); + gui.runPause(); } }); Button step = new Button("Step"); - step.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { - if (isResetRequired()) { - resetExperiment(); - } gui.step(); } }); - flowButtons.getChildren().addAll(play, step); + Button reset = new Button("Reset"); + reset.setOnAction(new EventHandler<ActionEvent>() { + @Override + public void handle(ActionEvent event) { + gui.reset(); + } + }); + + flowButtons.getChildren().addAll(runPause, step, reset); - HBox.setHgrow(play, Priority.ALWAYS); - play.setMaxWidth(Double.MAX_VALUE); + HBox.setHgrow(runPause, Priority.ALWAYS); + runPause.setMaxWidth(Double.MAX_VALUE); HBox.setHgrow(step, Priority.ALWAYS); step.setMaxWidth(Double.MAX_VALUE); + HBox.setHgrow(reset, Priority.ALWAYS); + reset.setMaxWidth(Double.MAX_VALUE); + controls.getChildren().add(flowButtons); mainContainer.getChildren().add(controls); @@ -288,11 +285,12 @@ public class SettingsPane extends AnchorPane { parameters.removeAll(vb.getChildren()); vb.getChildren().clear(); for (int i = 0; i < newParameters.length; i++) { - GUIParameter gp = GUIParameter.create(newParameters[i]); + GUIParameter gp = GUIParameter.create(newParameters[i], this); parameters.add(gp); vb.getChildren().add(gp); } + revalidateParameters(); } private void refreshFunctions(final FunctionSet fs, VBox vb) { @@ -377,21 +375,38 @@ public class SettingsPane extends AnchorPane { * * @return true if the experiment needs to be reset, false otherwise. */ - private boolean isResetRequired() { - boolean reset = false; + public boolean isResetRequired() { for (GUIParameter parameter : parameters) { - reset |= parameter.requiresReset(); + if (parameter.requiresReset()) { + return true; + } } - System.out.println("reset: " + reset); - return reset; + return false; } - private void resetExperiment() { + public boolean areParametersValid() { for (GUIParameter parameter : parameters) { - parameter.applyValue(); + if (!parameter.isValid()) { + return false; + } } - gui.reset(); - + return true; + } + + public void revalidateParameters() { + for (GUIParameter parameter : parameters) { + parameter.validate(); + } + } + + public void applyParameters() { + for (GUIParameter parameter : parameters) { + parameter.applyValue(); + } + } + + public Button getRunButton() { + return runPause; } diff --git a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java index da564f7..fc66e7d 100644 --- a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java @@ -6,12 +6,13 @@ import javafx.scene.control.CheckBox; import jcgp.backend.parameters.BooleanParameter; import jcgp.backend.parameters.ParameterStatus; import jcgp.gui.GUI; +import jcgp.gui.settings.SettingsPane; public class GUIBooleanParameter extends GUIParameter { private boolean originalValue; - GUIBooleanParameter(final BooleanParameter parameter) { + GUIBooleanParameter(final BooleanParameter parameter, final SettingsPane sp) { super(); this.parameter = parameter; @@ -32,18 +33,8 @@ public class GUIBooleanParameter extends GUIParameter { ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { parameter.set(newValue); - if (parameter.getStatus() == ParameterStatus.INVALID) { - valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); - valueControl.setTooltip(tooltip); - tooltip.setText(parameter.getStatus().getDetails()); - } else if (parameter.getStatus() == ParameterStatus.WARNING) { - valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); - valueControl.setTooltip(tooltip); - tooltip.setText(parameter.getStatus().getDetails()); - } else { - valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); - valueControl.setTooltip(null); - } + setValidityStyle(); + sp.revalidateParameters(); } }); } @@ -60,7 +51,8 @@ public class GUIBooleanParameter extends GUIParameter { @Override public boolean requiresReset() { - return parameter.isCritical() && ((BooleanParameter) parameter).get() != originalValue; + return (parameter.isCritical() && ((BooleanParameter) parameter).get() != originalValue) + || parameter.getStatus() == ParameterStatus.WARNING_RESET; } @Override @@ -68,4 +60,27 @@ public class GUIBooleanParameter extends GUIParameter { originalValue = ((BooleanParameter) parameter).get(); } + /** + * @param parameter + */ + private void setValidityStyle() { + if (parameter.getStatus() == ParameterStatus.INVALID) { + valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING || parameter.getStatus() == ParameterStatus.WARNING_RESET) { + valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); + valueControl.setTooltip(null); + } + } + + @Override + public void validate() { + ((BooleanParameter) parameter).validate(((BooleanParameter) parameter).get()); + setValidityStyle(); + } } diff --git a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java index a1b4526..190f4b7 100644 --- a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java @@ -11,12 +11,13 @@ import javafx.scene.text.Text; import jcgp.backend.parameters.DoubleParameter; import jcgp.backend.parameters.ParameterStatus; import jcgp.gui.GUI; +import jcgp.gui.settings.SettingsPane; public class GUIDoubleParameter extends GUIParameter { private double originalValue; - GUIDoubleParameter(final DoubleParameter parameter) { + GUIDoubleParameter(final DoubleParameter parameter, final SettingsPane sp) { super(); this.parameter = parameter; @@ -54,18 +55,8 @@ public class GUIDoubleParameter extends GUIParameter { String oldValue, String newValue) { if (!newValue.isEmpty()) { parameter.set(Double.parseDouble(newValue)); - if (parameter.getStatus() == ParameterStatus.INVALID) { - valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); - valueControl.setTooltip(tooltip); - tooltip.setText(parameter.getStatus().getDetails()); - } else if (parameter.getStatus() == ParameterStatus.WARNING) { - valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); - valueControl.setTooltip(tooltip); - tooltip.setText(parameter.getStatus().getDetails()); - } else { - valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); - valueControl.setTooltip(null); - } + setValidityStyle(); + sp.revalidateParameters(); } } @@ -93,7 +84,8 @@ public class GUIDoubleParameter extends GUIParameter { @Override public boolean requiresReset() { - return parameter.isCritical() && ((DoubleParameter) parameter).get() != originalValue; + return (parameter.isCritical() && ((DoubleParameter) parameter).get() != originalValue) + || parameter.getStatus() == ParameterStatus.WARNING_RESET; } @Override @@ -101,4 +93,25 @@ public class GUIDoubleParameter extends GUIParameter { originalValue = ((DoubleParameter) parameter).get(); } + @Override + public void validate() { + ((DoubleParameter) parameter).validate(((DoubleParameter) parameter).get()); + setValidityStyle(); + } + + private void setValidityStyle() { + if (parameter.getStatus() == ParameterStatus.INVALID) { + valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING || parameter.getStatus() == ParameterStatus.WARNING_RESET) { + valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); + valueControl.setTooltip(null); + } + } + } diff --git a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java index bf1a61e..a2040d7 100644 --- a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java @@ -11,12 +11,13 @@ import javafx.scene.text.Text; import jcgp.backend.parameters.IntegerParameter; import jcgp.backend.parameters.ParameterStatus; import jcgp.gui.GUI; +import jcgp.gui.settings.SettingsPane; public class GUIIntegerParameter extends GUIParameter { private int originalValue; - GUIIntegerParameter(final IntegerParameter parameter) { + GUIIntegerParameter(final IntegerParameter parameter, final SettingsPane sp) { super(); this.parameter = parameter; @@ -54,18 +55,8 @@ public class GUIIntegerParameter extends GUIParameter { String oldValue, String newValue) { if (!newValue.isEmpty()) { parameter.set(Integer.parseInt(newValue)); - if (parameter.getStatus() == ParameterStatus.INVALID) { - valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); - valueControl.setTooltip(tooltip); - tooltip.setText(parameter.getStatus().getDetails()); - } else if (parameter.getStatus() == ParameterStatus.WARNING) { - valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); - valueControl.setTooltip(tooltip); - tooltip.setText(parameter.getStatus().getDetails()); - } else { - valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); - valueControl.setTooltip(null); - } + setValidityStyle(); + sp.revalidateParameters(); } } @@ -92,7 +83,9 @@ public class GUIIntegerParameter extends GUIParameter { @Override public boolean requiresReset() { - return parameter.isCritical() && ((IntegerParameter) parameter).get() != originalValue; + // true if critical and the value has changed, or it requires a reset anyway + return (parameter.isCritical() && ((IntegerParameter) parameter).get() != originalValue) + || parameter.getStatus() == ParameterStatus.WARNING_RESET; } @Override @@ -100,4 +93,25 @@ public class GUIIntegerParameter extends GUIParameter { originalValue = ((IntegerParameter) parameter).get(); } + @Override + public void validate() { + ((IntegerParameter) parameter).validate(((IntegerParameter) parameter).get()); + setValidityStyle(); + } + + private void setValidityStyle() { + if (parameter.getStatus() == ParameterStatus.INVALID) { + valueControl.setStyle(GUI.INVALID_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING || parameter.getStatus() == ParameterStatus.WARNING_RESET) { + valueControl.setStyle(GUI.WARNING_PARAMETER_STYLE); + valueControl.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + valueControl.setStyle(GUI.VALID_PARAMETER_STYLE); + valueControl.setTooltip(null); + } + } + } diff --git a/src/jcgp/gui/settings/parameters/GUIParameter.java b/src/jcgp/gui/settings/parameters/GUIParameter.java index 9d6f07a..da2fe86 100644 --- a/src/jcgp/gui/settings/parameters/GUIParameter.java +++ b/src/jcgp/gui/settings/parameters/GUIParameter.java @@ -14,6 +14,8 @@ import jcgp.backend.parameters.BooleanParameter; import jcgp.backend.parameters.DoubleParameter; import jcgp.backend.parameters.IntegerParameter; import jcgp.backend.parameters.Parameter; +import jcgp.backend.parameters.ParameterStatus; +import jcgp.gui.settings.SettingsPane; public abstract class GUIParameter extends HBox { @@ -59,15 +61,23 @@ public abstract class GUIParameter extends HBox { public abstract void applyValue(); - public static GUIParameter create(Parameter parameter) { + public abstract void validate(); + + public static GUIParameter create(Parameter parameter, SettingsPane sp) { if (parameter instanceof IntegerParameter) { - return new GUIIntegerParameter((IntegerParameter) parameter); + return new GUIIntegerParameter((IntegerParameter) parameter, sp); } else if (parameter instanceof DoubleParameter) { - return new GUIDoubleParameter((DoubleParameter) parameter); + return new GUIDoubleParameter((DoubleParameter) parameter, sp); } else if (parameter instanceof BooleanParameter) { - return new GUIBooleanParameter((BooleanParameter) parameter); + return new GUIBooleanParameter((BooleanParameter) parameter, sp); } else { throw new ClassCastException("No GUIParameter subclass exists for argument of type " + parameter.getClass()); } } + + public boolean isValid() { + return parameter.getStatus() == ParameterStatus.VALID + || parameter.getStatus() == ParameterStatus.WARNING + || parameter.getStatus() == ParameterStatus.WARNING_RESET; + } } |