Browse Source

脚本公式API

master
richie 5 years ago
parent
commit
1a52430f4d
  1. 12
      src/main/java/com/fanruan/api/cal/FormulaKit.java
  2. 15
      src/test/java/com/fanruan/api/cal/FormulaKitTest.java

12
src/main/java/com/fanruan/api/cal/FormulaKit.java

@ -1,7 +1,9 @@
package com.fanruan.api.cal;
import com.fanruan.api.err.KitError;
import com.fanruan.api.util.GeneralKit;
import com.fr.base.BaseFormula;
import com.fr.base.ScriptFormula;
import com.fr.log.FineLoggerFactory;
import com.fr.script.Calculator;
import com.fr.stable.FormulaProvider;
@ -58,6 +60,16 @@ public class FormulaKit {
return BaseFormula.createFormulaBuilder().build(content);
}
/**
* 生成脚本公式对象
*
* @param content 公式的内容是一段javascript脚本
* @return 脚本公式
*/
public @NotNull static BaseFormula newScriptFormula(Object content) {
return new ScriptFormula(GeneralKit.objectToString(content));
}
/**
* 检查公式内容合法性
*

15
src/test/java/com/fanruan/api/cal/FormulaKitTest.java

@ -2,9 +2,11 @@ package com.fanruan.api.cal;
import com.fanruan.api.Prepare;
import com.fanruan.api.err.KitError;
import com.fr.base.BaseFormula;
import com.fr.base.Formula;
import com.fr.base.ParameterMapNameSpace;
import com.fr.script.Calculator;
import com.fr.stable.UtilEvalError;
import com.fr.stable.script.CalculatorProvider;
import org.junit.Assert;
import org.junit.Test;
@ -43,6 +45,19 @@ public class FormulaKitTest extends Prepare {
Assert.fail();
}
}
@Test
public void testScriptFormula() {
BaseFormula formula = FormulaKit.newScriptFormula("=return Math.abs(-1)");
CalculatorProvider provider = CalculatorKit.createCalculator();
try {
Object r = formula.evalValue(provider);
Assert.assertEquals(1, r);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testCheckFormulaContent() {
assertEquals(true, FormulaKit.checkFormulaContent(new Formula("")));

Loading…
Cancel
Save