This commit is contained in:
rusefi 2017-03-12 14:21:05 -04:00
parent 30a08f7cd6
commit 4cf36013de
4 changed files with 45 additions and 45 deletions

View File

@ -101,13 +101,13 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
public static final Function TANGENTH = new Function("tanh", 1); public static final Function TANGENTH = new Function("tanh", 1);
/** Returns the minimum of n numbers (n>=1) */ /** Returns the minimum of n numbers (n>=1) */
public static final Function MIN = new Function("min", 1, Integer.MAX_VALUE); public static final Function MIN = new Function("min", 2);
/** Returns the maximum of n numbers (n>=1) */ /** Returns the maximum of n numbers (n>=1) */
public static final Function MAX = new Function("max", 1, Integer.MAX_VALUE); public static final Function MAX = new Function("max", 2);
/** Returns the sum of n numbers (n>=1) */ /** Returns the sum of n numbers (n>=1) */
public static final Function SUM = new Function("sum", 1, Integer.MAX_VALUE); // public static final Function SUM = new Function("sum", 2);
/** Returns the average of n numbers (n>=1) */ /** Returns the average of n numbers (n>=1) */
public static final Function AVERAGE = new Function("avg", 1, Integer.MAX_VALUE); // public static final Function AVERAGE = new Function("avg", 2);
/** Returns the natural logarithm of a number */ /** Returns the natural logarithm of a number */
public static final Function LN = new Function("ln", 1); public static final Function LN = new Function("ln", 1);
@ -159,7 +159,7 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
private static final Operator[] OPERATORS = new Operator[]{NEGATE, NOT, NOT2, MORE, MORE_EQ, AND, AND2, OR, OR2, LESS, LESS_EQ, MINUS, PLUS, MULTIPLY, DIVIDE, EXPONENT, MODULO}; private static final Operator[] OPERATORS = new Operator[]{NEGATE, NOT, NOT2, MORE, MORE_EQ, AND, AND2, OR, OR2, LESS, LESS_EQ, MINUS, PLUS, MULTIPLY, DIVIDE, EXPONENT, MODULO};
/** The whole set of predefined functions */ /** The whole set of predefined functions */
private static final Function[] FUNCTIONS = new Function[]{SINE, COSINE, TANGENT, ASINE, ACOSINE, ATAN, SINEH, COSINEH, TANGENTH, MIN, MAX, SUM, AVERAGE, LN, LOG, ROUND, CEIL, FLOOR, ABS, RANDOM, private static final Function[] FUNCTIONS = new Function[]{SINE, COSINE, TANGENT, ASINE, ACOSINE, ATAN, SINEH, COSINEH, TANGENTH, MIN, MAX, LN, LOG, ROUND, CEIL, FLOOR, ABS, RANDOM,
if_function, fsio_setting}; if_function, fsio_setting};
/** The whole set of predefined constants */ /** The whole set of predefined constants */
private static final Constant[] CONSTANTS = new Constant[]{TRUE, FALSE}; private static final Constant[] CONSTANTS = new Constant[]{TRUE, FALSE};
@ -343,19 +343,19 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
while (arguments.hasNext()) { while (arguments.hasNext()) {
result = Math.max(result, arguments.next()); result = Math.max(result, arguments.next());
} }
} else if (SUM.equals(function)) { // } else if (SUM.equals(function)) {
result = 0.; // result = 0.;
while (arguments.hasNext()) { // while (arguments.hasNext()) {
result = result + arguments.next(); // result = result + arguments.next();
} // }
} else if (AVERAGE.equals(function)) { // } else if (AVERAGE.equals(function)) {
result = 0.; // result = 0.;
int nb = 0; // int nb = 0;
while (arguments.hasNext()) { // while (arguments.hasNext()) {
result = result + arguments.next(); // result = result + arguments.next();
nb++; // nb++;
} // }
result = result/nb; // result = result/nb;
} else if (LN.equals(function)) { } else if (LN.equals(function)) {
result = Math.log(arguments.next()); result = Math.log(arguments.next());
} else if (LOG.equals(function)) { } else if (LOG.equals(function)) {

View File

@ -5,38 +5,27 @@ package com.fathzer.soft.javaluator;
* @see <a href="../../../license.html">License information</a> * @see <a href="../../../license.html">License information</a>
*/ */
public class Function { public class Function {
private String name; private final String name;
private int minArgumentCount; private final int minArgumentCount;
private int maxArgumentCount; private final int maxArgumentCount;
/** Constructor.
* <br>This constructor builds a function with a fixed arguments count.
* @param name The function's name
* @param argumentCount The function's argument count.
* @throws IllegalArgumentException if argumentCount is lower than 0 or if the function name is null or empty.
*/
public Function(String name, int argumentCount) {
this(name, argumentCount, argumentCount);
}
/** Constructor. /** Constructor.
* <br>This constructor builds a function with a variable arguments count. * <br>This constructor builds a function with a variable arguments count.
* <br>For instance, a minimum function may have at least one argument. * <br>For instance, a minimum function may have at least one argument.
* @param name The function's name * @param name The function's name
* @param minArgumentCount The function's minimum argument count. * @param argumentCount The function's argument count.
* @param maxArgumentCount The function's maximum argument count (Integer.MAX_VALUE to specify no upper limit).
* @throws IllegalArgumentException if minArgumentCount is less than 0 or greater than maxArgumentCount or if the function name is null or empty. * @throws IllegalArgumentException if minArgumentCount is less than 0 or greater than maxArgumentCount or if the function name is null or empty.
*/ */
public Function(String name, int minArgumentCount, int maxArgumentCount) { public Function(String name, int argumentCount) {
if ((minArgumentCount<0) || (minArgumentCount>maxArgumentCount)) { this.minArgumentCount = argumentCount;
this.maxArgumentCount = argumentCount;
if ((argumentCount<0) || (minArgumentCount>maxArgumentCount)) {
throw new IllegalArgumentException("Invalid argument count"); throw new IllegalArgumentException("Invalid argument count");
} }
if (name==null || name.length()==0) { if (name==null || name.length()==0) {
throw new IllegalArgumentException("Invalid function name"); throw new IllegalArgumentException("Invalid function name");
} }
this.name = name; this.name = name;
this.minArgumentCount = minArgumentCount;
this.maxArgumentCount = maxArgumentCount;
} }
/** Gets the function's name. /** Gets the function's name.

View File

@ -8,6 +8,7 @@ import java.text.ParseException;
import java.util.Stack; import java.util.Stack;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/** /**
* @see DoubleEvaluator * @see DoubleEvaluator
@ -17,7 +18,15 @@ public class ParserTest {
@Test @Test
public void testFunctionParameters() { public void testFunctionParameters() {
assertParseB("3 log", "log(3)"); assertParseB("3 log", "log(3)");
assertParse("1 2 3 if", "if(1, 2, 3)"); assertParseB("1 2 3 if", "if(1, 2, 3)");
assertParseB("1 3 max", "max(1, 3)");
try {
assertParse("3 1 max", "max(1 3)");
fail("Error expected");
} catch (Throwable ignored) {
// expected
}
assertParse("log 3", "log 3 "); // todo: better handling? assertParse("log 3", "log 3 "); // todo: better handling?
assertParseB("0 fsio_setting", "fsio_setting(0)"); assertParseB("0 fsio_setting", "fsio_setting(0)");
@ -42,10 +51,10 @@ public class ParserTest {
private void assertParseB(String rpn, String expression) { private void assertParseB(String rpn, String expression) {
assertParse(rpn, expression); assertParse(rpn, expression);
String h = getInfix(rpn); String infix = getInfix(rpn);
System.out.println(h); System.out.println(infix);
assertEquals(getReplace(expression).toLowerCase(), getReplace(h).toLowerCase()); assertEquals("infix recovery", getReplace(expression).toLowerCase(), getReplace(infix).toLowerCase());
} }
private String getReplace(String h) { private String getReplace(String h) {
@ -70,8 +79,10 @@ public class ParserTest {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < pCount; i++) { for (int i = 0; i < pCount; i++) {
if (i != 0) if (i != 0)
sb.append(", "); sb.insert(0, ", ");
sb.append(stack.pop()); if (stack.isEmpty())
throw new IllegalStateException("While getting " + pCount + "parameters for " + token);
sb.insert(0, stack.pop());
} }
stack.push(token + "(" + sb + ")"); stack.push(token + "(" + sb + ")");

View File

@ -44,7 +44,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
* @see EngineSnifferPanel * @see EngineSnifferPanel
*/ */
public class Launcher { public class Launcher {
public static final int CONSOLE_VERSION = 20170311; public static final int CONSOLE_VERSION = 20170312;
public static final boolean SHOW_STIMULATOR = false; public static final boolean SHOW_STIMULATOR = false;
private static final String TAB_INDEX = "main_tab"; private static final String TAB_INDEX = "main_tab";
protected static final String PORT_KEY = "port"; protected static final String PORT_KEY = "port";