From bf1834cdcc1577902aa9df0f8af038687efc2cf9 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 12 Mar 2017 14:21:05 -0400 Subject: [PATCH] progress #369 --- .../soft/javaluator/DoubleEvaluator.java | 36 +++++++++---------- .../com/fathzer/soft/javaluator/Function.java | 27 +++++--------- .../src/com/rusefi/test/ParserTest.java | 25 +++++++++---- java_console/ui/src/com/rusefi/Launcher.java | 2 +- 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java b/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java index efc88743f6..9529b303f5 100644 --- a/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java +++ b/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java @@ -101,13 +101,13 @@ public class DoubleEvaluator extends AbstractEvaluator { 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 { 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 { 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)) { diff --git a/java_console/models/src/com/fathzer/soft/javaluator/Function.java b/java_console/models/src/com/fathzer/soft/javaluator/Function.java index 7995c25ffb..e72160a232 100644 --- a/java_console/models/src/com/fathzer/soft/javaluator/Function.java +++ b/java_console/models/src/com/fathzer/soft/javaluator/Function.java @@ -5,38 +5,27 @@ package com.fathzer.soft.javaluator; * @see License information */ public class Function { - private String name; - private int minArgumentCount; - private int maxArgumentCount; - - /** Constructor. - *
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. *
This constructor builds a function with a variable arguments count. *
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. diff --git a/java_console/models/src/com/rusefi/test/ParserTest.java b/java_console/models/src/com/rusefi/test/ParserTest.java index e2bebbbbff..2a0bba5cd4 100644 --- a/java_console/models/src/com/rusefi/test/ParserTest.java +++ b/java_console/models/src/com/rusefi/test/ParserTest.java @@ -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); } diff --git a/java_console/ui/src/com/rusefi/Launcher.java b/java_console/ui/src/com/rusefi/Launcher.java index 18b8df4a44..e29d2ff86c 100644 --- a/java_console/ui/src/com/rusefi/Launcher.java +++ b/java_console/ui/src/com/rusefi/Launcher.java @@ -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";