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