mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-18 00:19:32 -08:00
Add stop button
This commit is contained in:
parent
beb390ad27
commit
0108c7bcc1
|
@ -1,5 +1,6 @@
|
|||
package org.nwapw.abacus.fx;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
|
@ -25,7 +26,10 @@ public class AbacusController {
|
|||
* Constant string that is displayed if the tree could not be reduced.
|
||||
*/
|
||||
private static final String ERR_EVAL = "Evaluation Error";
|
||||
|
||||
/**
|
||||
* Constant string that is displayed if the calculations are stopped before they are done.
|
||||
*/
|
||||
private static final String ERR_STOP = "Stopped";
|
||||
@FXML
|
||||
private TableView<HistoryModel> historyTable;
|
||||
@FXML
|
||||
|
@ -54,6 +58,16 @@ public class AbacusController {
|
|||
*/
|
||||
private ObservableList<String> numberImplementationOptions;
|
||||
|
||||
/**
|
||||
* Thread used for calculating.
|
||||
*/
|
||||
private Thread calcThread;
|
||||
|
||||
/**
|
||||
* Checks whether the calculator is calculating.
|
||||
*/
|
||||
private boolean calculating;
|
||||
|
||||
private Abacus abacus;
|
||||
|
||||
@FXML
|
||||
|
@ -87,24 +101,48 @@ public class AbacusController {
|
|||
|
||||
@FXML
|
||||
private void performCalculation(){
|
||||
inputButton.setDisable(true);
|
||||
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
||||
if(constructedTree == null){
|
||||
outputText.setText(ERR_SYNTAX);
|
||||
inputButton.setDisable(false);
|
||||
return;
|
||||
}
|
||||
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
||||
if(evaluatedNumber == null){
|
||||
outputText.setText(ERR_EVAL);
|
||||
inputButton.setDisable(false);
|
||||
return;
|
||||
}
|
||||
outputText.setText(evaluatedNumber.toString());
|
||||
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
||||
Runnable calculator = new Runnable(){
|
||||
public void run() {
|
||||
calculating = true;
|
||||
Platform.runLater(() -> inputButton.setDisable(true));
|
||||
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
||||
if (constructedTree == null) {
|
||||
Platform.runLater(() ->outputText.setText(ERR_SYNTAX));
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
//return;
|
||||
}else {
|
||||
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
||||
if (evaluatedNumber == null) {
|
||||
if(Thread.currentThread().isInterrupted()){
|
||||
Platform.runLater(() -> outputText.setText(ERR_STOP));
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
}else {
|
||||
Platform.runLater(() -> outputText.setText(ERR_EVAL));
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
//return;
|
||||
}
|
||||
} else {
|
||||
Platform.runLater(() -> outputText.setText(evaluatedNumber.toString()));
|
||||
|
||||
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
||||
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
Platform.runLater(() -> inputField.setText(""));
|
||||
}
|
||||
}
|
||||
calculating = false;
|
||||
}
|
||||
};
|
||||
if(!calculating) {
|
||||
calcThread = new Thread(calculator);
|
||||
calcThread.start();
|
||||
}
|
||||
}
|
||||
@FXML
|
||||
private void stopCalculation(){
|
||||
calcThread.interrupt();
|
||||
calculating = false;
|
||||
inputButton.setDisable(false);
|
||||
inputField.setText("");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ public class StandardPlugin extends Plugin {
|
|||
NumberInterface factorial = params[0];
|
||||
NumberInterface multiplier = params[0];
|
||||
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
||||
while ((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1) {
|
||||
while (!Thread.currentThread().isInterrupted()&&(multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1) {
|
||||
factorial = factorial.multiply(multiplier);
|
||||
}
|
||||
return factorial;
|
||||
|
@ -170,11 +170,12 @@ public class StandardPlugin extends Plugin {
|
|||
int n = 0;
|
||||
if(params[0].signum() <= 0){
|
||||
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
||||
while(FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0){
|
||||
while(!Thread.currentThread().isInterrupted()&&FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0){
|
||||
n++;
|
||||
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
||||
sum = sum.add(currentTerm);
|
||||
}
|
||||
//System.out.println(Thread.currentThread().isInterrupted());
|
||||
return sum;
|
||||
}
|
||||
else{
|
||||
|
@ -192,8 +193,11 @@ public class StandardPlugin extends Plugin {
|
|||
right = right.multiply(nextN);
|
||||
//System.out.println(left + ", " + right);
|
||||
}
|
||||
while(left.compareTo(right) > 0);
|
||||
while(!Thread.currentThread().isInterrupted()&&left.compareTo(right) > 0);
|
||||
//System.out.println(n+1);
|
||||
//System.out.println(Thread.currentThread().isInterrupted());
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return left;
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
@ -211,7 +215,7 @@ public class StandardPlugin extends Plugin {
|
|||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface param = params[0];
|
||||
int powersOf2 = 0;
|
||||
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass())) >= 0) {
|
||||
while (!Thread.currentThread().isInterrupted()&&FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass())) >= 0) {
|
||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
||||
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||
powersOf2++;
|
||||
|
@ -265,7 +269,7 @@ public class StandardPlugin extends Plugin {
|
|||
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
|
||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
||||
int n = 0;
|
||||
while (a.compareTo(maxError) >= 1) {
|
||||
while (!Thread.currentThread().isInterrupted()&&a.compareTo(maxError) >= 1) {
|
||||
n++;
|
||||
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
|
||||
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
|
||||
|
@ -352,10 +356,12 @@ public class StandardPlugin extends Plugin {
|
|||
}
|
||||
ArrayList<NumberInterface> list = factorialLists.get(numberClass);
|
||||
if(n >= list.size()){
|
||||
while(list.size() < n + 16){
|
||||
while(!Thread.currentThread().isInterrupted()&&list.size() < n + 16){
|
||||
list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass)));
|
||||
}
|
||||
}
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
return list.get(n);
|
||||
}
|
||||
|
||||
|
|
|
@ -92,10 +92,19 @@ public class BinaryNode extends TreeNode {
|
|||
|
||||
@Override
|
||||
public <T> T reduce(Reducer<T> reducer) {
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
T leftReduce = left.reduce(reducer);
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
T rightReduce = right.reduce(reducer);
|
||||
if (leftReduce == null || rightReduce == null) return null;
|
||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
T a = reducer.reduceNode(this, leftReduce, rightReduce);
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -62,12 +62,20 @@ public class FunctionNode extends TreeNode {
|
|||
|
||||
@Override
|
||||
public <T> T reduce(Reducer<T> reducer) {
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
Object[] reducedChildren = new Object[children.size()];
|
||||
for (int i = 0; i < reducedChildren.length; i++) {
|
||||
reducedChildren[i] = children.get(i).reduce(reducer);
|
||||
if (reducedChildren[i] == null) return null;
|
||||
}
|
||||
return reducer.reduceNode(this, reducedChildren);
|
||||
//System.out.println(Thread.currentThread().isInterrupted());
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
T a = reducer.reduceNode(this, reducedChildren);
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -33,9 +33,16 @@ public class UnaryNode extends TreeNode {
|
|||
|
||||
@Override
|
||||
public <T> T reduce(Reducer<T> reducer) {
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
Object reducedChild = applyTo.reduce(reducer);
|
||||
if (reducedChild == null) return null;
|
||||
return reducer.reduceNode(this, reducedChild);
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
T a = reducer.reduceNode(this, reducedChild);
|
||||
if(Thread.currentThread().isInterrupted())
|
||||
return null;
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -37,6 +37,8 @@
|
|||
<TextField fx:id="inputField" onAction="#performCalculation"/>
|
||||
<Button fx:id="inputButton" text="Calculate" maxWidth="Infinity"
|
||||
onAction="#performCalculation"/>
|
||||
<Button fx:id="stopButton" text="Stop" maxWidth="Infinity"
|
||||
onAction="#stopCalculation"/>
|
||||
</VBox>
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
|
|
Loading…
Reference in New Issue
Block a user