postfix functions registry

This commit is contained in:
serso 2011-11-01 12:31:38 +04:00
parent 80af3d0468
commit b756cb6966
5 changed files with 54 additions and 73 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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, "-", "="),
binary_operation(600, false, false, "-", "+", "*", "×", "", "/", "^") {
@Override

View File

@ -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<Function> functionsRegistry;
public AndroidFunctionsRegistryImpl(@NotNull MathRegistry<Function> 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<Function> getEntities() {
return FunctionsRegistry.getInstance().getEntities();
return functionsRegistry.getEntities();
}
@NotNull
@Override
public List<Function> getSystemEntities() {
return FunctionsRegistry.getInstance().getSystemEntities();
return functionsRegistry.getSystemEntities();
}
@Override
public Function add(@Nullable String name, @NotNull IBuilder<Function> 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<String> 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);
}
}

View File

@ -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<PreparedExpression> 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<Operator> postfixFunctionsRegistry = engine.getPostfixFunctionsRegistry();
@NotNull
private final static Set<String> tooLongExecutionCache = new HashSet<String>();
@ -168,7 +176,7 @@ public enum CalculatorEngine {
final String result;
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<Thread> calculationThread = new MutableObject<Thread>(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<Operator> 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;