AVERAGE 函数可用于计算一组数值的算术平均值。
AVERAGE(数字1,数字2,...)
AVERAGE(1,3,5),返回结果为 3
首先我们在function包下创建math包,在math包下创建AvgFunction类,代码如下:
package com.ql.util.express.self.combat.function.math;
import com.ql.util.express.Operator;
import com.ql.util.express.OperatorOfNumber;
import com.ql.util.express.self.combat.exception.FormulaException;
/**
* 类描述: AVERAGE函数
*
* @author admin
* @version 1.0.0
* @date 2023/11/23 9:11
*/
public class AvgFunction extends Operator {
public AvgFunction(String name) {
this.name = name;
}
@Override
public Object executeInner(Object[] objects) throws Exception {
// 参数校验
if (objects.length == 0) {
throw new FormulaException("操作数异常");
}
Object result = 0;
int len = objects.length;
for (int i = 0; i < len; i++) {
// isPrecise true 代表高精度计算,false代表普通计算
result = OperatorOfNumber.add(result, objects[i], isPrecise);
}
// 求平均值
result = OperatorOfNumber.divide(result,len,isPrecise);
return result;
}
}
把AvgFunction类注册到公式函数入口类中,代码如下:
package com.ql.util.express.self.combat.ext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressResourceLoader;
import com.ql.util.express.parse.NodeTypeManager;
import com.ql.util.express.self.combat.function.logic.*;
import com.ql.util.express.self.combat.function.math.AbsFunction;
import com.ql.util.express.self.combat.function.math.AvgFunction;
/**
* 类描述: 仿简道云公式函数实战入口类
*
* @author admin
* @version 1.0.0
* @date 2023/11/21 15:29
*/
public class FormulaRunner extends ExpressRunner {
public FormulaRunner() {
super();
}
public FormulaRunner(boolean isPrecise, boolean isTrace) {
super(isPrecise,isTrace);
}
public FormulaRunner(boolean isPrecise, boolean isStrace, NodeTypeManager nodeTypeManager) {
super(isPrecise,isStrace,nodeTypeManager);
}
public FormulaRunner(boolean isPrecise, boolean isTrace, IExpressResourceLoader iExpressResourceLoader, NodeTypeManager nodeTypeManager) {
super(isPrecise,isTrace,iExpressResourceLoader,nodeTypeManager);
}
@Override
public void addSystemFunctions() {
// ExpressRunner 的内部系统函数
super.addSystemFunctions();
// 扩展公式函数
this.customFunction();
}
/***
* 自定义公式函数
*/
public void customFunction() {
// 逻辑公式函数
this.addLogicFunction();
// 数学公式函数
this.addMathFunction();
}
public void addLogicFunction() {
// AND函数
this.addFunction("AND",new AndFunction("AND"));
// IF函数
this.addFunction("IF",new IfFunction("IF"));
// IFS函数
this.addFunction("IFS",new IfsFunction("IFS"));
// XOR函数
this.addFunction("XOR",new XorFunction("XOR"));
// TRUE函数
this.addFunction("TRUE",new TrueFunction("TRUE"));
// FALSE函数
this.addFunction("FALSE",new FalseFunction("FALSE"));
// NOT函数
this.addFunction("NOT",new NotFunction("NOT"));
// OR函数
this.addFunction("OR",new OrFunction("OR"));
}
public void addMathFunction() {
// ABS函数
this.addFunction("ABS",new AbsFunction("ABS"));
// AVERAGE函数
this.addFunction("AVERAGE",new AvgFunction("AVERAGE"));
}
}
创建测试用例
package com.ql.util.express.self.combat;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.self.combat.ext.FormulaRunner;
import org.junit.Test;
/**
* 类描述: 实战测试类
*
* @author admin
* @version 1.0.0
* @date 2023/11/21 15:45
*/
public class CombatTest {
@Test
public void AVERAGE() throws Exception{
FormulaRunner formulaRunner = new FormulaRunner(true,true);
// 创建上下文
DefaultContext<String, Object> context = new DefaultContext<>();
String express = "AVERAGE(Integer.MAX_VALUE*2,Integer.MAX_VALUE*2,Integer.MAX_VALUE*2)";
Object object = formulaRunner.execute(express, context, null, true, true);
System.out.println(object);
}
}
运行结果