Browse Source

解析公式相关的API

master
richie 5 years ago
parent
commit
ab073473fd
  1. 10
      src/main/java/com/fanruan/api/cal/FormulaKit.java
  2. 22
      src/main/java/com/fanruan/api/cal/ParameterKit.java
  3. 27
      src/test/java/com/fanruan/api/cal/ParameterKitTest.java
  4. 2
      src/test/java/com/fanruan/api/session/SessionKitTest.java

10
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);
}
}

22
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);
}
}

27
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<String, Object> 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);
}
}

2
src/test/java/com/fanruan/api/session/SessionFactoryTest.java → 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 {
Loading…
Cancel
Save