提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.mdk.predict.impl; |
潘 |
2 |
|
|
3 |
import com.iailab.module.data.api.point.DataPointApi; |
|
4 |
import com.iailab.module.data.api.point.dto.ApiPointDTO; |
e78121
|
5 |
import com.iailab.module.data.enums.DataPointFreqEnum; |
a4891a
|
6 |
import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; |
7f0bcd
|
7 |
import com.iailab.module.model.mcs.pre.enums.ItemRunStatusEnum; |
3059dd
|
8 |
import com.iailab.module.model.mcs.pre.service.MmItemOutputService; |
D |
9 |
import com.iailab.module.model.mcs.pre.service.MmItemResultService; |
7fd198
|
10 |
import com.iailab.module.model.mdk.common.enums.ItemPredictStatus; |
潘 |
11 |
import com.iailab.module.model.mdk.common.exceptions.ItemInvokeException; |
|
12 |
import com.iailab.module.model.mdk.factory.ItemEntityFactory; |
|
13 |
import com.iailab.module.model.mdk.factory.PredictItemFactory; |
|
14 |
import com.iailab.module.model.mdk.predict.PredictItemHandler; |
|
15 |
import com.iailab.module.model.mdk.predict.PredictResultHandler; |
|
16 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
|
17 |
import com.iailab.module.model.mdk.vo.ItemVO; |
|
18 |
import com.iailab.module.model.mdk.vo.PredictResultVO; |
|
19 |
import lombok.extern.slf4j.Slf4j; |
|
20 |
import org.springframework.beans.factory.annotation.Autowired; |
|
21 |
import org.springframework.stereotype.Component; |
|
22 |
|
|
23 |
import java.sql.Timestamp; |
|
24 |
import java.util.*; |
07890e
|
25 |
import java.util.stream.Collectors; |
7fd198
|
26 |
|
潘 |
27 |
/** |
|
28 |
* @author PanZhibao |
|
29 |
* @Description |
|
30 |
* @createTime 2024年09月01日 |
|
31 |
*/ |
|
32 |
@Slf4j |
|
33 |
@Component |
|
34 |
public class PredictItemMergeHandlerImpl implements PredictItemHandler { |
|
35 |
|
|
36 |
@Autowired |
|
37 |
private ItemEntityFactory itemEntityFactory; |
|
38 |
|
|
39 |
@Autowired |
|
40 |
private DataPointApi dataPointApi; |
|
41 |
|
|
42 |
@Autowired |
|
43 |
private PredictItemFactory predictItemFactory; |
|
44 |
|
|
45 |
@Autowired |
|
46 |
private PredictResultHandler predictResultHandler; |
|
47 |
|
3059dd
|
48 |
@Autowired |
D |
49 |
private MmItemResultService mmItemResultService; |
|
50 |
|
|
51 |
@Autowired |
|
52 |
private MmItemOutputService mmItemOutputService; |
|
53 |
|
4f1717
|
54 |
/** |
潘 |
55 |
* MergeItem预测 |
|
56 |
* |
|
57 |
* @param predictTime |
|
58 |
* @param predictItemDto |
|
59 |
* @return |
|
60 |
* @throws ItemInvokeException |
|
61 |
*/ |
7fd198
|
62 |
@Override |
1178da
|
63 |
public PredictResultVO predict(Date predictTime, ItemVO predictItemDto, Map<String, double[]> predictValueMap) |
7fd198
|
64 |
throws ItemInvokeException { |
潘 |
65 |
PredictResultVO predictResult = new PredictResultVO(); |
|
66 |
ItemPredictStatus itemStatus = ItemPredictStatus.PREDICTING; |
|
67 |
String itemId = predictItemDto.getId(); |
|
68 |
try { |
|
69 |
String expression = itemEntityFactory.getMergeItem(itemId).getExpression(); |
|
70 |
int predictLength = itemEntityFactory.getItemById(itemId).getPredictLength(); |
|
71 |
double[][] predictResultMat = new double[predictLength][1]; |
3059dd
|
72 |
String[] mathOutPutId = expression.split("[\\+ \\-]"); |
7fd198
|
73 |
ArrayList<Character> operator = new ArrayList<>(); |
潘 |
74 |
for (int i = 0; i < expression.length(); i++) { |
|
75 |
if (expression.charAt(i)=='+' || expression.charAt(i)=='-'){ |
|
76 |
operator.add(expression.charAt(i)); |
|
77 |
} |
|
78 |
} |
07890e
|
79 |
// String[] compositionItem = expression.split(String.valueOf("&".toCharArray())); |
7fd198
|
80 |
//是否为计算预测项 |
3059dd
|
81 |
if (mathOutPutId.length > 1) { |
07890e
|
82 |
// Map<String, List<DataValueVO>> predictValueMap = new HashMap<>(); |
D |
83 |
// for (String outPutId : mathOutPutId) { |
|
84 |
// if (outPutId.length() > 4) { |
|
85 |
// Date endTime = predictTime; |
|
86 |
//// ItemVO itemEntity = itemEntityFactory.getItemByItemNo(itemNo); |
|
87 |
//// List<MmItemOutputEntity> outPutList = itemEntityFactory.getOutPutByItemId(itemEntity.getId()); |
|
88 |
// MmItemOutputEntity outPut = mmItemOutputService.getOutPutById(outPutId); |
|
89 |
// ApiPointDTO pointEntity = dataPointApi.getInfoById(outPut.getPointid()); |
|
90 |
// |
|
91 |
// Calendar calendar = Calendar.getInstance(); |
|
92 |
// calendar.setTime(endTime); |
|
93 |
// calendar.add(Calendar.SECOND, (predictLength - 1) * DataPointFreqEnum.getEumByCode(pointEntity.getMinfreqid()).getValue()); |
|
94 |
// endTime = new Timestamp(calendar.getTime().getTime()); |
|
95 |
//// List<DataValueVO> predictValueList = predictResultHandler.getPredictValueByItemNo(itemNo, predictTime, endTime); |
|
96 |
// List<DataValueVO> predictValueList = mmItemResultService.getPredictValue(outPutId, predictTime, endTime); |
|
97 |
// if (predictValueList.size() != predictLength) { |
|
98 |
// log.debug("merge项融合失败:缺少子项预测数据,对应子项outPutId=" + outPutId); |
|
99 |
// return null; |
|
100 |
// } |
|
101 |
// predictValueMap.put(outPutId, predictValueList); |
|
102 |
// } |
|
103 |
// } |
7fd198
|
104 |
|
潘 |
105 |
for (Integer i = 0; i < predictLength; i++) { |
|
106 |
double sum =0.0; |
07890e
|
107 |
sum = predictValueMap.get(mathOutPutId[0])[i]; |
3059dd
|
108 |
for (int j = 1; j < mathOutPutId.length; j++) { |
7fd198
|
109 |
if (operator.get(j-1)=='+') |
07890e
|
110 |
{sum += predictValueMap.get(mathOutPutId[j])[i];} |
7fd198
|
111 |
if (operator.get(j-1)=='-') |
07890e
|
112 |
{sum -= predictValueMap.get(mathOutPutId[j])[i];} |
7fd198
|
113 |
} |
潘 |
114 |
predictResultMat[i][0] = sum; |
|
115 |
} |
|
116 |
} |
|
117 |
//是否为组合预测项 |
07890e
|
118 |
// if (compositionItem.length > 1) { |
D |
119 |
// Map<String, PredictResultVO> predictResultMap = new HashMap<>(); |
|
120 |
// Integer columnTotalNumber = 0; |
|
121 |
// Integer rowNumber = 0; |
|
122 |
// for (String itemNo : compositionItem) { |
|
123 |
// PredictItemHandler predictItem = (PredictItemHandler) predictItemFactory.create(itemEntityFactory. |
|
124 |
// getItemByItemNo(itemNo).getId()); |
|
125 |
// predictResult = predictItem.predict(predictTime, predictItemDto); |
|
126 |
// columnTotalNumber += Integer.valueOf(predictResult.getPredictMatrix().length); |
|
127 |
// predictResultMap.put(itemNo, predictItem.predict(predictTime, predictItemDto)); |
|
128 |
// } |
|
129 |
// double[][] matrix = new double[columnTotalNumber][1]; |
|
130 |
// for (String itemNo : compositionItem) { |
|
131 |
// for (Integer i = 0; i < predictResultMap.get(itemNo).getPredictMatrix().length; i++) { |
|
132 |
// matrix[rowNumber][0] = predictResultMap.get(itemNo).getPredictMatrix()[i][0]; |
|
133 |
// rowNumber++; |
|
134 |
// } |
|
135 |
// } |
|
136 |
// predictResult.setPredictMatrix(matrix); |
|
137 |
// } |
1178da
|
138 |
log.info("计算预测模型结果:" + predictResultMat); |
7fd198
|
139 |
predictResult.setPredictId(itemId); |
潘 |
140 |
predictResult.setPredictMatrix(predictResultMat); |
|
141 |
predictResult.setPredictTime(predictTime); |
|
142 |
//预测项预测成功的状态 |
|
143 |
itemStatus = ItemPredictStatus.SUCCESS; |
|
144 |
} catch (Exception e) { |
|
145 |
//预测项预测失败的状态 |
|
146 |
itemStatus = ItemPredictStatus.FAILED; |
|
147 |
log.debug("merge项预测失败,itemId:" + itemId); |
|
148 |
throw e; |
|
149 |
} |
|
150 |
log.debug("预测完成,itemId:" + itemId + ",itemStatus:" + itemStatus.getValue()); |
|
151 |
return predictResult; |
|
152 |
} |
|
153 |
} |