07.仿简道云公式函数实战-逻辑函数-AND
2023-12-15 15:40:11
1.AND函数
AND 函数可用于表示:当所有参数逻辑值为 true 时,返回 true;只要有任何一个参数逻辑值为 false,则返回false。
2. 函数用法
AND(语文成绩>90,数学成绩>90,英语成绩>90)
3.函数示例
1)AND(A,B),表示如果同时满足条件 A,B,则返回 true,否则返回 false。例如:
-
AND(1<5,1<6),返回结果为 true; -
AND(1<5,7<6),返回结果为 false。
2)AND 函数可与 IF 函数等组合使用。如成绩判定时,需设置当三科成绩均大于 90 分时为“优秀”,否则为“其他”,则设置公式为IF(AND(语文成绩>90,数学成绩>90,英语成绩>90),"优秀","其他")
4.代码实战
首先我们在function包下创建AndFunction类,代码如下:
package com.ql.util.express.self.combat.function;
import com.ql.util.express.Operator;
import com.ql.util.express.self.combat.exception.FormulaException;
/**
* 类描述: AND公式函数
*
* @author admin
* @version 1.0.0
* @date 2023/11/21 15:37
*/
public class AndFunction extends Operator {
public AndFunction(String name) {
this.name = name;
}
public Object executeInner(Object[] list) throws Exception {
return this.executeInner(list[0], list[1]);
}
public Object executeInner(Object operand1, Object operand2) throws Exception {
boolean r1;
if(operand1 == null) {
r1 = false;
} else {
if(!(operand1 instanceof Boolean)) {
String msg = "没有定义类型" + operand1 + "和" + operand2 + " 的 " + this.name + "操作";
throw new FormulaException(msg);
}
r1 = ((Boolean)operand1).booleanValue();
}
boolean r2;
if(operand2 == null) {
r2 = false;
} else {
if(!(operand2 instanceof Boolean)) {
String msg = "没有定义类型" + operand1 + "和" + operand2 + " 的 " + this.name + "操作";
throw new FormulaException(msg);
}
r2 = ((Boolean)operand2).booleanValue();
}
return Boolean.valueOf(r1 && r2);
}
}
把AndFunction类注册到公式函数入口类中,代码如下:
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.AndFunction;
/**
* 类描述: 仿简道云公式函数实战入口类
*
* @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"));
}
}
创建测试用例:
/**
* 类描述: 实战测试类
*
* @author admin
* @version 1.0.0
* @date 2023/11/21 15:45
*/
public class CombatTest {
@Test
public void and() throws Exception{
FormulaRunner formulaRunner = new FormulaRunner(true,false);
// 创建上下文
DefaultContext<String, Object> context = new DefaultContext<>();
String and = "AND(语文成绩>90,数学成绩>90,英语成绩>90)";
context.put("语文成绩",100);
context.put("数学成绩",92);
context.put("英语成绩",96);
Object object = formulaRunner.execute(and, context, null, true, false);
System.out.println(object);
}
}
运行结果:

最近笔者创建了一个圈子纷传
欢迎大家加入一起交流学习。
文章来源:https://blog.csdn.net/DSDS454651/article/details/134918825
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!