13.仿简道云公式函数实战-逻辑函数-NOT

1. NOT函数

NOT 函数可用于对其参数的逻辑求反,当逻辑为 true 时,返回结果 false;当逻辑为 false 时,返回结果 true。

2. 函数用法

NOT(logical)

3. 函数示例

1)NOT(A),表示如果 A 为 true 时,则返回 false;A 为 false 时,则返回 true。例如:

  • NOT(50<60),返回 false;

  • NOT(90<60),返回 true。

2)NOT 函数可与 IF 函数等组合使用,如成绩判定时,当成绩不低于 60 分为及格,否则为不及格,则设置公式为:IF(NOT(成绩<60),"及格","不及格")

4. 代码实战

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

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

import com.ql.util.express.Operator;
import com.ql.util.express.self.combat.exception.FormulaException;

/**
 * 类描述: NOT函数
 *
 * @author admin
 * @version 1.0.0
 * @date 2023/11/23 8:31
 */
public class NotFunction extends Operator {
    public NotFunction(String name) {
        this.name = name;
    }

    public NotFunction(String aliasName, String name, String errorInfo) {
        this.name = name;
        this.aliasName = aliasName;
        this.errorInfo = errorInfo;
    }

    public Object executeInner(Object[] list) throws Exception {
        return this.executeInner(list[0]);
    }

    public Object executeInner(Object op) throws Exception {
        if (op == null) {
            throw new FormulaException("null 不能执行操作:" + this.getAliasName());
        } else if (Boolean.class.equals(op.getClass())) {
            Object result = Boolean.valueOf(!((Boolean) op).booleanValue());
            return result;
        } else {
            String msg = "没有定义类型" + op.getClass().getName() + " 的 " + this.name + "操作";
            throw new FormulaException(msg);
        }
    }
}

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

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.*;

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

        // 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"));
    }
}

创建测试用例

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

        FormulaRunner formulaRunner = new FormulaRunner(true,true);
        // 创建上下文
        DefaultContext context = new DefaultContext<>();
        String express = "IF(NOT(成绩<60),\"及格\",\"不及格\")";
        context.put("成绩",79);
        Object object = formulaRunner.execute(express, context, null, true, true);
        System.out.println(object);

    }

}

运行结果

13.仿简道云公式函数实战-逻辑函数-NOT_第1张图片

你可能感兴趣的:(仿简道云公式函数实战,java,算法,数据结构,大数据,数据仓库,后端,中间件)