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