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