This commit is contained in:
rusefi 2017-03-12 14:21:05 -04:00
parent b776e3fd94
commit bf1834cdcc
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);
/** 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) */
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) */
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) */
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 */
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};
/** 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};
/** The whole set of predefined constants */
private static final Constant[] CONSTANTS = new Constant[]{TRUE, FALSE};
@ -343,19 +343,19 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
while (arguments.hasNext()) {
result = Math.max(result, arguments.next());
}
} else if (SUM.equals(function)) {
result = 0.;
while (arguments.hasNext()) {
result = result + arguments.next();
}
} else if (AVERAGE.equals(function)) {
result = 0.;
int nb = 0;
while (arguments.hasNext()) {
result = result + arguments.next();
nb++;
}
result = result/nb;
// } else if (SUM.equals(function)) {
// result = 0.;
// while (arguments.hasNext()) {
// result = result + arguments.next();
// }
// } else if (AVERAGE.equals(function)) {
// result = 0.;
// int nb = 0;
// while (arguments.hasNext()) {
// result = result + arguments.next();
// nb++;
// }
// result = result/nb;
} else if (LN.equals(function)) {
result = Math.log(arguments.next());
} else if (LOG.equals(function)) {

View File

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

View File

@ -8,6 +8,7 @@ import java.text.ParseException;
import java.util.Stack;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @see DoubleEvaluator
@ -17,7 +18,15 @@ public class ParserTest {
@Test
public void testFunctionParameters() {
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?
assertParseB("0 fsio_setting", "fsio_setting(0)");
@ -42,10 +51,10 @@ public class ParserTest {
private void assertParseB(String rpn, String expression) {
assertParse(rpn, expression);
String h = getInfix(rpn);
System.out.println(h);
String infix = getInfix(rpn);
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) {
@ -70,8 +79,10 @@ public class ParserTest {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pCount; i++) {
if (i != 0)
sb.append(", ");
sb.append(stack.pop());
sb.insert(0, ", ");
if (stack.isEmpty())
throw new IllegalStateException("While getting " + pCount + "parameters for " + token);
sb.insert(0, stack.pop());
}
stack.push(token + "(" + sb + ")");
@ -108,7 +119,7 @@ public class ParserTest {
* do we even need to and/or should to support unary minus?
*
* http://stackoverflow.com/questions/20246787/handling-unary-minus-for-shunting-yard-algorithm
*/
*/
assertValue("3 negate", "negate3", -3);
assertValue("2 3 6 ^ negate +", "2+negate 3^6", -727.0);
}

View File

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