postfix functions registry
This commit is contained in:
parent
4312f7c475
commit
a25b7f5b8a
@ -6,8 +6,9 @@
|
|||||||
package org.solovyev.android.calculator.jscl;
|
package org.solovyev.android.calculator.jscl;
|
||||||
|
|
||||||
|
|
||||||
import jscl.math.Expression;
|
import jscl.text.ParseException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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.DummyTextProcessor;
|
||||||
import org.solovyev.android.calculator.model.FromJsclSimplifyTextProcessor;
|
import org.solovyev.android.calculator.model.FromJsclSimplifyTextProcessor;
|
||||||
import org.solovyev.android.calculator.model.TextProcessor;
|
import org.solovyev.android.calculator.model.TextProcessor;
|
||||||
@ -17,16 +18,16 @@ public enum JsclOperation {
|
|||||||
simplify(new FromJsclSimplifyTextProcessor()) {
|
simplify(new FromJsclSimplifyTextProcessor()) {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String evaluate(@NotNull Expression expression) {
|
public String evaluate(@NotNull String expression) throws ParseException {
|
||||||
return expression.simplify().toString();
|
return CalculatorEngine.instance.getEngine().simplify(expression);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
elementary(DummyTextProcessor.instance) {
|
elementary(DummyTextProcessor.instance) {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String evaluate(@NotNull Expression expression) {
|
public String evaluate(@NotNull String expression) throws ParseException {
|
||||||
return expression.elementary().toString();
|
return CalculatorEngine.instance.getEngine().elementary(expression);
|
||||||
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -34,8 +35,8 @@ public enum JsclOperation {
|
|||||||
numeric(new FromJsclNumericTextProcessor()) {
|
numeric(new FromJsclNumericTextProcessor()) {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String evaluate(@NotNull Expression expression) {
|
public String evaluate(@NotNull String expression) throws ParseException {
|
||||||
return expression.numeric().toString();
|
return CalculatorEngine.instance.getEngine().evaluate(expression);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -52,5 +53,5 @@ public enum JsclOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract String evaluate(@NotNull Expression expression);
|
public abstract String evaluate(@NotNull String expression) throws ParseException;
|
||||||
}
|
}
|
||||||
|
@ -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<String> allPostfix = Arrays.asList(FACTORIAL, DEGREE);
|
|
||||||
}
|
|
@ -49,7 +49,14 @@ public enum MathType {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
postfix_function(400, false, true, Functions.allPostfix),
|
postfix_function(400, false, true) {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<String> getTokens() {
|
||||||
|
return CalculatorEngine.instance.getPostfixFunctionsRegistry().getNames();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
unary_operation(500, false, false, "-", "="),
|
unary_operation(500, false, false, "-", "="),
|
||||||
binary_operation(600, false, false, "-", "+", "*", "×", "∙", "/", "^") {
|
binary_operation(600, false, false, "-", "+", "*", "×", "∙", "/", "^") {
|
||||||
@Override
|
@Override
|
||||||
|
@ -8,12 +8,12 @@ package org.solovyev.android.calculator.model;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import jscl.math.function.Function;
|
import jscl.math.function.Function;
|
||||||
import jscl.math.function.FunctionsRegistry;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.calculator.R;
|
import org.solovyev.android.calculator.R;
|
||||||
import org.solovyev.android.calculator.RClassUtils;
|
import org.solovyev.android.calculator.RClassUtils;
|
||||||
import org.solovyev.common.definitions.IBuilder;
|
import org.solovyev.common.definitions.IBuilder;
|
||||||
|
import org.solovyev.common.math.MathRegistry;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -28,6 +28,13 @@ public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
|
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private final MathRegistry<Function> functionsRegistry;
|
||||||
|
|
||||||
|
public AndroidFunctionsRegistryImpl(@NotNull MathRegistry<Function> functionsRegistry) {
|
||||||
|
this.functionsRegistry = functionsRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public String getDescription(@NotNull Context context, @NotNull String functionName) {
|
public String getDescription(@NotNull Context context, @NotNull String functionName) {
|
||||||
@ -54,38 +61,38 @@ public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public List<Function> getEntities() {
|
public List<Function> getEntities() {
|
||||||
return FunctionsRegistry.getInstance().getEntities();
|
return functionsRegistry.getEntities();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public List<Function> getSystemEntities() {
|
public List<Function> getSystemEntities() {
|
||||||
return FunctionsRegistry.getInstance().getSystemEntities();
|
return functionsRegistry.getSystemEntities();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Function add(@Nullable String name, @NotNull IBuilder<Function> IBuilder) {
|
public Function add(@Nullable String name, @NotNull IBuilder<Function> IBuilder) {
|
||||||
return FunctionsRegistry.getInstance().add(name, IBuilder);
|
return functionsRegistry.add(name, IBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(@NotNull Function var) {
|
public void remove(@NotNull Function var) {
|
||||||
FunctionsRegistry.getInstance().remove(var);
|
functionsRegistry.remove(var);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public List<String> getNames() {
|
public List<String> getNames() {
|
||||||
return FunctionsRegistry.getInstance().getNames();
|
return functionsRegistry.getNames();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(@NotNull String name) {
|
public boolean contains(@NotNull String name) {
|
||||||
return FunctionsRegistry.getInstance().contains(name);
|
return functionsRegistry.contains(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Function get(@NotNull String name) {
|
public Function get(@NotNull String name) {
|
||||||
return FunctionsRegistry.getInstance().get(name);
|
return functionsRegistry.get(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,9 @@ package org.solovyev.android.calculator.model;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import jscl.math.Expression;
|
import jscl.JsclMathEngine;
|
||||||
|
import jscl.MathEngine;
|
||||||
|
import jscl.math.operator.Operator;
|
||||||
import jscl.text.ParseInterruptedException;
|
import jscl.text.ParseInterruptedException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
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.calculator.jscl.JsclOperation;
|
||||||
import org.solovyev.android.msg.AndroidMessage;
|
import org.solovyev.android.msg.AndroidMessage;
|
||||||
import org.solovyev.common.NumberMapper;
|
import org.solovyev.common.NumberMapper;
|
||||||
|
import org.solovyev.common.math.MathRegistry;
|
||||||
import org.solovyev.common.msg.MessageRegistry;
|
import org.solovyev.common.msg.MessageRegistry;
|
||||||
import org.solovyev.common.msg.MessageType;
|
import org.solovyev.common.msg.MessageType;
|
||||||
import org.solovyev.common.utils.CollectionsUtils;
|
import org.solovyev.common.utils.CollectionsUtils;
|
||||||
@ -57,6 +60,9 @@ public enum CalculatorEngine {
|
|||||||
|
|
||||||
private int precision = 5;
|
private int precision = 5;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private MathEngine engine = new JsclMathEngine();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public final TextProcessor<PreparedExpression> preprocessor = new ToJsclTextProcessor();
|
public final TextProcessor<PreparedExpression> preprocessor = new ToJsclTextProcessor();
|
||||||
|
|
||||||
@ -64,7 +70,9 @@ public enum CalculatorEngine {
|
|||||||
private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl();
|
private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final AndroidFunctionsRegistry functionsRegistry = new AndroidFunctionsRegistryImpl();
|
private final AndroidFunctionsRegistry functionsRegistry = new AndroidFunctionsRegistryImpl(engine.getFunctionsRegistry());
|
||||||
|
|
||||||
|
private final MathRegistry<Operator> postfixFunctionsRegistry = engine.getPostfixFunctionsRegistry();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final static Set<String> tooLongExecutionCache = new HashSet<String>();
|
private final static Set<String> tooLongExecutionCache = new HashSet<String>();
|
||||||
@ -168,7 +176,7 @@ public enum CalculatorEngine {
|
|||||||
|
|
||||||
final String result;
|
final String result;
|
||||||
if (!tooLongExecutionCache.contains(jsclExpression)) {
|
if (!tooLongExecutionCache.contains(jsclExpression)) {
|
||||||
final MutableObject<Object> calculationResult = new MutableObject<Object>(null);
|
final MutableObject<String> calculationResult = new MutableObject<String>(null);
|
||||||
final MutableObject<ParseException> exception = new MutableObject<ParseException>(null);
|
final MutableObject<ParseException> exception = new MutableObject<ParseException>(null);
|
||||||
final MutableObject<Thread> calculationThread = new MutableObject<Thread>(null);
|
final MutableObject<Thread> calculationThread = new MutableObject<Thread>(null);
|
||||||
|
|
||||||
@ -182,7 +190,7 @@ public enum CalculatorEngine {
|
|||||||
//Log.d(CalculatorEngine.class.getName(), "Calculation thread started work: " + thread.getName());
|
//Log.d(CalculatorEngine.class.getName(), "Calculation thread started work: " + thread.getName());
|
||||||
//System.out.println(jsclExpression);
|
//System.out.println(jsclExpression);
|
||||||
calculationThread.setObject(thread);
|
calculationThread.setObject(thread);
|
||||||
calculationResult.setObject(finalOperation.evaluate(Expression.valueOf(jsclExpression)));
|
calculationResult.setObject(finalOperation.evaluate(jsclExpression));
|
||||||
} catch (ArithmeticException e) {
|
} catch (ArithmeticException e) {
|
||||||
//System.out.println(e.getMessage());
|
//System.out.println(e.getMessage());
|
||||||
exception.setObject(new ParseException(e.getMessage(), e));
|
exception.setObject(new ParseException(e.getMessage(), e));
|
||||||
@ -297,36 +305,20 @@ public enum CalculatorEngine {
|
|||||||
return functionsRegistry;
|
return functionsRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public MathRegistry<Operator> getPostfixFunctionsRegistry() {
|
||||||
|
return postfixFunctionsRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public MathEngine getEngine() {
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
// for tests
|
// for tests
|
||||||
void setTimeout(int timeout) {
|
void setTimeout(int timeout) {
|
||||||
this.timeout = 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
|
// for tests only
|
||||||
void setThreadKiller(@NotNull ThreadKiller threadKiller) {
|
void setThreadKiller(@NotNull ThreadKiller threadKiller) {
|
||||||
this.threadKiller = threadKiller;
|
this.threadKiller = threadKiller;
|
||||||
|
Loading…
Reference in New Issue
Block a user