From b756cb6966f4b6f6aa6b985f201a6c5c7f48e83f Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 1 Nov 2011 12:31:38 +0400 Subject: [PATCH] postfix functions registry --- .../calculator/jscl/JsclOperation.java | 17 +++--- .../android/calculator/math/Functions.java | 26 ---------- .../android/calculator/math/MathType.java | 9 +++- .../model/AndroidFunctionsRegistryImpl.java | 23 +++++--- .../calculator/model/CalculatorEngine.java | 52 ++++++++----------- 5 files changed, 54 insertions(+), 73 deletions(-) delete mode 100644 src/main/java/org/solovyev/android/calculator/math/Functions.java diff --git a/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java b/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java index 5a21b05f..435415f3 100644 --- a/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java +++ b/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java @@ -6,8 +6,9 @@ package org.solovyev.android.calculator.jscl; -import jscl.math.Expression; +import jscl.text.ParseException; import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.DummyTextProcessor; import org.solovyev.android.calculator.model.FromJsclSimplifyTextProcessor; import org.solovyev.android.calculator.model.TextProcessor; @@ -17,16 +18,16 @@ public enum JsclOperation { simplify(new FromJsclSimplifyTextProcessor()) { @NotNull @Override - public String evaluate(@NotNull Expression expression) { - return expression.simplify().toString(); + public String evaluate(@NotNull String expression) throws ParseException { + return CalculatorEngine.instance.getEngine().simplify(expression); } }, elementary(DummyTextProcessor.instance) { @NotNull @Override - public String evaluate(@NotNull Expression expression) { - return expression.elementary().toString(); + public String evaluate(@NotNull String expression) throws ParseException { + return CalculatorEngine.instance.getEngine().elementary(expression); } }, @@ -34,8 +35,8 @@ public enum JsclOperation { numeric(new FromJsclNumericTextProcessor()) { @NotNull @Override - public String evaluate(@NotNull Expression expression) { - return expression.numeric().toString(); + public String evaluate(@NotNull String expression) throws ParseException { + return CalculatorEngine.instance.getEngine().evaluate(expression); } }; @@ -52,5 +53,5 @@ public enum JsclOperation { } @NotNull - public abstract String evaluate(@NotNull Expression expression); + public abstract String evaluate(@NotNull String expression) throws ParseException; } diff --git a/src/main/java/org/solovyev/android/calculator/math/Functions.java b/src/main/java/org/solovyev/android/calculator/math/Functions.java deleted file mode 100644 index 5fc42eb1..00000000 --- a/src/main/java/org/solovyev/android/calculator/math/Functions.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - */ - -package org.solovyev.android.calculator.math; - -import java.util.*; - -/** - * User: serso - * Date: 9/17/11 - * Time: 10:01 PM - */ -public class Functions { - - // not intended for instantiation - private Functions() { - throw new AssertionError("Not allowed!"); - } - - public final static String DEGREE = "°"; - public final static String FACTORIAL = "!"; - - public static final List allPostfix = Arrays.asList(FACTORIAL, DEGREE); -} diff --git a/src/main/java/org/solovyev/android/calculator/math/MathType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java index edebdcf2..871899e7 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -49,7 +49,14 @@ public enum MathType { } }, - postfix_function(400, false, true, Functions.allPostfix), + postfix_function(400, false, true) { + @NotNull + @Override + public List getTokens() { + return CalculatorEngine.instance.getPostfixFunctionsRegistry().getNames(); + } + }, + unary_operation(500, false, false, "-", "="), binary_operation(600, false, false, "-", "+", "*", "×", "∙", "/", "^") { @Override diff --git a/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java index 590292e5..0f7813f7 100644 --- a/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java +++ b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java @@ -8,12 +8,12 @@ package org.solovyev.android.calculator.model; import android.content.Context; import jscl.math.function.Function; -import jscl.math.function.FunctionsRegistry; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.RClassUtils; import org.solovyev.common.definitions.IBuilder; +import org.solovyev.common.math.MathRegistry; import java.util.List; import java.util.Map; @@ -28,6 +28,13 @@ public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry { @NotNull private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_"; + @NotNull + private final MathRegistry functionsRegistry; + + public AndroidFunctionsRegistryImpl(@NotNull MathRegistry functionsRegistry) { + this.functionsRegistry = functionsRegistry; + } + @Nullable @Override public String getDescription(@NotNull Context context, @NotNull String functionName) { @@ -54,38 +61,38 @@ public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry { @NotNull @Override public List getEntities() { - return FunctionsRegistry.getInstance().getEntities(); + return functionsRegistry.getEntities(); } @NotNull @Override public List getSystemEntities() { - return FunctionsRegistry.getInstance().getSystemEntities(); + return functionsRegistry.getSystemEntities(); } @Override public Function add(@Nullable String name, @NotNull IBuilder IBuilder) { - return FunctionsRegistry.getInstance().add(name, IBuilder); + return functionsRegistry.add(name, IBuilder); } @Override public void remove(@NotNull Function var) { - FunctionsRegistry.getInstance().remove(var); + functionsRegistry.remove(var); } @NotNull @Override public List getNames() { - return FunctionsRegistry.getInstance().getNames(); + return functionsRegistry.getNames(); } @Override public boolean contains(@NotNull String name) { - return FunctionsRegistry.getInstance().contains(name); + return functionsRegistry.contains(name); } @Override public Function get(@NotNull String name) { - return FunctionsRegistry.getInstance().get(name); + return functionsRegistry.get(name); } } 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 3324c35d..8fa8595b 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -7,7 +7,9 @@ package org.solovyev.android.calculator.model; import android.content.Context; import android.content.SharedPreferences; -import jscl.math.Expression; +import jscl.JsclMathEngine; +import jscl.MathEngine; +import jscl.math.operator.Operator; import jscl.text.ParseInterruptedException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -15,6 +17,7 @@ import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.msg.AndroidMessage; import org.solovyev.common.NumberMapper; +import org.solovyev.common.math.MathRegistry; import org.solovyev.common.msg.MessageRegistry; import org.solovyev.common.msg.MessageType; import org.solovyev.common.utils.CollectionsUtils; @@ -57,6 +60,9 @@ public enum CalculatorEngine { private int precision = 5; + @NotNull + private MathEngine engine = new JsclMathEngine(); + @NotNull public final TextProcessor preprocessor = new ToJsclTextProcessor(); @@ -64,7 +70,9 @@ public enum CalculatorEngine { private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl(); @NotNull - private final AndroidFunctionsRegistry functionsRegistry = new AndroidFunctionsRegistryImpl(); + private final AndroidFunctionsRegistry functionsRegistry = new AndroidFunctionsRegistryImpl(engine.getFunctionsRegistry()); + + private final MathRegistry postfixFunctionsRegistry = engine.getPostfixFunctionsRegistry(); @NotNull private final static Set tooLongExecutionCache = new HashSet(); @@ -168,7 +176,7 @@ public enum CalculatorEngine { final String result; if (!tooLongExecutionCache.contains(jsclExpression)) { - final MutableObject calculationResult = new MutableObject(null); + final MutableObject calculationResult = new MutableObject(null); final MutableObject exception = new MutableObject(null); final MutableObject calculationThread = new MutableObject(null); @@ -182,7 +190,7 @@ public enum CalculatorEngine { //Log.d(CalculatorEngine.class.getName(), "Calculation thread started work: " + thread.getName()); //System.out.println(jsclExpression); calculationThread.setObject(thread); - calculationResult.setObject(finalOperation.evaluate(Expression.valueOf(jsclExpression))); + calculationResult.setObject(finalOperation.evaluate(jsclExpression)); } catch (ArithmeticException e) { //System.out.println(e.getMessage()); exception.setObject(new ParseException(e.getMessage(), e)); @@ -297,36 +305,20 @@ public enum CalculatorEngine { return functionsRegistry; } + @NotNull + public MathRegistry getPostfixFunctionsRegistry() { + return postfixFunctionsRegistry; + } + + @NotNull + public MathEngine getEngine() { + return engine; + } + // for tests void setTimeout(int timeout) { this.timeout = timeout; } - - /* private String commands(String str) { - return commands(str, false); - } - - - private void exec(String str) throws EvalError { - interpreter.eval(str); - } - - private String eval(String str) throws EvalError { - return interpreter.eval(commands(str)).toString(); - } - - private String commands(String str, boolean found) { - for (int i = 0; i < cmds.length; i++) { - int n = str.length() - cmds[i].length() - 1; - if (n >= 0 && (" " + cmds[i].toLowerCase()).equals(str.substring(n))) - return commands(str.substring(0, n), true) + "." + cmds[i] + "()"; - } - str = str.replaceAll("\n", ""); - return found ? "jscl.math.Expression.valueOf(\"" + str + "\")" : str; - } - - private static final String cmds[] = new String[]{"expand", "factorize", "elementary", "simplify", "numeric", "toMathML", "toJava"};*/ - // for tests only void setThreadKiller(@NotNull ThreadKiller threadKiller) { this.threadKiller = threadKiller;