|
@@ -1,6 +1,7 @@
|
|
|
package org.springblade.manager.service.impl;
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.jfireel.expression.Expression;
|
|
|
import com.mixsmart.utils.CustomFunction;
|
|
@@ -40,14 +41,13 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
public final static String CHAIN="trees";
|
|
|
public final static String FC="FC.";
|
|
|
|
|
|
-
|
|
|
public static final String POLY_REG= "(avg|min|max|sum)\\(([^)]+)\\)";
|
|
|
public static final Pattern POLY = Pattern.compile(POLY_REG);
|
|
|
- public void execute( List<FormData> list ){
|
|
|
+ @Override
|
|
|
+ public void execute(List<FormData> list ){
|
|
|
Map<Boolean,List<FormData>> map = list.stream().collect(Collectors.partitioningBy(e->e!=null&&e.getFormula().getFormula().contains("E[")));
|
|
|
List<FormData>total = new ArrayList<>();
|
|
|
/*用来保存所有变量*/
|
|
|
- Map<String,Object> variables = new HashMap<>();
|
|
|
Map<String,Object> constantMap = new HashMap<>();
|
|
|
/*预设变量start*/
|
|
|
List<WbsTree> trees = new ArrayList<>();
|
|
@@ -84,48 +84,84 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
/*预处理公式脚本*/
|
|
|
String tmp =fd.getFormula().getFormula();
|
|
|
tmp = tmp.replace(FC, CustomFunction.CLASS_CALL);
|
|
|
- Matcher m = PP.matcher(tmp);
|
|
|
- while (m.find()){
|
|
|
- tmp= tmp.replace(m.group(),"'"+m.group()+"'");
|
|
|
+
|
|
|
+ if(tmp.contains("WP[")){
|
|
|
+ Matcher m = PP.matcher(tmp);
|
|
|
+ while (m.find()){
|
|
|
+ tmp= tmp.replace("WP"+m.group()+"]","WP['"+m.group()+"']");
|
|
|
+ }
|
|
|
}
|
|
|
- Matcher m2 = P.matcher(tmp);
|
|
|
- while (m2.find()){
|
|
|
- tmp= tmp.replace(m2.group(),"'"+m2.group()+"'");
|
|
|
+
|
|
|
+ if(tmp.contains(CustomFunction.CLASS_CALL+"ifelse")){
|
|
|
+ Matcher im =IF.matcher(tmp);
|
|
|
+ while (im.find()){
|
|
|
+ String rep =im.group();
|
|
|
+ Matcher fm=P.matcher(rep);
|
|
|
+ while (fm.find()){
|
|
|
+ rep=rep.replace(fm.group(),"'+"+fm.group()+"+'");
|
|
|
+ rep="'"+rep+"'";
|
|
|
+ }
|
|
|
+ tmp=tmp.replace(im.group(),rep);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(tmp.contains("E[")){
|
|
|
+ Matcher m2 = P.matcher(tmp);
|
|
|
+ while (m2.find()){
|
|
|
+ tmp= tmp.replace("E["+m2.group()+"]","E['"+m2.group()+"']");
|
|
|
+ }
|
|
|
}
|
|
|
- fd.getFormula().setFormula(tmp);
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ fd.getFormula().setFormula(tmp);
|
|
|
+ }
|
|
|
+ /*公式执行*/
|
|
|
for(FormData fd:total){
|
|
|
Formula formula= fd.getFormula();
|
|
|
+
|
|
|
if(formula!=null){
|
|
|
String f=formula.getFormula();
|
|
|
if(Func.isNotBlank(f)){
|
|
|
+
|
|
|
Map<String, Object> currentMap = new HashMap<>(constantMap);
|
|
|
Map<String,Object> E = getMap(currentMap,"E");
|
|
|
List<String> relyList = fd.getFormula().getRelyList();
|
|
|
+
|
|
|
if(CollectionUtil.isNotEmpty(relyList)){
|
|
|
+
|
|
|
List<FormData> ele = total.stream().filter(e->relyList.contains(e.getCode())).collect(Collectors.toList());
|
|
|
+
|
|
|
if(f.contains(CustomFunction.CLASS_CALL+"avg(")||f.contains(CustomFunction.CLASS_CALL+"min(")||f.contains(CustomFunction.CLASS_CALL+"max(")||f.contains(CustomFunction.CLASS_CALL+"sum(")){
|
|
|
+ /*聚合*/
|
|
|
Matcher m = POLY.matcher(f);
|
|
|
while (m.find()){
|
|
|
List<String> elementCodes=CustomFunction.parseElementCode(m.group(2));
|
|
|
List<FormData> tmp = ele.stream().filter(e->elementCodes.contains(e.getCode())).collect(Collectors.toList());
|
|
|
ele.removeAll(tmp);
|
|
|
+
|
|
|
tmp.forEach(e-> E.put(e.getCode(),e.getValues().stream().map(ElementData::getValue).collect(Collectors.toList())));
|
|
|
Object data = Expression.parse(CustomFunction.CLASS_CALL+m.group()).calculate(currentMap);
|
|
|
+
|
|
|
f = f.replace(CustomFunction.CLASS_CALL+m.group(),data.toString());
|
|
|
fd.getFormula().setFormula(f);
|
|
|
}
|
|
|
}
|
|
|
if(CollectionUtil.isNotEmpty(ele)){
|
|
|
Map<Integer,Map<String,List<Object>>> dataSource = new HashMap<>();
|
|
|
+
|
|
|
for(FormData e:ele){
|
|
|
Map<Integer,List<ElementData>> m = e.getValues().stream().collect(Collectors.groupingBy(ElementData::getGroupId));
|
|
|
+
|
|
|
for(Map.Entry<Integer,List<ElementData>> entry:m.entrySet()){
|
|
|
Map<String,List<Object>> t = dataSource.computeIfAbsent(entry.getKey(), k -> new HashMap());
|
|
|
t.put(e.getCode(),entry.getValue().stream().map(ElementData::getValue).collect(Collectors.toList()));
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
if(dataSource.size()>0){
|
|
|
for(Map.Entry<Integer,Map<String,List<Object>>> entry:dataSource.entrySet()){
|
|
|
if(entry.getValue().keySet().size()>=ele.size()){
|
|
@@ -134,6 +170,7 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}else{
|
|
|
fd.getValues().add(new ElementData(0,0,Expression.parse(formula.getFormula()).calculate(currentMap)));
|
|
|
}
|
|
@@ -144,9 +181,7 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-// total.stream().filter(e->Func.isNotBlank(e.getFormula().getFormula())).map(FormData::getValues).forEach(e->e.stream().map(ElementData::getValue).forEach(System.out::println));
|
|
|
- total.stream().filter(e->Func.isNotBlank(e.getFormula().getFormula())).map(FormData::getValues).forEach(System.out::println);
|
|
|
-
|
|
|
+ total.stream().filter(e->Func.isNotBlank(e.getFormula().getFormula())).map(FormData::getValues).forEach(e->e.stream().map(JSON::toJSONString).forEach(System.out::println));
|
|
|
}
|
|
|
|
|
|
public Map<String,Object> getMap(Map<String,Object> main,String key){
|
|
@@ -159,24 +194,25 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
}
|
|
|
|
|
|
public List<ElementData> slice(Map<String,List<Object>> elementMap,Map<String,Object> constantMap,String formula,Integer groupId){
|
|
|
+
|
|
|
int min =0;
|
|
|
List<ElementData> result = new ArrayList<>();
|
|
|
+
|
|
|
for(Map.Entry<String,List<Object>> entry:elementMap.entrySet()){
|
|
|
List<Object> tmp = entry.getValue();
|
|
|
- if(min==0){
|
|
|
- min=tmp.size();
|
|
|
- }else{
|
|
|
- min= Math.min(min,tmp.size());
|
|
|
- }
|
|
|
+ min = min==0?tmp.size():Math.min(min,tmp.size());
|
|
|
}
|
|
|
if(min>0){
|
|
|
List<LocalVariable> lvab= new ArrayList<>();
|
|
|
+
|
|
|
for(int i=0;i<min;i++){
|
|
|
Map<String, Object> currentMap = new HashMap<>(constantMap);
|
|
|
Map<String,Object> E=getMap(currentMap,"E");
|
|
|
+
|
|
|
for(Map.Entry<String,List<Object>> e:elementMap.entrySet()){
|
|
|
E.put(e.getKey(),e.getValue().get(i));
|
|
|
}
|
|
|
+
|
|
|
lvab.add(new LocalVariable(i,formula,currentMap));
|
|
|
}
|
|
|
/*并发执行*/
|
|
@@ -184,6 +220,7 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
Object data =Expression.parse(e.getFormula()).calculate(e.getCurrentMap());
|
|
|
return new ElementData(e.getIndex(),groupId,data);
|
|
|
}).collect(Collectors.toList());
|
|
|
+
|
|
|
if(CollectionUtil.isNotEmpty(r)){
|
|
|
result.addAll(r);
|
|
|
}
|
|
@@ -195,6 +232,8 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
public static final String WP_REG= "(?<=P\\[)[^]]+(?=\\])";
|
|
|
public static final Pattern PP = Pattern.compile(WP_REG);
|
|
|
|
|
|
+ public static final String IF_REG= "(?<=T\\(com.mixsmart.utils.CustomFunction\\).ifelse\\()[^,]+(?=,)";
|
|
|
+ public static final Pattern IF = Pattern.compile(WP_REG);
|
|
|
/**
|
|
|
* 依赖排序
|
|
|
*/
|