From 9d5f9d901cbfe87d4b0bea4e343342bd9f8157db Mon Sep 17 00:00:00 2001 From: Riley Jones Date: Thu, 3 Aug 2017 15:16:26 -0700 Subject: [PATCH] Plugin fixes --- .../org/nwapw/abacus/fx/AbacusController.java | 21 ++++++++++++++- .../nwapw/abacus/plugin/StandardPlugin.java | 27 ++++++++++++++++--- .../org/nwapw/abacus/tree/BinaryNode.java | 4 --- .../org/nwapw/abacus/tree/FunctionNode.java | 5 +--- .../java/org/nwapw/abacus/tree/UnaryNode.java | 2 -- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/src/main/java/org/nwapw/abacus/fx/AbacusController.java index 8b45f85..9a4478c 100644 --- a/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -68,6 +68,10 @@ public class AbacusController { */ private boolean calculating; + /** + * Seconds delayed for timer; + */ + private double delay = 0; private Abacus abacus; @FXML @@ -103,6 +107,20 @@ public class AbacusController { private void performCalculation(){ Runnable calculator = new Runnable(){ 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()); @@ -135,6 +153,7 @@ public class AbacusController { }; if(!calculating) { calcThread = new Thread(calculator); + calcThread.setName("calcThread"); calcThread.start(); } } @@ -142,7 +161,7 @@ public class AbacusController { private void stopCalculation(){ calcThread.interrupt(); calculating = false; - inputButton.setDisable(false); + Platform.runLater(() ->inputButton.setDisable(false)); } } diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index f879a65..9fa9d29 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -31,9 +31,13 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(Thread.currentThread().isInterrupted()) + return null; NumberInterface sum = params[0]; for (int i = 1; i < params.length; i++) { sum = sum.add(params[i]); + if(Thread.currentThread().isInterrupted()) + return null; } return sum; } @@ -49,7 +53,10 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(Thread.currentThread().isInterrupted()) + return null; return params[0].subtract(params[1]); + } }); /** @@ -63,6 +70,8 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(Thread.currentThread().isInterrupted()) + return null; return params[0].negate(); } }); @@ -77,9 +86,13 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(Thread.currentThread().isInterrupted()) + return null; NumberInterface product = params[0]; for (int i = 1; i < params.length; i++) { product = product.multiply(params[i]); + if(Thread.currentThread().isInterrupted()) + return null; } return product; } @@ -95,6 +108,8 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(Thread.currentThread().isInterrupted()) + return null; return params[0].divide(params[1]); } }); @@ -110,15 +125,19 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(Thread.currentThread().isInterrupted()) + return null; if (params[0].signum() == 0) { return (new NaiveNumber(1)).promoteTo(params[0].getClass()); } NumberInterface factorial = params[0]; NumberInterface multiplier = params[0]; //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); } + if(Thread.currentThread().isInterrupted()) + return null; return factorial; /*if(!storedList.containsKey(params[0].getClass())){ storedList.put(params[0].getClass(), new ArrayList()); @@ -138,6 +157,8 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(Thread.currentThread().isInterrupted()) + return null; 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). NumberInterface currentNumerator = x, currentTerm = x, sum = x; int n = 1; - while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) { + while (!Thread.currentThread().isInterrupted()&&FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) { n++; currentNumerator = currentNumerator.multiply(x).negate(); currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass())); @@ -361,7 +382,7 @@ public class StandardPlugin extends Plugin { } } if(Thread.currentThread().isInterrupted()) - return null; + return NaiveNumber.ONE.promoteTo(numberClass); return list.get(n); } diff --git a/src/main/java/org/nwapw/abacus/tree/BinaryNode.java b/src/main/java/org/nwapw/abacus/tree/BinaryNode.java index 7d95404..c165088 100644 --- a/src/main/java/org/nwapw/abacus/tree/BinaryNode.java +++ b/src/main/java/org/nwapw/abacus/tree/BinaryNode.java @@ -95,12 +95,8 @@ public class BinaryNode extends TreeNode { 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; - if(Thread.currentThread().isInterrupted()) - return null; T a = reducer.reduceNode(this, leftReduce, rightReduce); if(Thread.currentThread().isInterrupted()) return null; diff --git a/src/main/java/org/nwapw/abacus/tree/FunctionNode.java b/src/main/java/org/nwapw/abacus/tree/FunctionNode.java index af0662d..b685b1c 100644 --- a/src/main/java/org/nwapw/abacus/tree/FunctionNode.java +++ b/src/main/java/org/nwapw/abacus/tree/FunctionNode.java @@ -67,11 +67,8 @@ public class FunctionNode extends TreeNode { 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; + 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); if(Thread.currentThread().isInterrupted()) return null; diff --git a/src/main/java/org/nwapw/abacus/tree/UnaryNode.java b/src/main/java/org/nwapw/abacus/tree/UnaryNode.java index ce1ff90..def2fd8 100644 --- a/src/main/java/org/nwapw/abacus/tree/UnaryNode.java +++ b/src/main/java/org/nwapw/abacus/tree/UnaryNode.java @@ -37,8 +37,6 @@ public class UnaryNode extends TreeNode { return null; Object reducedChild = applyTo.reduce(reducer); if (reducedChild == null) return null; - if(Thread.currentThread().isInterrupted()) - return null; T a = reducer.reduceNode(this, reducedChild); if(Thread.currentThread().isInterrupted()) return null;