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