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