forked from fanruan/finekit
richie
5 years ago
4 changed files with 130 additions and 0 deletions
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
} |
||||
} |
@ -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…
Reference in new issue