|
@@ -0,0 +1,129 @@
|
|
|
+package com.platform.service.util;
|
|
|
+
|
|
|
+import com.greenpineyu.fel.FelEngine;
|
|
|
+import com.greenpineyu.fel.FelEngineImpl;
|
|
|
+import com.greenpineyu.fel.context.FelContext;
|
|
|
+import com.platform.common.cache.ConfigCache;
|
|
|
+import com.platform.common.util.StringUtils;
|
|
|
+import com.platform.dao.enums.SysConfigEnum;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description: 数学公式解析计算的参数转换
|
|
|
+ * @author: jxq
|
|
|
+ * @date: 2021/3/25 12:44 下午
|
|
|
+ * @version: 1.0
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+public class FelParameterUtil {
|
|
|
+
|
|
|
+ private final static FelEngine fel = new FelEngineImpl();
|
|
|
+ private final static FelContext ctx = fel.getContext();
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ String expression = "(m3-m1)*100/m2";
|
|
|
+ Map<String, Double> param = new HashMap<>();
|
|
|
+ param.put("m3", 110.0);
|
|
|
+ param.put("m1", 100.0);
|
|
|
+ param.put("m2", 10.0);
|
|
|
+ int scale = 4;
|
|
|
+ String result = getResultFromMap(expression, param, scale);
|
|
|
+ System.out.println(result);
|
|
|
+
|
|
|
+ String ss = "m₃";
|
|
|
+ String ssss = "₃";
|
|
|
+ System.out.println(ss.contains(ssss));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算一条表达式的值
|
|
|
+ *
|
|
|
+ * @param expression
|
|
|
+ * @param param
|
|
|
+ * @param scale
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getResultFromMap(String expression, Map<String, Double> param, int scale) {
|
|
|
+
|
|
|
+ // 处理数据下标和上标的转换
|
|
|
+ String config = ConfigCache.getLabelByValueAllowNull(SysConfigEnum.QUALITY_FORMULA_TRANSFER.name());
|
|
|
+ if (StringUtils.isNotBlank(config)) {
|
|
|
+ String[] configArray = config.split(";");
|
|
|
+ Map<String, String> mapType = new HashMap<>();
|
|
|
+ for (String c : configArray) {
|
|
|
+ String[] mapStr = c.split(",");
|
|
|
+ mapType.put(mapStr[0], mapStr[1]);
|
|
|
+ }
|
|
|
+ System.out.println("这个是用JSON类,指定解析类型,来解析JSON字符串!!!");
|
|
|
+ for (String key : mapType.keySet()) {
|
|
|
+ System.out.println("key为:" + key + "值为:" + mapType.get(key));
|
|
|
+ // 替换公式里面的
|
|
|
+ if (expression.contains(key)) {
|
|
|
+ expression = expression.replace(key, mapType.get(key));
|
|
|
+ }
|
|
|
+ // 替换参数里面的,只能使用这个删除,动态循环里面不允许删除
|
|
|
+ Map<String, Double> changeMap = new HashMap<>();
|
|
|
+ Iterator<Map.Entry<String, Double>> iterator = param.entrySet().iterator();
|
|
|
+ // 找到需要替换的,放到一个新map,删除原有map被替换的数据
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ Map.Entry<String, Double> next = iterator.next();
|
|
|
+ if (next.getKey().equals(key)) {
|
|
|
+ changeMap.put(mapType.get(key),next.getValue());
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 需要替换的,更新到原有map
|
|
|
+ for (Map.Entry<String, Double> entry : changeMap.entrySet()) {
|
|
|
+ param.put(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("开始时间:" + LocalDateTime.now());
|
|
|
+ for (Map.Entry<String, Double> entry : param.entrySet()) {
|
|
|
+ log.info("key = " + entry.getKey() + ", value = " + entry.getValue());
|
|
|
+ String entryKey = entry.getKey();
|
|
|
+ Double entryValue = entry.getValue();
|
|
|
+ ctx.set(entryKey, entryValue);
|
|
|
+ }
|
|
|
+ log.info("expression = " + expression);
|
|
|
+ Object obj = fel.eval(expression);
|
|
|
+ String resultD = "";
|
|
|
+ if (obj instanceof Integer) {
|
|
|
+ log.info("Integer: " + obj.toString());
|
|
|
+ resultD = obj.toString();
|
|
|
+ } else {
|
|
|
+ if (Double.isNaN((double) obj)) {
|
|
|
+ resultD = "0";
|
|
|
+ } else {
|
|
|
+ resultD = obj.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("结束时间:" + LocalDateTime.now());
|
|
|
+ return getScaleResult(Double.valueOf(resultD), scale);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回指定精度的结果
|
|
|
+ * @param result
|
|
|
+ * @param scale
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getScaleResult(Double result, int scale){
|
|
|
+ StringBuffer sb = new StringBuffer("#0.");
|
|
|
+ for (int i = 0; i < scale; i++) {
|
|
|
+ sb.append("0");
|
|
|
+ }
|
|
|
+ String pattern = sb.toString();
|
|
|
+ DecimalFormat decimalFormat = new DecimalFormat(pattern);// 格式化设置
|
|
|
+ return decimalFormat.format(result);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|