dengzedong
2024-12-26 efdc380e66bbc3112eb87d7522f0a37d082082e1
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mdk.sample;
2
50084d 3 import com.iailab.module.data.api.plan.PlanItemApi;
8bf553 4 import com.iailab.module.data.api.plan.dto.ApiPlanItemDTO;
7fd198 5 import com.iailab.module.data.api.point.DataPointApi;
6 import com.iailab.module.data.api.point.dto.ApiPointDTO;
7 import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
8 import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
50084d 9 import com.iailab.module.data.common.ApiDataQueryDTO;
D 10 import com.iailab.module.data.common.ApiDataValueDTO;
c4b37d 11 import com.iailab.module.model.common.enums.OutResultType;
a4891a 12 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
13 import com.iailab.module.model.mcs.pre.service.MmItemOutputService;
c4b37d 14 import com.iailab.module.model.mcs.pre.service.MmItemResultJsonService;
7fd198 15 import com.iailab.module.model.mcs.pre.service.MmItemResultService;
a4891a 16 import com.iailab.module.model.mcs.pre.service.MmItemTypeService;
17 import com.iailab.module.model.mdk.common.enums.ModelParamType;
7fd198 18 import com.iailab.module.model.mdk.sample.dto.ColumnItem;
19 import com.iailab.module.model.mdk.sample.dto.ColumnItemPort;
20 import com.iailab.module.model.mdk.sample.dto.SampleData;
21 import com.iailab.module.model.mdk.sample.dto.SampleInfo;
22 import com.iailab.module.model.mdk.vo.DataValueVO;
50084d 23 import lombok.extern.slf4j.Slf4j;
c4b37d 24 import org.apache.commons.lang3.StringUtils;
7fd198 25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component;
50084d 29 import org.springframework.util.CollectionUtils;
7fd198 30
31 import java.util.*;
214275 32 import java.util.stream.Collectors;
7fd198 33
34 /**
35  * 预测样本数据构造
36  */
50084d 37 @Slf4j
7fd198 38 @Component
39 public class PredictSampleDataConstructor extends SampleDataConstructor {
40
41     private Logger logger = LoggerFactory.getLogger(getClass());
42
43     @Autowired
44     private DataPointApi dataPointApi;
50084d 45
D 46     @Autowired
47     private PlanItemApi planItemApi;
7fd198 48
49     @Autowired
50     private MmItemResultService mmItemResultService;
c4b37d 51
D 52     @Autowired
53     private MmItemResultJsonService mmItemResultJsonService;
7fd198 54
55     @Autowired
a4891a 56     private MmItemTypeService mmItemTypeService;
57
58     @Autowired
59     private MmItemOutputService mmItemOutputService;
7fd198 60
61     /**
62      * alter by zfc 2020.11.24 修改数据样本构造方案:sampleInfo中数据已按爪子进行分类,但爪内数据为无序的,
63      * 对爪内数据样本拼接:先基于modelParamOrder对项进行排序(重写comparator匿名函数),再逐项拼接
64      *
65      * @param sampleInfo
66      * @return
67      */
68     @Override
50084d 69     public List<SampleData>  prepareSampleData(SampleInfo sampleInfo) throws Exception {
7fd198 70         List<SampleData> sampleDataList = new ArrayList<>();
50084d 71         Map<String, ApiPointDTO> pointMap = sampleInfo.getPointMap();
8bf553 72         Map<String, ApiPlanItemDTO> planMap = sampleInfo.getPlanMap();
7fd198 73         //对每个爪分别进行计算
74         for (ColumnItemPort entry : sampleInfo.getColumnInfo()) {
75             //先依据爪内数据项的modelParamOrder进行排序——重写comparator匿名函数
76             Collections.sort(entry.getColumnItemList(), new Comparator<ColumnItem>() {
77                 @Override
78                 public int compare(ColumnItem o1, ColumnItem o2) {
79                     return o1.getModelParamOrder() - o2.getModelParamOrder();
80                 }
81             });
82
83             //默认都是double类型的数据,且按列向量进行拼接,默认初始值为0.0
84             double[][] matrix = new double[entry.getDataLength()][entry.getColumnItemList().size()];
85             for (int i = 0; i < entry.getColumnItemList().size(); i++) {
86                 for (int j = 0; j < entry.getDataLength(); j++) {
87                     matrix[j][i] = -2.0;
88                 }
89             }
90
91             //对每一项依次进行数据查询,然后将查询出的值赋给matrix对应的位置
92             for (int i = 0; i < entry.getColumnItemList().size(); i++) {
93                 try {
8bf553 94                     List<DataValueVO> dataEntityList = getData(entry.getColumnItemList().get(i),pointMap,planMap);
7fd198 95                     //补全数据
96                     ColumnItem columnItem = entry.getColumnItemList().get(i);
a4891a 97                     dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime,
8bf553 98                             columnItem.paramId, columnItem.getParamType(),pointMap,planMap);
7fd198 99
100                     /** 如果数据取不满,把缺失的数据点放在后面 */
101                     if (dataEntityList != null && dataEntityList.size() != 0) {
102                         logger.info("设置matrix, i = " + i + ", size = " + dataEntityList.size());
103                         for (int k = 0; k < dataEntityList.size(); k++) {
104                             matrix[k][i] = dataEntityList.get(k).getDataValue();
105                         }
106                     }
107                 } catch (Exception e) {
108                     e.printStackTrace();
50084d 109                     throw e;
7fd198 110                 }
111             }
112             SampleData sampleData = new SampleData();
113             sampleData.setMatrix(matrix);
114             sampleDataList.add(sampleData);
115         }
116         return sampleDataList;
117     }
118
119     /**
120      * getData
121      *
122      * @param columnItem
50084d 123      * @param pointMap
8bf553 124      * @param planMap
7fd198 125      * @return
126      * @throws Exception
127      */
8bf553 128     private List<DataValueVO> getData(ColumnItem columnItem, Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) throws Exception {
7fd198 129         List<DataValueVO> dataList = new ArrayList<>();
130         String paramType = columnItem.getParamType();
a4891a 131         switch (ModelParamType.getEumByCode(paramType)) {
132             case DATAPOINT:
7fd198 133                 ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
50084d 134                 queryDto.setPointNo(pointMap.get(columnItem.getParamId()).getPointNo());
7fd198 135                 queryDto.setStart(columnItem.getStartTime());
136                 queryDto.setEnd(columnItem.getEndTime());
536b8e 137                 List<ApiPointValueDTO> pointValueList = dataPointApi.queryPointHistoryValue(queryDto);
50084d 138                 if (CollectionUtils.isEmpty(pointValueList)) {
D 139                     break;
140                 }
a4891a 141                 dataList = pointValueList.stream().map(t -> {
214275 142                     DataValueVO vo = new DataValueVO();
143                     vo.setDataTime(t.getT());
144                     vo.setDataValue(t.getV());
145                     return vo;
146                 }).collect(Collectors.toList());
7fd198 147                 break;
19a4fb 148             case NORMALITEM:
D 149             case MERGEITEM:
c4b37d 150                 MmItemOutputEntity outPut = mmItemOutputService.getOutPutById(columnItem.getParamId());
D 151                 OutResultType outResultType = OutResultType.getEumByCode(outPut.getResultType());
152                 List<DataValueVO> predictValue = new ArrayList<>();
037fb7 153
D 154                 // double类型特殊处理
efdc38 155                 if (OutResultType.D.equals(outResultType)) {
037fb7 156                     // columnItem.getStartTime()就是预测时间
D 157                     String doubleData = mmItemResultJsonService.getDoubleData(outPut.getId(), columnItem.getStartTime());
158                     if (StringUtils.isNotBlank(doubleData)) {
159                         DataValueVO dataValueVO = new DataValueVO();
160                         dataValueVO.setDataTime(columnItem.getStartTime());
161                         dataValueVO.setDataValue(Double.valueOf(doubleData));
162                         predictValue.add(dataValueVO);
163                     }
164                 } else {
165                     predictValue = mmItemResultService.getPredictValue(outPut.getId(), columnItem.getStartTime(), columnItem.getEndTime());
c4b37d 166                 }
037fb7 167
50084d 168                 if (CollectionUtils.isEmpty(predictValue)) {
D 169                     break;
7fd198 170                 }
50084d 171                 dataList = predictValue;
7fd198 172                 break;
50084d 173             case PLAN:
D 174                 ApiDataQueryDTO queryPlanItemDto = new ApiDataQueryDTO();
8bf553 175                 queryPlanItemDto.setItemNo(planMap.get(columnItem.getParamId()).getItemNo());
50084d 176                 queryPlanItemDto.setStart(columnItem.getStartTime());
D 177                 queryPlanItemDto.setEnd(columnItem.getEndTime());
178                 List<ApiDataValueDTO> planValueList = planItemApi.queryPlanItemHistoryValue(queryPlanItemDto);
179                 if (CollectionUtils.isEmpty(planValueList)) {
180                     break;
181                 }
182                 dataList = planValueList.stream().map(t -> {
183                     DataValueVO vo = new DataValueVO();
184                     vo.setDataTime(t.getDataTime());
185                     vo.setDataValue(t.getDataValue());
186                     return vo;
187                 }).collect(Collectors.toList());
7fd198 188             default:
189                 break;
190         }
e9f7b1 191         // 避免生产环境日志过多,分级打印
9f09d4 192         log.debug("数据获取,columnItem:" + columnItem + ",dataList:" + dataList);
D 193         log.info("数据获取,columnItem:" + columnItem + ",dataListLength:" + dataList.size());
7fd198 194         return dataList;
195     }
196 }