From ab073473fd1c87d221451206e6e69da66a129678 Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 16 Aug 2019 09:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=85=AC=E5=BC=8F=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fanruan/api/cal/FormulaKit.java | 10 +++++++ .../com/fanruan/api/cal/ParameterKit.java | 22 +++++++++++++++ .../com/fanruan/api/cal/ParameterKitTest.java | 27 +++++++++++++++++++ ...onFactoryTest.java => SessionKitTest.java} | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/fanruan/api/cal/ParameterKit.java create mode 100644 src/test/java/com/fanruan/api/cal/ParameterKitTest.java rename src/test/java/com/fanruan/api/session/{SessionFactoryTest.java => SessionKitTest.java} (86%) diff --git a/src/main/java/com/fanruan/api/cal/FormulaKit.java b/src/main/java/com/fanruan/api/cal/FormulaKit.java index 4a3096e..fe86a9d 100644 --- a/src/main/java/com/fanruan/api/cal/FormulaKit.java +++ b/src/main/java/com/fanruan/api/cal/FormulaKit.java @@ -3,6 +3,7 @@ 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.FormulaProvider; import com.fr.stable.UtilEvalError; import com.fr.stable.script.CalculatorProvider; @@ -38,4 +39,13 @@ public class FormulaKit { throw new KitError(u); } } + + /** + * 生成公式对象 + * @param content 公式的内容 + * @return 公式对象 + */ + public static FormulaProvider createFormula(Object content) { + return BaseFormula.createFormulaBuilder().build(content); + } } diff --git a/src/main/java/com/fanruan/api/cal/ParameterKit.java b/src/main/java/com/fanruan/api/cal/ParameterKit.java new file mode 100644 index 0000000..4bc9820 --- /dev/null +++ b/src/main/java/com/fanruan/api/cal/ParameterKit.java @@ -0,0 +1,22 @@ +package com.fanruan.api.cal; + +import com.fr.base.ParameterHelper; +import com.fr.stable.ParameterProvider; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-16 + * 参数相关的工具类 + */ +public class ParameterKit { + + /** + * 分析公式中所携带的参数 + * @param text 公式内容 + * @return 参数数组 + */ + public static ParameterProvider[] analyze4ParametersFromFormula(String text) { + return ParameterHelper.analyze4ParametersFromFormula(text); + } +} diff --git a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java new file mode 100644 index 0000000..e37126c --- /dev/null +++ b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java @@ -0,0 +1,27 @@ +package com.fanruan.api.cal; + +import com.fanruan.api.Prepare; +import com.fr.stable.ParameterProvider; +import org.junit.Assert; +import org.junit.Test; + +import java.util.TreeMap; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-16 + */ +public class ParameterKitTest extends Prepare { + + @Test + public void analyze4ParametersFromFormula() { + ParameterProvider[] providers = ParameterKit.analyze4ParametersFromFormula("=sum($p1, $p2) + max($p4 - 1, pow(2, $p5))"); + TreeMap map = new TreeMap<>(); + for (ParameterProvider provider : providers) { + map.put(provider.getName(), provider.getValue()); + } + String[] names = map.keySet().toArray(new String[0]); + Assert.assertArrayEquals(new String[]{"p1", "p2", "p4", "p5"}, names); + } +} \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/session/SessionFactoryTest.java b/src/test/java/com/fanruan/api/session/SessionKitTest.java similarity index 86% rename from src/test/java/com/fanruan/api/session/SessionFactoryTest.java rename to src/test/java/com/fanruan/api/session/SessionKitTest.java index 2a6b71b..3e486cb 100644 --- a/src/test/java/com/fanruan/api/session/SessionFactoryTest.java +++ b/src/test/java/com/fanruan/api/session/SessionKitTest.java @@ -9,7 +9,7 @@ import org.junit.Test; * @version 10.0 * Created by richie on 2019-08-09 */ -public class SessionFactoryTest extends Prepare { +public class SessionKitTest extends Prepare { @Test public void getSession() throws Exception {