dengzedong
3 天以前 7f0bcd00c556889ba890e5e68c681f1b5d4267e1
提交 | 用户 | 时间
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
7f0bcd 63     public PredictResultVO predict(Date predictTime, ItemVO predictItemDto, Map<String, double[]> predictValueMap, ItemRunStatusEnum itemRunStatusEnum)
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 //            }
7fd198 138             predictResult.setPredictId(itemId);
139             predictResult.setPredictMatrix(predictResultMat);
140             predictResult.setPredictTime(predictTime);
141             //预测项预测成功的状态
142             itemStatus = ItemPredictStatus.SUCCESS;
143         } catch (Exception e) {
144             //预测项预测失败的状态
145             itemStatus = ItemPredictStatus.FAILED;
146             log.debug("merge项预测失败,itemId:" + itemId);
147             throw e;
148         }
149         log.debug("预测完成,itemId:" + itemId + ",itemStatus:" + itemStatus.getValue());
150         return predictResult;
151     }
152 }