FALSE 函数可直接返回逻辑值 false。
FALSE()
FALSE 函数一般不会作为公式单独使用,可与其他公式一起使用,或作为判断逻辑的结果。如,仓储管理中,判断库存数量与盘点数量是否一致时,可设置公式为IF(库存数量==库存盘点数量,TRUE(),FALSE())
,即数量一致时返回 true,反之返回 false。
首先我们在function包下创建logic包,在logic包下创建FalseFunction类,代码如下:
package com.ql.util.express.self.combat.function.logic;
import com.ql.util.express.Operator;
/**
* 类描述:FALSE函数
*
* @author admin
* @version 1.0.0
* @date 2023/11/21 17:38
*/
public class FalseFunction extends Operator {
public FalseFunction(String name) {
this.name = name;
}
@Override
public Object executeInner(Object[] objects) throws Exception {
return Boolean.FALSE;
}
}
把FalseFunction类注册到公式函数入口类中,代码如下:
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 FALSE() throws Exception{
FormulaRunner formulaRunner = new FormulaRunner(true,true);
// 创建上下文
DefaultContext context = new DefaultContext<>();
String express = "IF(1>2,TRUE(),FALSE())";
Object object = formulaRunner.execute(express, context, null, true, true);
System.out.println(object);
}
}
运行结果