1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-05 02:09:53 -08:00

Remove old stopping code.

This commit is contained in:
Danila Fedorin 2017-08-05 13:26:29 -07:00
parent 508e98413d
commit 23a3eb88f1
6 changed files with 33 additions and 156 deletions

View File

@ -1,6 +1,5 @@
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;
@ -97,20 +96,6 @@ public class AbacusController implements PluginListener {
* The abacus instance used for changing the plugin configuration. * The abacus instance used for changing the plugin configuration.
*/ */
private Abacus abacus; private Abacus abacus;
/**
* Thread used for calculating.
*/
private Thread calcThread;
/**
* Checks whether the calculator is calculating.
*/
private boolean calculating;
/**
* Seconds delayed for timer;
*/
private double delay = 0;
/** /**
* Boolean which represents whether changes were made to the configuration. * Boolean which represents whether changes were made to the configuration.
@ -187,63 +172,24 @@ public class AbacusController implements PluginListener {
@FXML @FXML
private void performCalculation() { private void performCalculation() {
Runnable calculator = new Runnable() { inputButton.setDisable(true);
public void run() {
if (delay > 0) {
Runnable timer = new Runnable() {
public void run() {
long gap = (long) (delay * 1000);
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime <= gap) {
}
stopCalculation();
}
};
Thread maxTime = new Thread(timer);
maxTime.setName("maxTime");
maxTime.start();
}
calculating = true;
Platform.runLater(() -> inputButton.setDisable(true));
TreeNode constructedTree = abacus.parseString(inputField.getText()); TreeNode constructedTree = abacus.parseString(inputField.getText());
if (constructedTree == null) { if (constructedTree == null) {
Platform.runLater(() -> outputText.setText(ERR_SYNTAX)); outputText.setText(ERR_SYNTAX);
Platform.runLater(() -> inputButton.setDisable(false)); inputButton.setDisable(false);
//return; return;
} else { }
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree); NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
if (evaluatedNumber == null) { if (evaluatedNumber == null) {
if (Thread.currentThread().isInterrupted()) { outputText.setText(ERR_EVAL);
Platform.runLater(() -> outputText.setText(ERR_STOP)); inputButton.setDisable(false);
Platform.runLater(() -> inputButton.setDisable(false)); return;
} else {
Platform.runLater(() -> outputText.setText(ERR_EVAL));
Platform.runLater(() -> inputButton.setDisable(false));
//return;
} }
} else { outputText.setText(evaluatedNumber.toString());
Platform.runLater(() -> outputText.setText(evaluatedNumber.toString()));
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString())); historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
Platform.runLater(() -> inputButton.setDisable(false)); inputButton.setDisable(false);
Platform.runLater(() -> inputField.setText("")); inputField.setText("");
}
}
calculating = false;
}
};
if (!calculating) {
calcThread = new Thread(calculator);
calcThread.setName("calcThread");
calcThread.start();
}
}
@FXML
private void stopCalculation() {
calcThread.interrupt();
calculating = false;
//Platform.runLater(() ->inputButton.setDisable(false));
} }
@FXML @FXML

View File

@ -29,13 +29,9 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
NumberInterface sum = params[0]; NumberInterface sum = params[0];
for (int i = 1; i < params.length; i++) { for (int i = 1; i < params.length; i++) {
sum = sum.add(params[i]); sum = sum.add(params[i]);
if (Thread.currentThread().isInterrupted())
return null;
} }
return sum; return sum;
} }
@ -51,8 +47,6 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
return params[0].subtract(params[1]); return params[0].subtract(params[1]);
} }
@ -68,8 +62,6 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
return params[0].negate(); return params[0].negate();
} }
}); });
@ -84,13 +76,9 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
NumberInterface product = params[0]; NumberInterface product = params[0];
for (int i = 1; i < params.length; i++) { for (int i = 1; i < params.length; i++) {
product = product.multiply(params[i]); product = product.multiply(params[i]);
if (Thread.currentThread().isInterrupted())
return null;
} }
return product; return product;
} }
@ -106,8 +94,6 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
return params[0].divide(params[1]); return params[0].divide(params[1]);
} }
}); });
@ -125,19 +111,15 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
if (params[0].signum() == 0) { if (params[0].signum() == 0) {
return (new NaiveNumber(1)).promoteTo(params[0].getClass()); return (new NaiveNumber(1)).promoteTo(params[0].getClass());
} }
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 (!Thread.currentThread().isInterrupted() && (multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))) != null && multiplier.signum() == 1) { while ((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1) {
factorial = factorial.multiply(multiplier); factorial = factorial.multiply(multiplier);
} }
if (Thread.currentThread().isInterrupted())
return null;
return factorial; return factorial;
/*if(!storedList.containsKey(params[0].getClass())){ /*if(!storedList.containsKey(params[0].getClass())){
storedList.put(params[0].getClass(), new ArrayList<NumberInterface>()); storedList.put(params[0].getClass(), new ArrayList<NumberInterface>());
@ -157,8 +139,6 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass())); return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass()));
} }
}; };
@ -173,32 +153,26 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted())
return null;
NumberInterface param = params[0]; NumberInterface param = params[0];
int powersOf2 = 0; int powersOf2 = 0;
NumberInterface check; while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) {
while (!Thread.currentThread().isInterrupted() && (check = FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())))) != null && (check.compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass()))) >= 0) { if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
if ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))) != null && check.signum() == 1) {
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass())); param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
powersOf2++; powersOf2++;
if ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))) == null || check.signum() != 1) { if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
break; break;
//No infinite loop for you. //No infinite loop for you.
} }
} else { } else {
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass())); param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
powersOf2--; powersOf2--;
if ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))) == null || check.signum() != 1) { if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
break; break;
//No infinite loop for you. //No infinite loop for you.
} }
} }
} }
NumberInterface check2; return getLog2(param).multiply((new NaiveNumber(powersOf2)).promoteTo(param.getClass())).add(getLogPartialSum(param));
if (!Thread.currentThread().isInterrupted() && (check = getLog2(param)) != null && (check = check.multiply((new NaiveNumber(powersOf2).promoteTo(param.getClass())))) != null && (check2 = getLogPartialSum(param)) != null && (check = check.add(check2)) != null)
return check;
return null;
} }
/** /**
@ -209,22 +183,16 @@ public class StandardPlugin extends Plugin {
*/ */
private NumberInterface getLogPartialSum(NumberInterface x) { private NumberInterface getLogPartialSum(NumberInterface x) {
if (Thread.currentThread().isInterrupted())
return null;
NumberInterface maxError = getMaxError(x); NumberInterface maxError = getMaxError(x);
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1). x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
NumberInterface currentNumerator = x, currentTerm = x, sum = x; NumberInterface currentNumerator = x, currentTerm = x, sum = x;
int n = 1; int n = 1;
NumberInterface check; while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
while (!Thread.currentThread().isInterrupted() && (check = FUNCTION_ABS.apply(currentTerm)) != null && check.compareTo(maxError) > 0) {
n++; n++;
if ((currentNumerator = currentNumerator.multiply(x)) == null || (currentNumerator = currentNumerator.negate()) == null) currentNumerator = currentNumerator.multiply(x).negate();
return null;
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass())); currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
sum = sum.add(currentTerm); sum = sum.add(currentTerm);
} }
if (Thread.currentThread().isInterrupted())
return null;
return sum; return sum;
} }
@ -234,8 +202,6 @@ public class StandardPlugin extends Plugin {
* @return the value of log(2) with the appropriate precision. * @return the value of log(2) with the appropriate precision.
*/ */
private NumberInterface getLog2(NumberInterface number) { private NumberInterface getLog2(NumberInterface number) {
if (Thread.currentThread().isInterrupted())
return null;
NumberInterface maxError = getMaxError(number); NumberInterface maxError = getMaxError(number);
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass()); //NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n) //We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
@ -244,17 +210,13 @@ 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 (!Thread.currentThread().isInterrupted() && a.compareTo(maxError) >= 1) { while (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()));
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass())); c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
NumberInterface check; sum = sum.add(a.add(b).multiply(c));
if (a == null || (check = a.add(b)) == null || (check = check.multiply(c)) == null || (sum = sum.add(check)) == null)
return null;
} }
if (Thread.currentThread().isInterrupted())
return null;
return sum; return sum;
} }
}; };
@ -378,15 +340,11 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (Thread.currentThread().isInterrupted()) return null; if (params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
else if (params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
return NaiveNumber.ZERO.promoteTo(params[0].getClass()); return NaiveNumber.ZERO.promoteTo(params[0].getClass());
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0) else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
return NaiveNumber.ONE.promoteTo(params[1].getClass()); return NaiveNumber.ONE.promoteTo(params[1].getClass());
FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1])); return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
NumberInterface check = FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0]));
if(check == null) return null;
return FUNCTION_EXP.apply(check.multiply(params[1]));
} }
}); });
/** /**
@ -524,8 +482,6 @@ public class StandardPlugin extends Plugin {
* @return a number of numClass with value n factorial. * @return a number of numClass with value n factorial.
*/ */
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n) { public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n) {
if (Thread.currentThread().isInterrupted())
return null;
if (!FACTORIAL_LISTS.containsKey(numberClass)) { if (!FACTORIAL_LISTS.containsKey(numberClass)) {
FACTORIAL_LISTS.put(numberClass, new ArrayList<>()); FACTORIAL_LISTS.put(numberClass, new ArrayList<>());
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass)); FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
@ -533,12 +489,10 @@ public class StandardPlugin extends Plugin {
} }
ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(numberClass); ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(numberClass);
if (n >= list.size()) { if (n >= list.size()) {
while (!Thread.currentThread().isInterrupted() && list.size() < n + 16) { while (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);
} }
@ -578,8 +532,6 @@ public class StandardPlugin extends Plugin {
} }
public static NumberInterface intPow(NumberInterface number, Class<? extends NumberInterface> numberClass, NumberInterface exponent) { public static NumberInterface intPow(NumberInterface number, Class<? extends NumberInterface> numberClass, NumberInterface exponent) {
if (Thread.currentThread().isInterrupted())
return null;
if (exponent.compareTo((new NaiveNumber(0)).promoteTo(numberClass)) == 0) { if (exponent.compareTo((new NaiveNumber(0)).promoteTo(numberClass)) == 0) {
return (new NaiveNumber(1)).promoteTo(numberClass); return (new NaiveNumber(1)).promoteTo(numberClass);
} }
@ -588,14 +540,10 @@ public class StandardPlugin extends Plugin {
NumberInterface power = number; NumberInterface power = number;
for (NumberInterface currentExponent = (new NaiveNumber(1)).promoteTo(numberClass); currentExponent.compareTo(exponent) < 0; currentExponent = currentExponent.add((new NaiveNumber(1)).promoteTo(numberClass))) { for (NumberInterface currentExponent = (new NaiveNumber(1)).promoteTo(numberClass); currentExponent.compareTo(exponent) < 0; currentExponent = currentExponent.add((new NaiveNumber(1)).promoteTo(numberClass))) {
power = power.multiply(number); power = power.multiply(number);
if (Thread.currentThread().isInterrupted())
return null;
} }
if (takeReciprocal) { if (takeReciprocal) {
power = (new NaiveNumber(1)).promoteTo(numberClass).divide(power); power = (new NaiveNumber(1)).promoteTo(numberClass).divide(power);
} }
if (Thread.currentThread().isInterrupted())
return null;
return power; return power;
} }

View File

@ -92,15 +92,10 @@ 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);
T rightReduce = right.reduce(reducer); T rightReduce = right.reduce(reducer);
if (leftReduce == null || rightReduce == null) return null; if (leftReduce == null || rightReduce == null) return null;
T a = reducer.reduceNode(this, leftReduce, rightReduce); return reducer.reduceNode(this, leftReduce, rightReduce);
if (Thread.currentThread().isInterrupted())
return null;
return a;
} }
@Override @Override

View File

@ -62,17 +62,12 @@ 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 (Thread.currentThread().isInterrupted() || reducedChildren[i] == null) return null; if (reducedChildren[i] == null) return null;
} }
T a = reducer.reduceNode(this, reducedChildren); return reducer.reduceNode(this, reducedChildren);
if (Thread.currentThread().isInterrupted())
return null;
return a;
} }
@Override @Override

View File

@ -33,14 +33,9 @@ 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;
T a = reducer.reduceNode(this, reducedChild); return reducer.reduceNode(this, reducedChild);
if (Thread.currentThread().isInterrupted())
return null;
return a;
} }
/** /**

View File

@ -34,8 +34,6 @@
<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>