dengzedong
2 天以前 e691b98e02153c9c439d7f5acb468924572d75d9
提交 | 用户 | 时间
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
bab433 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 {
bab433 94                     List<DataValueVO> dataEntityList = getData(entry.getColumnItemList().get(i), pointMap, planMap);
7fd198 95                     //补全数据
96                     ColumnItem columnItem = entry.getColumnItemList().get(i);
e691b9 97                     dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.getParamType(),columnItem.getGranularity());
7fd198 98
99                     /** 如果数据取不满,把缺失的数据点放在后面 */
100                     if (dataEntityList != null && dataEntityList.size() != 0) {
101                         logger.info("设置matrix, i = " + i + ", size = " + dataEntityList.size());
102                         for (int k = 0; k < dataEntityList.size(); k++) {
e691b9 103                             Double dataValue = dataEntityList.get(k).getDataValue();
D 104                             if (null != dataValue) {
105                                 matrix[k][i] = dataValue;
106                             }
7fd198 107                         }
108                     }
109                 } catch (Exception e) {
110                     e.printStackTrace();
50084d 111                     throw e;
7fd198 112                 }
113             }
114             SampleData sampleData = new SampleData();
115             sampleData.setMatrix(matrix);
116             sampleDataList.add(sampleData);
117         }
118         return sampleDataList;
119     }
120
121     /**
122      * getData
123      *
124      * @param columnItem
50084d 125      * @param pointMap
8bf553 126      * @param planMap
7fd198 127      * @return
128      * @throws Exception
129      */
8bf553 130     private List<DataValueVO> getData(ColumnItem columnItem, Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) throws Exception {
7fd198 131         List<DataValueVO> dataList = new ArrayList<>();
132         String paramType = columnItem.getParamType();
a4891a 133         switch (ModelParamType.getEumByCode(paramType)) {
134             case DATAPOINT:
7fd198 135                 ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
50084d 136                 queryDto.setPointNo(pointMap.get(columnItem.getParamId()).getPointNo());
7fd198 137                 queryDto.setStart(columnItem.getStartTime());
138                 queryDto.setEnd(columnItem.getEndTime());
536b8e 139                 List<ApiPointValueDTO> pointValueList = dataPointApi.queryPointHistoryValue(queryDto);
50084d 140                 if (CollectionUtils.isEmpty(pointValueList)) {
D 141                     break;
142                 }
a4891a 143                 dataList = pointValueList.stream().map(t -> {
214275 144                     DataValueVO vo = new DataValueVO();
145                     vo.setDataTime(t.getT());
146                     vo.setDataValue(t.getV());
147                     return vo;
148                 }).collect(Collectors.toList());
7fd198 149                 break;
19a4fb 150             case NORMALITEM:
D 151             case MERGEITEM:
033e69 152                 List<DataValueVO> predictValue = mmItemResultService.getPredictValue(columnItem.getParamId(), columnItem.getStartTime(), columnItem.getEndTime());
037fb7 153
50084d 154                 if (CollectionUtils.isEmpty(predictValue)) {
D 155                     break;
7fd198 156                 }
50084d 157                 dataList = predictValue;
7fd198 158                 break;
50084d 159             case PLAN:
D 160                 ApiDataQueryDTO queryPlanItemDto = new ApiDataQueryDTO();
8bf553 161                 queryPlanItemDto.setItemNo(planMap.get(columnItem.getParamId()).getItemNo());
50084d 162                 queryPlanItemDto.setStart(columnItem.getStartTime());
D 163                 queryPlanItemDto.setEnd(columnItem.getEndTime());
164                 List<ApiDataValueDTO> planValueList = planItemApi.queryPlanItemHistoryValue(queryPlanItemDto);
165                 if (CollectionUtils.isEmpty(planValueList)) {
166                     break;
167                 }
168                 dataList = planValueList.stream().map(t -> {
169                     DataValueVO vo = new DataValueVO();
170                     vo.setDataTime(t.getDataTime());
171                     vo.setDataValue(t.getDataValue());
172                     return vo;
173                 }).collect(Collectors.toList());
7fd198 174             default:
175                 break;
176         }
e9f7b1 177         // 避免生产环境日志过多,分级打印
9f09d4 178         log.debug("数据获取,columnItem:" + columnItem + ",dataList:" + dataList);
D 179         log.info("数据获取,columnItem:" + columnItem + ",dataListLength:" + dataList.size());
7fd198 180         return dataList;
181     }
182 }