dengzedong
2024-11-11 b2aca2d1f4d9d23d7d2ff018f60faa3b3706f9f0
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mdk.sample;
2
3 import com.iailab.framework.common.util.object.ConvertUtils;
4 import com.iailab.module.data.api.point.DataPointApi;
5 import com.iailab.module.data.api.point.dto.ApiPointDTO;
6 import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
7 import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
8 import com.iailab.module.model.mcs.pre.service.MmItemResultService;
9 import com.iailab.module.model.mdk.factory.ItemEntityFactory;
10 import com.iailab.module.model.mdk.sample.dto.ColumnItem;
11 import com.iailab.module.model.mdk.sample.dto.ColumnItemPort;
12 import com.iailab.module.model.mdk.sample.dto.SampleData;
13 import com.iailab.module.model.mdk.sample.dto.SampleInfo;
14 import com.iailab.module.model.mdk.vo.DataValueVO;
15 import com.iailab.module.model.mdk.vo.MmItemOutputVO;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Component;
20
21 import java.math.BigDecimal;
22 import java.util.*;
23
24 /**
25  * 预测样本数据构造
26  */
27 @Component
28 public class PredictSampleDataConstructor extends SampleDataConstructor {
29
30     private Logger logger = LoggerFactory.getLogger(getClass());
31
32     @Autowired
33     private DataPointApi dataPointApi;
34
35     @Autowired
36     private MmItemResultService mmItemResultService;
37
38     @Autowired
39     private ItemEntityFactory itemEntityFactory;
40
41     /**
42      * alter by zfc 2020.11.24 修改数据样本构造方案:sampleInfo中数据已按爪子进行分类,但爪内数据为无序的,
43      * 对爪内数据样本拼接:先基于modelParamOrder对项进行排序(重写comparator匿名函数),再逐项拼接
44      *
45      * @param sampleInfo
46      * @return
47      */
48     @Override
49     public List<SampleData> prepareSampleData(SampleInfo sampleInfo) {
50         List<SampleData> sampleDataList = new ArrayList<>();
51         //对每个爪分别进行计算
52         int deviationIndex = 0;
53         for (ColumnItemPort entry : sampleInfo.getColumnInfo()) {
54             //先依据爪内数据项的modelParamOrder进行排序——重写comparator匿名函数
55             Collections.sort(entry.getColumnItemList(), new Comparator<ColumnItem>() {
56                 @Override
57                 public int compare(ColumnItem o1, ColumnItem o2) {
58                     return o1.getModelParamOrder() - o2.getModelParamOrder();
59                 }
60             });
61
62             //默认都是double类型的数据,且按列向量进行拼接,默认初始值为0.0
63             double[][] matrix = new double[entry.getDataLength()][entry.getColumnItemList().size()];
64             for (int i = 0; i < entry.getColumnItemList().size(); i++) {
65                 for (int j = 0; j < entry.getDataLength(); j++) {
66                     matrix[j][i] = -2.0;
67                 }
68             }
69
70             //找出对应的调整值
71             BigDecimal[] deviationItem = null;
72             if (sampleInfo.getDeviation() != null && sampleInfo.getDeviation().length > 0) {
73                 deviationItem = sampleInfo.getDeviation()[deviationIndex];
74             }
75             deviationIndex++;
76
77             //对每一项依次进行数据查询,然后将查询出的值赋给matrix对应的位置
78             for (int i = 0; i < entry.getColumnItemList().size(); i++) {
79                 try {
80                     List<DataValueVO> dataEntityList = getData(entry.getColumnItemList().get(i));
81                     //设置调整值
82                     if (deviationItem != null && deviationItem.length > 0) {
83                         logger.info("设置调整值, i = " + i);
84                         if (deviationItem[i] != null && deviationItem[i].compareTo(BigDecimal.ZERO) != 0) {
85                             for (int dataKey = 1; dataKey < dataEntityList.size(); dataKey++) {
86                                 DataValueVO item = dataEntityList.get(dataKey);
87                                 item.setDataValue(item.getDataValue() + deviationItem[i].doubleValue());
88                             }
89                         }
90                     }
91                     //补全数据
92                     ColumnItem columnItem = entry.getColumnItemList().get(i);
b2aca2 93 //                    dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.getEndTime(), columnItem.granularity);
D 94                     dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.paramId,columnItem.getParamType());
7fd198 95
96                     /** 如果数据取不满,把缺失的数据点放在后面 */
97                     if (dataEntityList != null && dataEntityList.size() != 0) {
98                         logger.info("设置matrix, i = " + i + ", size = " + dataEntityList.size());
99                         for (int k = 0; k < dataEntityList.size(); k++) {
100                             matrix[k][i] = dataEntityList.get(k).getDataValue();
101                         }
102                     }
103                 } catch (Exception e) {
104                     e.printStackTrace();
105                 }
106             }
107             SampleData sampleData = new SampleData();
108             sampleData.setMatrix(matrix);
109             sampleDataList.add(sampleData);
110         }
111         return sampleDataList;
112     }
113
114     /**
115      * getData
116      *
117      * @param columnItem
118      * @return
119      * @throws Exception
120      */
121     private List<DataValueVO> getData(ColumnItem columnItem) throws Exception {
122         List<DataValueVO> dataList = new ArrayList<>();
123         String paramType = columnItem.getParamType();
124         switch (paramType) {
125             case "DATAPOINT":
b2aca2 126                 ApiPointDTO point = dataPointApi.getInfoById(columnItem.getParamId());
7fd198 127                 ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
128                 queryDto.setPointNo(point.getPointNo());
129                 queryDto.setStart(columnItem.getStartTime());
130                 queryDto.setEnd(columnItem.getEndTime());
536b8e 131                 List<ApiPointValueDTO> pointValueList = dataPointApi.queryPointHistoryValue(queryDto);
7fd198 132                 dataList = ConvertUtils.sourceToTarget(pointValueList, DataValueVO.class);
133                 break;
134             case "PREDICTITEM":
135                 MmItemOutputVO outPut = itemEntityFactory.getItemOutPutById(columnItem.getId());
136                 dataList = mmItemResultService.getPredictValue(outPut.getId(),
137                         columnItem.getStartTime(), columnItem.getEndTime());
138                 if (dataList == null) {
139                     throw new Exception("没有预测值");
140                 }
141                 break;
142
143
144             default:
145                 break;
146         }
147         return dataList;
148     }
149 }