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