From 7f19b66df9ddf2183ced6ff512336e9d656cf320 Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 15 Aug 2019 19:02:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=BC=8F=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fanruan/api/cal/FormulaKit.java | 41 +++++++++++++++++ .../java/com/fanruan/api/err/KitError.java | 21 +++++++++ .../com/fanruan/api/cal/FormulaKitTest.java | 46 +++++++++++++++++++ .../fanruan/api/data/TableDataKitTest.java | 22 +++++++++ 4 files changed, 130 insertions(+) create mode 100644 src/main/java/com/fanruan/api/cal/FormulaKit.java create mode 100644 src/main/java/com/fanruan/api/err/KitError.java create mode 100644 src/test/java/com/fanruan/api/cal/FormulaKitTest.java create mode 100644 src/test/java/com/fanruan/api/data/TableDataKitTest.java diff --git a/src/main/java/com/fanruan/api/cal/FormulaKit.java b/src/main/java/com/fanruan/api/cal/FormulaKit.java new file mode 100644 index 0000000..4a3096e --- /dev/null +++ b/src/main/java/com/fanruan/api/cal/FormulaKit.java @@ -0,0 +1,41 @@ +package com.fanruan.api.cal; + +import com.fanruan.api.err.KitError; +import com.fr.base.BaseFormula; +import com.fr.script.Calculator; +import com.fr.stable.UtilEvalError; +import com.fr.stable.script.CalculatorProvider; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-15 + * 公式计算相关的工具类 + */ +public class FormulaKit { + + /** + * 计算公式的值,会新建一个算子对象来计算该公式 + * @param formula 公式内容 + * @return 公式计算后的结果值 + * @throws KitError 如果计算过程中出现错误,则抛出此异常 + */ + public static Object eval(String formula) throws KitError { + return eval(Calculator.createCalculator(), formula); + } + + /** + * 计算公式的值 + * @param calculator 自定义算子 + * @param formula 公式内容 + * @return 公式计算后的结果值 + * @throws KitError 如果计算过程中出现错误,则抛出此异常 + */ + public static Object eval(CalculatorProvider calculator, String formula) throws KitError { + try { + return BaseFormula.createFormulaBuilder().build(formula).eval(calculator); + } catch (UtilEvalError u) { + throw new KitError(u); + } + } +} diff --git a/src/main/java/com/fanruan/api/err/KitError.java b/src/main/java/com/fanruan/api/err/KitError.java new file mode 100644 index 0000000..eb2a57e --- /dev/null +++ b/src/main/java/com/fanruan/api/err/KitError.java @@ -0,0 +1,21 @@ +package com.fanruan.api.err; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-15 + */ +public class KitError extends Exception { + + public KitError() { + super(); + } + + public KitError(String message) { + super(message); + } + + public KitError(Throwable throwable) { + super(throwable); + } +} diff --git a/src/test/java/com/fanruan/api/cal/FormulaKitTest.java b/src/test/java/com/fanruan/api/cal/FormulaKitTest.java new file mode 100644 index 0000000..17c96f8 --- /dev/null +++ b/src/test/java/com/fanruan/api/cal/FormulaKitTest.java @@ -0,0 +1,46 @@ +package com.fanruan.api.cal; + +import com.fanruan.api.Prepare; +import com.fanruan.api.err.KitError; +import com.fr.base.ParameterMapNameSpace; +import com.fr.script.Calculator; +import com.fr.stable.script.CalculatorProvider; +import com.fr.stable.script.NameSpace; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-15 + */ +public class FormulaKitTest extends Prepare { + + @Test + public void eval() { + try { + Assert.assertEquals(4, FormulaKit.eval("=(6+6)/3")); + } catch (KitError kitError) { + Assert.fail(); + } + } + + @Test + public void testEval() { + CalculatorProvider calculator = Calculator.createCalculator(); + Map map = new HashMap<>(); + map.put("a", 1); + map.put("b", 2); + calculator.pushNameSpace(ParameterMapNameSpace.create(map)); + try { + Assert.assertEquals(1, FormulaKit.eval(calculator,"=($a+$b)/3")); + } catch (KitError kitError) { + Assert.fail(); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/data/TableDataKitTest.java b/src/test/java/com/fanruan/api/data/TableDataKitTest.java new file mode 100644 index 0000000..78dd869 --- /dev/null +++ b/src/test/java/com/fanruan/api/data/TableDataKitTest.java @@ -0,0 +1,22 @@ +package com.fanruan.api.data; + +import com.fanruan.api.Prepare; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-15 + */ +public class TableDataKitTest extends Prepare { + + @Test + public void getTableData() { + } + + @Test + public void testGetTableData() { + } +} \ No newline at end of file