大家好,欢迎来到IT知识分享网。
1. OR函数
OR 函数可用于表示:参数组中,任何一个参数逻辑值为 true 时,即返回 true;只有当所有逻辑参数值为 false 时,才返回 false。
2. 函数用法
OR(logical1,logical2, …)
3. 函数示例
1)OR(A,B),表示如果满足条件 A,或者满足条件 B(即 2 个条件中满足一个或一个以上),则返回 true,否则返回 false。例如:
OR(1<2,2<2),返回结果为 true;OR(3<2,2<2),返回结果为 false。
2)OR 函数可与 IF 函数等组合使用,如成绩判定时,当任意一门成绩大于 90 分时,即为“优秀”,否则为“其他”,则设置公式为IF(OR(语文成绩>90,数学成绩>90,英语成绩>90),"优秀","其他")
4. 代码实战
首先我们在function包下创建logic包,在logic包下创建OrFunction类,代码如下:
package com.ql.util.express.self.combat.function.logic; import com.ql.util.express.Operator; import com.ql.util.express.self.combat.exception.FormulaException; / * 类描述: OR函数 * * @author admin * @version 1.0.0 * @date 2023/11/23 8:45 */ public class OrFunction extends Operator { public OrFunction(String name) { this.name = name; } public OrFunction(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], list[1]); } public Object executeInner(Object op1, Object op2) throws Exception { boolean r1; String msg; if(op1 == null) { r1 = false; } else { if(!(op1 instanceof Boolean)) { msg = "没有定义类型" + op1 + "和" + op2 + " 的 " + this.name + "操作"; throw new FormulaException(msg); } r1 = ((Boolean)op1).booleanValue(); } boolean r2; if(op2 == null) { r2 = false; } else { if(!(op2 instanceof Boolean)) { msg = "没有定义类型" + op1 + "和" + op2 + " 的 " + this.name + "操作"; throw new FormulaException(msg); } r2 = ((Boolean)op2).booleanValue(); } return Boolean.valueOf(r1 || r2); } }
把OrFunction类注册到公式函数入口类中,代码如下:
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")); // OR函数 this.addFunction("OR",new OrFunction("OR")); } }
创建测试用例
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 OR() throws Exception{ FormulaRunner formulaRunner = new FormulaRunner(true,true); // 创建上下文 DefaultContext<String, Object> context = new DefaultContext<>(); String express = "IF(OR(语文成绩>90,数学成绩>90,英语成绩>90),\"优秀\",\"其他\")"; context.put("语文成绩",80); context.put("数学成绩",92); context.put("英语成绩",76); Object object = formulaRunner.execute(express, context, null, true, true); System.out.println(object); } }
运行结果
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/119096.html
