1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 23:40:08 -08:00

Merge branch 'time-limit'

This commit is contained in:
Danila Fedorin 2017-08-07 11:12:12 -07:00
commit c4a7003088
6 changed files with 90 additions and 11 deletions

View File

@ -30,6 +30,10 @@ public class Configuration {
*/
private static final TomlWriter TOML_WRITER = new TomlWriter();
/**
* The computation delay for which the thread can run without interruption.
*/
private double computationDelay = 0;
/**
* The implementation of the number that should be used.
*/
@ -51,10 +55,12 @@ public class Configuration {
/**
* Creates a new configuration with the given values.
*
* @param computationDelay the delay before the computation gets killed.
* @param numberImplementation the number implementation, like "naive" or "precise"
* @param disabledPlugins the list of disabled plugins.
*/
public Configuration(String numberImplementation, String[] disabledPlugins) {
public Configuration(double computationDelay, String numberImplementation, String[] disabledPlugins) {
this.computationDelay = computationDelay;
this.numberImplementation = numberImplementation;
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
}
@ -75,6 +81,7 @@ public class Configuration {
* @param otherConfiguration the configuration to copy from.
*/
public void copyFrom(Configuration otherConfiguration) {
this.computationDelay = otherConfiguration.computationDelay;
this.numberImplementation = otherConfiguration.numberImplementation;
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
}
@ -130,4 +137,23 @@ public class Configuration {
return disabledPlugins;
}
/**
* Gets the computation delay specified in the configuration.
*
* @return the computaton delay.
*/
public double getComputationDelay() {
return computationDelay;
}
/**
* Sets the computation delay.
*
* @param computationDelay the new computation delay.
*/
public void setComputationDelay(double computationDelay) {
this.computationDelay = computationDelay;
}
}

View File

@ -12,13 +12,25 @@ import javafx.stage.Stage;
*/
public class AbacusApplication extends Application {
/**
* The controller currently managing the application.
*/
private AbacusController controller;
@Override
public void start(Stage primaryStage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/abacus.fxml"));
Parent parent = loader.load();
controller = loader.getController();
Scene mainScene = new Scene(parent, 320, 480);
primaryStage.setScene(mainScene);
primaryStage.setTitle("Abacus");
primaryStage.show();
}
@Override
public void stop() throws Exception {
super.stop();
controller.performStop();
}
}

View File

@ -1,6 +1,8 @@
package org.nwapw.abacus.fx;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
@ -89,6 +91,8 @@ public class AbacusController implements PluginListener {
private ComboBox<String> numberImplementationBox;
@FXML
private ListView<ToggleablePlugin> enabledPluginView;
@FXML
private TextField computationLimitField;
/**
* The list of history entries, created by the users.
@ -124,6 +128,17 @@ public class AbacusController implements PluginListener {
* The alert shown when a press to "apply" is needed.
*/
private Alert reloadAlert;
/**
* The runnable that takes care of killing computations that take too long.
*/
private final Runnable TIMER_RUNNABLE = () -> {
try {
Configuration abacusConfig = abacus.getConfiguration();
if(abacusConfig.getComputationDelay() == 0) return;
Thread.sleep((long) (abacusConfig.getComputationDelay() * 1000));
performStop();
} catch (InterruptedException e) { }
};
/**
* The runnable used to perform the calculation.
*/
@ -161,6 +176,10 @@ public class AbacusController implements PluginListener {
});
}
};
/**
* The thread that is waiting to pause the calculation.
*/
private Thread computationLimitThread;
/**
* The thread in which the computation runs.
*/
@ -224,6 +243,15 @@ public class AbacusController implements PluginListener {
}
abacusPluginManager.reload();
computationLimitField.setText(Double.toString(abacus.getConfiguration().getComputationDelay()));
computationLimitField.textProperty().addListener((observable, oldValue, newValue) -> {
if(!newValue.matches("(\\d+(\\.\\d*)?)?")) {
computationLimitField.setText(oldValue);
} else {
changesMade = true;
}
});
changesMade = false;
reloadAlertShown = false;
@ -234,21 +262,29 @@ public class AbacusController implements PluginListener {
}
@FXML
private void performCalculation() {
public void performCalculation() {
inputButton.setDisable(true);
stopButton.setDisable(false);
calculationThread = new Thread(CALCULATION_RUNNABLE);
calculationThread.start();
computationLimitThread = new Thread(TIMER_RUNNABLE);
computationLimitThread.start();
}
@FXML
private void performStop(){
if(calculationThread != null)
public void performStop(){
if(calculationThread != null) {
calculationThread.interrupt();
calculationThread = null;
}
if(computationLimitThread != null){
computationLimitThread.interrupt();
computationLimitThread = null;
}
}
@FXML
private void performSaveAndReload() {
public void performSaveAndReload() {
performSave();
performReload();
changesMade = false;
@ -256,13 +292,13 @@ public class AbacusController implements PluginListener {
}
@FXML
private void performReload() {
public void performReload() {
alertIfApplyNeeded(true);
abacus.getPluginManager().reload();
}
@FXML
private void performSave() {
public void performSave() {
Configuration configuration = abacus.getConfiguration();
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
Set<String> disabledPlugins = configuration.getDisabledPlugins();
@ -270,6 +306,8 @@ public class AbacusController implements PluginListener {
for (ToggleablePlugin pluginEntry : enabledPlugins) {
if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
}
if(computationLimitField.getText().matches("\\d*(\\.\\d+)?") && computationLimitField.getText().length() != 0)
configuration.setComputationDelay(Double.parseDouble(computationLimitField.getText()));
configuration.saveTo(CONFIG_FILE);
changesMade = false;
reloadAlertShown = false;
@ -296,4 +334,5 @@ public class AbacusController implements PluginListener {
enabledPlugins.clear();
numberImplementationOptions.clear();
}
}

View File

@ -50,7 +50,9 @@
<ListView fx:id="enabledPluginView"
GridPane.rowIndex="1" GridPane.columnIndex="0"
GridPane.columnSpan="2" maxHeight="100"/>
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" hgap="10"
<Text GridPane.columnIndex="0" GridPane.rowIndex="2" text="Computation Limit"/>
<TextField fx:id="computationLimitField" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="3" hgap="10"
vgap="10">
<Button text="Apply" onAction="#performSave"/>
<Button text="Reload Plugins" onAction="#performReload"/>

View File

@ -11,7 +11,7 @@ import org.nwapw.abacus.tree.TreeNode;
public class CalculationTests {
private static Abacus abacus = new Abacus(new Configuration("precise", new String[]{}));
private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{}));
@BeforeClass
public static void prepareTests(){

View File

@ -19,7 +19,7 @@ import java.util.List;
public class TokenizerTests {
private static Abacus abacus = new Abacus(new Configuration("precise", new String[]{}));
private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{}));
private static LexerTokenizer lexerTokenizer = new LexerTokenizer();
private static Function subtractFunction = new Function() {
@Override