【脚本】JAVA 执行 阿里QLExpress 动态脚本 demo 基础版 增加项目灵活性 第一步 增加依赖!-- https://mvnrepository.com/artifact/com.alibaba/QLExpress -- dependency groupIdcom.alibaba/groupId artifactIdQLExpress/artifactId version3.3.2/version /dependency dependency groupIdorg.dromara.hutool/groupId artifactIdhutool-all/artifactId version6.0.0-M11/version /dependency方案一简单demo语法如果 (语文数学英语270) 则 及格 否则 不及格 //返回 及格package com.study.Script.QLExpress.简单demo; import com.guanwei.study.tools.WTools; import com.ql.util.express.DefaultContext; import com.ql.util.express.ExpressRunner; public class QLExpressDemo { public static void main(String[] args) throws Exception { ExpressRunner runner new ExpressRunner(); DefaultContextString, Object context new DefaultContextString, Object(); context.put(a,1); context.put(b,2); context.put(c,3); String express (ab*c-5)x50.11%; express WTools.mathUtil.transitionFormula(express); System.out.println(express); Object r runner.execute(express, context, null, true, false); System.out.println(r); runner.addOperatorWithAlias(如果, if,null); runner.addOperatorWithAlias(则, then,null); runner.addOperatorWithAlias(否则, else,null); runner.addOperatorWithAlias(返回, return,null); String exp 如果 (语文数学英语270) 则 及格 否则 不及格; context new DefaultContextString, Object(); context.put(语文,30); context.put(数学,2); context.put(英语,3); Object execute runner.execute(exp, context, null, false, false); System.out.println(execute); } }工具package com.guanwei.study.tools; import com.guanwei.study.math.MathUtil; public class WTools { public static MathUtil mathUtil new MathUtil(); }工具 解析特殊符号 转换为 QLExpress 能看懂的package com.guanwei.study.math; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ReUtil; import com.guanwei.study.string.CharUtil; import org.dromara.hutool.core.text.StrUtil; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class MathUtil { /** * 转换符号 公式等 * % - * / * param exp * return */ public String transitionFormula(String exp){ // exp ; //全角转半角 exp CharUtil.toDbc(exp); //所有字符转为小写 exp exp.toLowerCase(); exp exp .replaceAll(, ) .replaceAll(十, ) .replaceAll(┼, ) .replaceAll(╋, ) .replaceAll(╬, ) .replaceAll(, -) .replaceAll(一, -) .replaceAll(x, *) .replaceAll(×, *) .replaceAll(╳, *) .replaceAll(х, *) .replaceAll(·, *) .replaceAll(÷, /) .replaceAll(\\{, () .replaceAll(}, )) .replaceAll(\\[, () .replaceAll(], )) ; //包含百分号 if(exp.contains(%)){ //获取包含百分号的数字 ListString resultFindAll ReUtil.findAll(([\\d])*%|(\\d{1,10}).([\\d])*%, exp, 0, new ArrayListString()); // ListString resultFindAll2 ReUtil.findAll(([\\d])*%|([\\d{1,10}]).([\\d])*%, exp, 0, new ArrayListString()); if(CollUtil.isNotEmpty(resultFindAll)){ for (String s : resultFindAll) { s s.trim(); String replace s.replace(%, ); if(StrUtil.isBlank(replace)){ exp exp.replaceAll(s, 0); }else{ BigDecimal bigDecimal new BigDecimal(replace); bigDecimal bigDecimal.divide(new BigDecimal(100)); exp exp.replaceAll(s, bigDecimal.toString()); } } } System.out.println(resultFindAll); } return exp; } }