liriming
2024-11-26 5b952f77058a9da5af5e143a6c2c7ba195aa736d
提交 | 用户 | 时间
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;
7fd198 7 import com.iailab.module.model.mdk.common.enums.ItemPredictStatus;
8 import com.iailab.module.model.mdk.common.exceptions.ItemInvokeException;
9 import com.iailab.module.model.mdk.factory.ItemEntityFactory;
10 import com.iailab.module.model.mdk.factory.PredictItemFactory;
11 import com.iailab.module.model.mdk.predict.PredictItemHandler;
12 import com.iailab.module.model.mdk.predict.PredictResultHandler;
13 import com.iailab.module.model.mdk.vo.DataValueVO;
14 import com.iailab.module.model.mdk.vo.ItemVO;
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
4f1717 44     /**
45      * MergeItem预测
46      *
47      * @param predictTime
48      * @param predictItemDto
49      * @return
50      * @throws ItemInvokeException
51      */
7fd198 52     @Override
53     public PredictResultVO predict(Date predictTime, ItemVO predictItemDto)
54             throws ItemInvokeException {
55         PredictResultVO predictResult = new PredictResultVO();
56         ItemPredictStatus itemStatus = ItemPredictStatus.PREDICTING;
57         String itemId = predictItemDto.getId();
58         try {
59             String expression = itemEntityFactory.getMergeItem(itemId).getExpression();
60             int predictLength = itemEntityFactory.getItemById(itemId).getPredictLength();
61             double[][] predictResultMat = new double[predictLength][1];
62             Map<String, List<DataValueVO>> predictValueMap = new HashMap<>();
63             String[] mathItem = expression.split("[\\+ \\-]");
64             ArrayList<Character> operator = new ArrayList<>();
65             for (int i = 0; i < expression.length(); i++) {
66                 if (expression.charAt(i)=='+' || expression.charAt(i)=='-'){
67                     operator.add(expression.charAt(i));
68                 }
69             }
70             String[] compositionItem = expression.split(String.valueOf("&".toCharArray()));
71             //是否为计算预测项
72             if (mathItem.length > 1) {
73                 for (String itemNo : mathItem) {
74                     if (itemNo.length() > 4) {
75                         Date endTime = predictTime;
76                         ItemVO itemEntity = itemEntityFactory.getItemByItemNo(itemNo);
a4891a 77                         List<MmItemOutputEntity> outPutList = itemEntityFactory.getOutPutByItemId(itemEntity.getId());
78                         ApiPointDTO pointEntity = dataPointApi.getInfoById(outPutList.get(0).getPointid());
7fd198 79
80                         Calendar calendar = Calendar.getInstance();
81                         calendar.setTime(endTime);
e78121 82                         calendar.add(Calendar.SECOND, (predictLength - 1) * DataPointFreqEnum.getEumByCode(pointEntity.getMinfreqid()).getValue());
7fd198 83                         endTime = new Timestamp(calendar.getTime().getTime());
84                         List<DataValueVO> predictValueList = predictResultHandler.getPredictValueByItemNo(itemNo, predictTime, endTime);
85                         if (predictValueList.size() != predictLength) {
86                             log.debug("merge项融合失败:缺少子项预测数据,对应子项ItemNo=" + itemNo);
87                             return null;
88                         }
89                         predictValueMap.put(itemNo, predictValueList);
90                     }
91                 }
92                 for (Integer i = 0; i < predictLength; i++) {
93                     double sum =0.0;
94                     sum = predictValueMap.get(mathItem[0]).get(i).getDataValue();
95                     for (int j = 1; j < mathItem.length; j++) {
96                         if (operator.get(j-1)=='+')
97                         {sum += predictValueMap.get(mathItem[j]).get(i).getDataValue();}
98                         if (operator.get(j-1)=='-')
99                         {sum -= predictValueMap.get(mathItem[j]).get(i).getDataValue();}
100                     }
101                     predictResultMat[i][0] = sum;
102                 }
103             }
104             //是否为组合预测项
105             if (compositionItem.length > 1) {
106                 Map<String, PredictResultVO> predictResultMap = new HashMap<>();
107                 Integer columnTotalNumber = 0;
108                 Integer rowNumber = 0;
109                 for (String itemNo : compositionItem) {
110                     PredictItemHandler predictItem = (PredictItemHandler) predictItemFactory.create(itemEntityFactory.
111                             getItemByItemNo(itemNo).getId());
112                     predictResult = predictItem.predict(predictTime, predictItemDto);
113                     columnTotalNumber += Integer.valueOf(predictResult.getPredictMatrix().length);
114                     predictResultMap.put(itemNo, predictItem.predict(predictTime, predictItemDto));
115                 }
116                 double[][] matrix = new double[columnTotalNumber][1];
117                 for (String itemNo : compositionItem) {
118                     for (Integer i = 0; i < predictResultMap.get(itemNo).getPredictMatrix().length; i++) {
119                         matrix[rowNumber][0] = predictResultMap.get(itemNo).getPredictMatrix()[i][0];
120                         rowNumber++;
121                     }
122                 }
123                 predictResult.setPredictMatrix(matrix);
124             }
125             predictResult.setPredictId(itemId);
126             predictResult.setPredictMatrix(predictResultMat);
127             predictResult.setPredictTime(predictTime);
128             //预测项预测成功的状态
129             itemStatus = ItemPredictStatus.SUCCESS;
130         } catch (Exception e) {
131             //预测项预测失败的状态
132             itemStatus = ItemPredictStatus.FAILED;
133             log.debug("merge项预测失败,itemId:" + itemId);
134             throw e;
135         }
136         log.debug("预测完成,itemId:" + itemId + ",itemStatus:" + itemStatus.getValue());
137         return predictResult;
138     }
139 }