11.仿简道云公式函数实战-逻辑函数-TRUE

172 阅读3分钟

1. TRUE函数

TRUE 函数可直接返回逻辑值 true。

2. 函数用法

TRUE()

3. 函数示例

TRUE 函数一般不会作为函数单独使用,可与其他函数一起使用,或作为判断逻辑的结果。如,判断字段值是否为空时,设置公式为IF(ISEMPTY(方案选择)==TRUE(),"未选择","已选择"),为空时值为TRUE(),即返回“未选择”,反之返回“已选择”。

4. 代码实战

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

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

import com.ql.util.express.Operator;

/**
* 类描述: TRUE函数
*
*  @author admin
*  @version  1.0.0
*  @date  2023/11/21 17:30
*/
public  class TrueFunction extends Operator {

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

    @Override
    public Object executeInner(Object[] objects) throws Exception {
        return Boolean.TRUE;
    }
}

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

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

创建测试用例

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

        FormulaRunner formulaRunner = new FormulaRunner(true,true);
        // 创建上下文
DefaultContext<String, Object> context = new DefaultContext<>();
        String express = "IF(1<2,TRUE(),"已选择")" ;
        Object object = formulaRunner.execute(express, context, null, true, true);
        System.out.println(object);

    }

}

运行日志

[2024-02-06 09:08:36,995] [main] (ExpressParse.java:340) DEBUG com.ql.util.express.parse.ExpressParse - 执行的表达式:IF(1<2,TRUE(),"已选择")
[2024-02-06 09:08:36,998] [main] (ExpressParse.java:341) DEBUG com.ql.util.express.parse.ExpressParse - 单词分解结果:{IF},{(},{1},{<},{2},{,},{TRUE},{(},{)},{,},{"已选择"},{)}
[2024-02-06 09:08:36,998] [main] (ExpressParse.java:345) DEBUG com.ql.util.express.parse.ExpressParse - 预处理后结果:{IF},{(},{1},{<},{2},{,},{TRUE},{(},{)},{,},{"已选择"},{)}
[2024-02-06 09:08:37,000] [main] (ExpressParse.java:370) DEBUG com.ql.util.express.parse.ExpressParse - 单词分析结果:IF:FUNCTION_NAME,(:(,1:CONST_INTEGER,<:<,2:CONST_INTEGER,,:,,TRUE:FUNCTION_NAME,(:(,):),,:,,已选择:CONST_STRING,):)
[2024-02-06 09:08:37,003] [main] (ExpressParse.java:422) DEBUG com.ql.util.express.parse.ExpressParse - 最后的语法树:
1:   STAT_BLOCK:STAT_BLOCK                                                         	STAT_BLOCK
2:      STAT_SEMICOLON:STAT_SEMICOLON	STAT_SEMICOLON
3:         FUNCTION_CALL:FUNCTION_CALL	FUNCTION_CALL
4:            IF:CONST_STRING	CONST
4:            <:<	<
5:               1:CONST_INTEGER	CONST
5:               2:CONST_INTEGER	CONST
4:            FUNCTION_CALL:FUNCTION_CALL	FUNCTION_CALL
5:               TRUE:CONST_STRING	CONST
4:            已选择:CONST_STRING	CONST

[2024-02-06 09:08:37,006] [main] (ExpressRunner.java:730) DEBUG com.ql.util.express.ExpressRunner - 
1:LoadData 1
2:LoadData 2
3:OP : < OPNUMBER[2]
4:OP : TRUE OPNUMBER[0]
5:LoadData 已选择
6:OP : IF OPNUMBER[3]

[2024-02-06 09:08:37,012] [main] (InstructionConstData.java:26) DEBUG com.ql.util.express.instruction.detail.Instruction - LoadData 1
[2024-02-06 09:08:37,012] [main] (InstructionConstData.java:26) DEBUG com.ql.util.express.instruction.detail.Instruction - LoadData 2
[2024-02-06 09:08:37,012] [main] (InstructionOperator.java:46) DEBUG com.ql.util.express.instruction.detail.Instruction - <(1,2)
[2024-02-06 09:08:37,015] [main] (InstructionOperator.java:46) DEBUG com.ql.util.express.instruction.detail.Instruction - TRUE()
[2024-02-06 09:08:37,015] [main] (InstructionConstData.java:26) DEBUG com.ql.util.express.instruction.detail.Instruction - LoadData 已选择
[2024-02-06 09:08:37,015] [main] (InstructionOperator.java:46) DEBUG com.ql.util.express.instruction.detail.Instruction - IF(true,true,已选择)
true