1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 07:50:09 -08:00

Plugin fixes

This commit is contained in:
Riley Jones 2017-08-03 15:16:26 -07:00
parent 0108c7bcc1
commit c680168d18
5 changed files with 45 additions and 14 deletions

View File

@ -68,6 +68,10 @@ public class AbacusController {
*/ */
private boolean calculating; private boolean calculating;
/**
* Seconds delayed for timer;
*/
private double delay = 0;
private Abacus abacus; private Abacus abacus;
@FXML @FXML
@ -103,6 +107,20 @@ public class AbacusController {
private void performCalculation(){ private void performCalculation(){
Runnable calculator = new Runnable(){ Runnable calculator = new Runnable(){
public void run() { 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; calculating = true;
Platform.runLater(() -> inputButton.setDisable(true)); Platform.runLater(() -> inputButton.setDisable(true));
TreeNode constructedTree = abacus.parseString(inputField.getText()); TreeNode constructedTree = abacus.parseString(inputField.getText());
@ -135,6 +153,7 @@ public class AbacusController {
}; };
if(!calculating) { if(!calculating) {
calcThread = new Thread(calculator); calcThread = new Thread(calculator);
calcThread.setName("calcThread");
calcThread.start(); calcThread.start();
} }
} }
@ -142,7 +161,7 @@ public class AbacusController {
private void stopCalculation(){ private void stopCalculation(){
calcThread.interrupt(); calcThread.interrupt();
calculating = false; calculating = false;
inputButton.setDisable(false); Platform.runLater(() ->inputButton.setDisable(false));
} }
} }

View File

@ -31,9 +31,13 @@ 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;
} }
@ -49,7 +53,10 @@ 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]);
} }
}); });
/** /**
@ -63,6 +70,8 @@ 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();
} }
}); });
@ -77,9 +86,13 @@ 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;
} }
@ -95,6 +108,8 @@ 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]);
} }
}); });
@ -110,15 +125,19 @@ 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()))).signum() == 1) { while ((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass())))!=null&&multiplier.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>());
@ -138,6 +157,8 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted())
return null;
return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1])); return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1]));
} }
}); });
@ -246,7 +267,7 @@ public class StandardPlugin extends Plugin {
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;
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) { while (!Thread.currentThread().isInterrupted()&&FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
n++; n++;
currentNumerator = currentNumerator.multiply(x).negate(); currentNumerator = currentNumerator.multiply(x).negate();
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass())); currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
@ -361,7 +382,7 @@ public class StandardPlugin extends Plugin {
} }
} }
if(Thread.currentThread().isInterrupted()) if(Thread.currentThread().isInterrupted())
return null; return NaiveNumber.ONE.promoteTo(numberClass);
return list.get(n); return list.get(n);
} }

View File

@ -95,12 +95,8 @@ public class BinaryNode extends TreeNode {
if(Thread.currentThread().isInterrupted()) if(Thread.currentThread().isInterrupted())
return null; 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;
if(Thread.currentThread().isInterrupted())
return null;
T a = reducer.reduceNode(this, leftReduce, rightReduce); T a = reducer.reduceNode(this, leftReduce, rightReduce);
if(Thread.currentThread().isInterrupted()) if(Thread.currentThread().isInterrupted())
return null; return null;

View File

@ -67,11 +67,8 @@ public class FunctionNode extends TreeNode {
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 (Thread.currentThread().isInterrupted()||reducedChildren[i] == null) return null;
} }
//System.out.println(Thread.currentThread().isInterrupted());
if(Thread.currentThread().isInterrupted())
return null;
T a = reducer.reduceNode(this, reducedChildren); T a = reducer.reduceNode(this, reducedChildren);
if(Thread.currentThread().isInterrupted()) if(Thread.currentThread().isInterrupted())
return null; return null;

View File

@ -37,8 +37,6 @@ public class UnaryNode extends TreeNode {
return null; return null;
Object reducedChild = applyTo.reduce(reducer); Object reducedChild = applyTo.reduce(reducer);
if (reducedChild == null) return null; if (reducedChild == null) return null;
if(Thread.currentThread().isInterrupted())
return null;
T a = reducer.reduceNode(this, reducedChild); T a = reducer.reduceNode(this, reducedChild);
if(Thread.currentThread().isInterrupted()) if(Thread.currentThread().isInterrupted())
return null; return null;