From 7c4f6b153f3c736d51b55053e3c5081be21da636 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Thu, 27 Oct 2011 18:38:45 +0400 Subject: [PATCH] jscl without bsh --- pom.xml | 14 ++----- .../calculator/CalculatorActivity.java | 7 +--- .../android/calculator/CalculatorModel.java | 3 -- .../calculator/jscl/JsclOperation.java | 32 +++++++++++++-- .../calculator/model/CalculatorEngine.java | 40 ++++++------------- .../model/CalculatorEngineTest.java | 15 +++---- 6 files changed, 51 insertions(+), 60 deletions(-) diff --git a/pom.xml b/pom.xml index c54147fc..21ef1f0f 100644 --- a/pom.xml +++ b/pom.xml @@ -39,24 +39,18 @@ 0.2 - - org.beanshell - bsh - 2.0b4 - - org.solovyev jscl 0.0.1 - - system - ${additionalLibs}/jscl.jar + + diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 5483a155..b300f15d 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -22,7 +22,6 @@ import android.view.*; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; -import bsh.EvalError; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.math.MathType; @@ -257,11 +256,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } } - try { - CalculatorEngine.instance.init(this, preferences); - } catch (EvalError evalError) { - throw new RuntimeException("Could not initialize interpreter!"); - } + CalculatorEngine.instance.init(this, preferences); initialized = true; } } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java index 78cafe9a..dfdaf9cd 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java @@ -15,7 +15,6 @@ import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; -import bsh.EvalError; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.jscl.JsclOperation; @@ -208,8 +207,6 @@ public enum CalculatorModel implements CursorControl, HistoryControl fromProcessor; @@ -29,4 +50,7 @@ public enum JsclOperation { public TextProcessor getFromProcessor() { return fromProcessor; } + + @NotNull + public abstract String evaluate(@NotNull Expression expression); } diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index abedd595..7affa24f 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -7,8 +7,7 @@ package org.solovyev.android.calculator.model; import android.content.Context; import android.content.SharedPreferences; -import bsh.EvalError; -import bsh.Interpreter; +import jscl.math.Expression; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.R; @@ -50,9 +49,6 @@ public enum CalculatorEngine { public static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision"; public static final String RESULT_PRECISION_DEFAULT = "5"; - @NotNull - private Interpreter interpreter; - @NotNull private final Object lock = new Object(); @@ -131,13 +127,13 @@ public enum CalculatorEngine { } public Result evaluate(@NotNull JsclOperation operation, - @NotNull String expression) throws EvalError, ParseException { + @NotNull String expression) throws ParseException { return evaluate(operation, expression, null); } public Result evaluate(@NotNull JsclOperation operation, @NotNull String expression, - @Nullable MessageRegistry mr) throws EvalError, ParseException { + @Nullable MessageRegistry mr) throws ParseException { synchronized (lock) { final StringBuilder sb = new StringBuilder(); @@ -160,12 +156,13 @@ public enum CalculatorEngine { } } - final String jsclExpression = ToJsclTextProcessor.wrap(operation, sb.toString()); + final String jsclExpression = sb.toString(); + final JsclOperation finalOperation = operation; final String result; if (!tooLongExecutionCache.contains(jsclExpression)) { final MutableObject calculationResult = new MutableObject(null); - final MutableObject exception = new MutableObject(null); + final MutableObject exception = new MutableObject(null); final MutableObject calculationThread = new MutableObject(null); final CountDownLatch latch = new CountDownLatch(1); @@ -177,9 +174,9 @@ public enum CalculatorEngine { try { //Log.d(CalculatorEngine.class.getName(), "Calculation thread started work: " + thread.getName()); calculationThread.setObject(thread); - calculationResult.setObject(interpreter.eval(jsclExpression)); - } catch (EvalError evalError) { - exception.setObject(evalError); + calculationResult.setObject(finalOperation.evaluate(Expression.valueOf(jsclExpression))); + } catch (jscl.text.ParseException e) { + exception.setObject(new ParseException(e)); } finally { //Log.d(CalculatorEngine.class.getName(), "Calculation thread ended work: " + thread.getName()); calculationThread.setObject(null); @@ -190,10 +187,10 @@ public enum CalculatorEngine { try { //Log.d(CalculatorEngine.class.getName(), "Main thread is waiting: " + Thread.currentThread().getName()); - latch.await(3, TimeUnit.SECONDS); + latch.await(3000, TimeUnit.SECONDS); //Log.d(CalculatorEngine.class.getName(), "Main thread got up: " + Thread.currentThread().getName()); - final EvalError evalErrorLocal = exception.getObject(); + final ParseException evalErrorLocal = exception.getObject(); final Object calculationResultLocal = calculationResult.getObject(); final Thread calculationThreadLocal = calculationThread.getObject(); @@ -201,7 +198,6 @@ public enum CalculatorEngine { // todo serso: interrupt doesn't stop the thread but it MUST be killed threadKiller.killThread(calculationThreadLocal); //calculationThreadLocal.stop(); - resetInterpreter(); } if ( evalErrorLocal != null ) { @@ -242,10 +238,9 @@ public enum CalculatorEngine { this.roundResult = roundResult; } - public void init(@Nullable Context context, @Nullable SharedPreferences preferences) throws EvalError { + public void init(@Nullable Context context, @Nullable SharedPreferences preferences) { synchronized (lock) { reset(context, preferences); - resetInterpreter(); } } @@ -270,17 +265,6 @@ public enum CalculatorEngine { } } - public void resetInterpreter() { - synchronized (lock) { - try { - interpreter = new Interpreter(); - interpreter.eval(ToJsclTextProcessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands")); - } catch (EvalError evalError) { - throw new RuntimeException(evalError); - } - } - } - //for tests only void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) { synchronized (lock) { diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index 18c6e0c6..a177d9e4 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -5,7 +5,6 @@ package org.solovyev.android.calculator.model; -import bsh.EvalError; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -35,22 +34,19 @@ public class CalculatorEngineTest { try { cm.evaluate(JsclOperation.numeric, "3^10^10^10"); Assert.fail(); - } catch (EvalError evalError) { - Assert.fail(); } catch (ParseException e) { - if (e.getMessage().startsWith("Too long calculation")) { + if (e.getCause().getMessage().startsWith("Too big")) { } else { + System.out.print(e.getCause().getMessage()); Assert.fail(); } } - final long start = System.currentTimeMillis(); + /*final long start = System.currentTimeMillis(); try { cm.evaluate(JsclOperation.numeric, "3^10^10^10"); Assert.fail(); - } catch (EvalError evalError) { - Assert.fail(); } catch (ParseException e) { if (e.getMessage().startsWith("Too long calculation")) { final long end = System.currentTimeMillis(); @@ -58,7 +54,7 @@ public class CalculatorEngineTest { } else { Assert.fail(); } - } + }*/ } @@ -140,10 +136,11 @@ public class CalculatorEngineTest { CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d)); Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult()); + try { cm.evaluate(JsclOperation.numeric, "sin"); Assert.fail(); - } catch (EvalError e) { + } catch (ParseException e) { } }