Browse Source

公式计算

master
richie 5 years ago
parent
commit
7f19b66df9
  1. 41
      src/main/java/com/fanruan/api/cal/FormulaKit.java
  2. 21
      src/main/java/com/fanruan/api/err/KitError.java
  3. 46
      src/test/java/com/fanruan/api/cal/FormulaKitTest.java
  4. 22
      src/test/java/com/fanruan/api/data/TableDataKitTest.java

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

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

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

22
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() {
}
}
Loading…
Cancel
Save