09.仿简道云公式函数实战-逻辑函数-IFS

1. IFS函数

IFS 函数可用于判断是否满足一个或多个条件,且返回符合第一个 true 条件的值。 IFS 公式可以取代多个嵌套 IF 语句,并且有多个条件时更方便阅读。

2. 函数用法

IFS(logical_test1, value_if_true1, logical_test2, value_if_true2, … ,logical_testn, value_if_truen)

其中各参数的含义如下:

  • logical_test1:必需,计算结果为 true 或 false 的条件;
  • value_if_true1:必需,当 logical_test1 的计算结果为 true 时要返回结果,可以为空;
  • logical_test2…logical_testn:非必需,计算结果为 true 或 false 的条件;
  • value_if_true2…value_if_truen:非必需,当 logical_testn 的计算结果为 true 时要返回结果。 每个 value_if_truen 对应于一个条件 logical_testn,可以为空。

3. 函数示例

IFS(A1,B1,A2,B2,A3,B3…),表示满足条件 A1 时,返回结果 B1;满足条件 A2 时,返回结果 B2;满足条件 A3 时,返回结果 B3。依次类推。

如,可通过 IFS 函数来计算成绩的等级,设置公式为

IFS(成绩<60,"不及格",成绩<=79,"及格",成绩<=89,"良好",成绩>=90,"优秀")

4. 代码实战

首先我们在function包下创建logic包,在logic包下创建IfsFunction类,代码如下:

package com.ql.util.express.self.combat.function.logic;

import com.ql.util.express.ArraySwap;
import com.ql.util.express.InstructionSetContext;
import com.ql.util.express.OperateData;
import com.ql.util.express.instruction.op.OperatorBase;
import com.ql.util.express.self.combat.exception.FormulaException;

/**
 * 类描述:IFS公式函数
 *
 * @author admin
 * @version 1.0.0
 * @date 2023/11/21 16:59
 */
public class IfsFunction extends OperatorBase {

    public IfsFunction(String name) {
        this.name = name;
    }

    @Override
    public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
        if (list.length < 2) {
            throw new FormulaException("\"" + this.aliasName + "\"操作至少要两个操作数");
        }
        OperateData rst = null;
        for (int i=0;i<list.length-1;i+=2) {
            object obj = list.get(i).getobject(parent);
            if (obj == null) {
                String msg = "\"" + this.aliasName + "\"的判断条件不能为空";
                throw new FormulaException(msg);
            } else if (!(obj instanceof Boolean)) {
                String msg = "\"" + this.aliasName + "\"的判断条件 必须是 Boolean,不能是:";
                throw new FormulaException(msg + obj.getClass().getName());
            } else {
                if ((Boolean)obj) {
                    rst =  list.get(i+1);
                    break;
                }
            }
        }
        return rst;
    }
}

把IfsFunction类注册到公式函数入口类中,代码如下:

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.AndFunction;
import com.ql.util.express.self.combat.function.logic.IfFunction;
import com.ql.util.express.self.combat.function.logic.IfsFunction;

/**
 * 类描述: 仿简道云公式函数实战入口类
 *
 * @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() {

        // AND函数
        this.addFunction("AND",new AndFunction("AND"));

        // IF函数
        this.addFunction("IF",new IfFunction("IF"));

        // IFS函数
        this.addFunction("IFS",new IfsFunction("IFS"));
    }
}

创建测试用例:

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 IFS() throws Exception{

        FormulaRunner formulaRunner = new FormulaRunner(true,true);
        // 创建上下文
        DefaultContext<String, object> context = new DefaultContext<>();
        String express = "IFS(成绩<60,\"不及格\",成绩<=79,\"及格\",成绩<=89,\"良好\",成绩>=90,\"优秀\")";
        context.put("成绩",119);
        object object = formulaRunner.execute(express, context, null, true, true);
        System.out.println(object);

    }

}

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值