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