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