提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.mdk.sample; |
潘 |
2 |
|
75a848
|
3 |
import cn.hutool.core.date.DateUtil; |
D |
4 |
import com.iailab.module.data.api.ind.IndItemApi; |
|
5 |
import com.iailab.module.data.api.ind.dto.ApiIndItemDTO; |
|
6 |
import com.iailab.module.data.api.ind.dto.ApiIndItemQueryDTO; |
|
7 |
import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO; |
50084d
|
8 |
import com.iailab.module.data.api.plan.PlanItemApi; |
8bf553
|
9 |
import com.iailab.module.data.api.plan.dto.ApiPlanItemDTO; |
7fd198
|
10 |
import com.iailab.module.data.api.point.DataPointApi; |
潘 |
11 |
import com.iailab.module.data.api.point.dto.ApiPointDTO; |
|
12 |
import com.iailab.module.data.api.point.dto.ApiPointValueDTO; |
|
13 |
import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; |
50084d
|
14 |
import com.iailab.module.data.common.ApiDataQueryDTO; |
D |
15 |
import com.iailab.module.data.common.ApiDataValueDTO; |
1ab119
|
16 |
import com.iailab.module.model.common.utils.ASCIIUtil; |
a4891a
|
17 |
import com.iailab.module.model.mcs.pre.service.MmItemOutputService; |
c4b37d
|
18 |
import com.iailab.module.model.mcs.pre.service.MmItemResultJsonService; |
7fd198
|
19 |
import com.iailab.module.model.mcs.pre.service.MmItemResultService; |
a4891a
|
20 |
import com.iailab.module.model.mcs.pre.service.MmItemTypeService; |
潘 |
21 |
import com.iailab.module.model.mdk.common.enums.ModelParamType; |
7fd198
|
22 |
import com.iailab.module.model.mdk.sample.dto.ColumnItem; |
潘 |
23 |
import com.iailab.module.model.mdk.sample.dto.ColumnItemPort; |
|
24 |
import com.iailab.module.model.mdk.sample.dto.SampleData; |
|
25 |
import com.iailab.module.model.mdk.sample.dto.SampleInfo; |
|
26 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
50084d
|
27 |
import lombok.extern.slf4j.Slf4j; |
7fd198
|
28 |
import org.slf4j.Logger; |
潘 |
29 |
import org.slf4j.LoggerFactory; |
|
30 |
import org.springframework.beans.factory.annotation.Autowired; |
|
31 |
import org.springframework.stereotype.Component; |
50084d
|
32 |
import org.springframework.util.CollectionUtils; |
7fd198
|
33 |
|
fdcde1
|
34 |
import java.math.BigDecimal; |
7fd198
|
35 |
import java.util.*; |
214275
|
36 |
import java.util.stream.Collectors; |
7fd198
|
37 |
|
潘 |
38 |
/** |
|
39 |
* 预测样本数据构造 |
|
40 |
*/ |
50084d
|
41 |
@Slf4j |
7fd198
|
42 |
@Component |
潘 |
43 |
public class PredictSampleDataConstructor extends SampleDataConstructor { |
|
44 |
|
|
45 |
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
46 |
|
|
47 |
@Autowired |
|
48 |
private DataPointApi dataPointApi; |
50084d
|
49 |
|
D |
50 |
@Autowired |
|
51 |
private PlanItemApi planItemApi; |
7fd198
|
52 |
|
潘 |
53 |
@Autowired |
75a848
|
54 |
private IndItemApi indItemApi; |
D |
55 |
|
|
56 |
@Autowired |
7fd198
|
57 |
private MmItemResultService mmItemResultService; |
c4b37d
|
58 |
|
D |
59 |
@Autowired |
|
60 |
private MmItemResultJsonService mmItemResultJsonService; |
7fd198
|
61 |
|
潘 |
62 |
@Autowired |
a4891a
|
63 |
private MmItemTypeService mmItemTypeService; |
潘 |
64 |
|
|
65 |
@Autowired |
|
66 |
private MmItemOutputService mmItemOutputService; |
7fd198
|
67 |
|
潘 |
68 |
/** |
|
69 |
* alter by zfc 2020.11.24 修改数据样本构造方案:sampleInfo中数据已按爪子进行分类,但爪内数据为无序的, |
|
70 |
* 对爪内数据样本拼接:先基于modelParamOrder对项进行排序(重写comparator匿名函数),再逐项拼接 |
|
71 |
* |
|
72 |
* @param sampleInfo |
|
73 |
* @return |
|
74 |
*/ |
|
75 |
@Override |
bab433
|
76 |
public List<SampleData> prepareSampleData(SampleInfo sampleInfo) throws Exception { |
7fd198
|
77 |
List<SampleData> sampleDataList = new ArrayList<>(); |
50084d
|
78 |
Map<String, ApiPointDTO> pointMap = sampleInfo.getPointMap(); |
8bf553
|
79 |
Map<String, ApiPlanItemDTO> planMap = sampleInfo.getPlanMap(); |
75a848
|
80 |
Map<String, ApiIndItemDTO> indMap = sampleInfo.getIndMap(); |
1ab119
|
81 |
// 校验数据 |
D |
82 |
for (ColumnItemPort itemPort : sampleInfo.getColumnInfo()) { |
|
83 |
for (ColumnItem columnItem : itemPort.getColumnItemList()) { |
|
84 |
if (columnItem.getParamType().equals(ModelParamType.IND_ASCII.getCode())) { |
|
85 |
if (columnItem.getModelParamOrder() != 1 || itemPort.getColumnItemList().size() != 1) { |
|
86 |
throw new RuntimeException("模型输入数据异常:IND_ASCII类型输入独占一个端口;ParamPortOrder:" + columnItem.getModelParamPortOrder() + ",ParamOrder:" + columnItem.getModelParamOrder()); |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
} |
|
91 |
|
67f59a
|
92 |
int portIdx = 1; |
7fd198
|
93 |
//对每个爪分别进行计算 |
潘 |
94 |
for (ColumnItemPort entry : sampleInfo.getColumnInfo()) { |
1ab119
|
95 |
double[][] matrix = new double[0][0]; |
D |
96 |
// 特殊处理IND_ASCII类型 |
|
97 |
if (entry.getColumnItemList().get(0).getParamType().equals(ModelParamType.IND_ASCII.getCode())) { |
|
98 |
// 获取指标数据 |
|
99 |
ColumnItem columnItem = entry.getColumnItemList().get(0); |
|
100 |
ApiIndItemQueryDTO queryIndItemDTO = new ApiIndItemQueryDTO(); |
|
101 |
ApiIndItemDTO intItem = indMap.get(columnItem.getParamId()); |
|
102 |
queryIndItemDTO.setItemNo(intItem.getItemNo()); |
|
103 |
queryIndItemDTO.setStart(columnItem.getStartTime()); |
|
104 |
queryIndItemDTO.setEnd(columnItem.getEndTime()); |
|
105 |
List<ApiIndItemValueDTO> indItemValueList = indItemApi.queryIndItemHistoryValue(queryIndItemDTO); |
|
106 |
if (!CollectionUtils.isEmpty(indItemValueList)) { |
|
107 |
matrix = new double[entry.getDataLength()][0]; |
|
108 |
if (indItemValueList.size() > entry.getDataLength()) { |
b3674c
|
109 |
indItemValueList = indItemValueList.subList(0, entry.getDataLength()); |
fdcde1
|
110 |
} |
1ab119
|
111 |
for (int i = 0; i < indItemValueList.size(); i++) { |
D |
112 |
String stringValue = indItemValueList.get(i).getDataValue().toString(); |
|
113 |
double[] asciiArray = ASCIIUtil.stringToAsciiArray(stringValue); |
|
114 |
matrix[i] = asciiArray; |
|
115 |
} |
7fd198
|
116 |
} |
b3674c
|
117 |
} else { |
1ab119
|
118 |
//先依据爪内数据项的modelParamOrder进行排序——重写comparator匿名函数 |
D |
119 |
Collections.sort(entry.getColumnItemList(), new Comparator<ColumnItem>() { |
|
120 |
@Override |
|
121 |
public int compare(ColumnItem o1, ColumnItem o2) { |
|
122 |
return o1.getModelParamOrder() - o2.getModelParamOrder(); |
|
123 |
} |
|
124 |
}); |
fdcde1
|
125 |
|
1ab119
|
126 |
//默认都是double类型的数据,且按列向量进行拼接,默认初始值为0.0 |
D |
127 |
matrix = new double[entry.getDataLength()][entry.getColumnItemList().size()]; |
|
128 |
for (int i = 0; i < entry.getColumnItemList().size(); i++) { |
|
129 |
for (int j = 0; j < entry.getDataLength(); j++) { |
|
130 |
matrix[j][i] = -2.0; |
|
131 |
} |
7fd198
|
132 |
} |
潘 |
133 |
|
1ab119
|
134 |
//对每一项依次进行数据查询,然后将查询出的值赋给matrix对应的位置 |
D |
135 |
for (int i = 0; i < entry.getColumnItemList().size(); i++) { |
|
136 |
try { |
b3674c
|
137 |
List<DataValueVO> dataEntityList = getData(entry.getColumnItemList().get(i), pointMap, planMap, indMap); |
962879
|
138 |
|
1ab119
|
139 |
//补全数据 |
D |
140 |
ColumnItem columnItem = entry.getColumnItemList().get(i); |
b3674c
|
141 |
dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.getParamType(), columnItem.getGranularity()); |
7fd198
|
142 |
|
67f59a
|
143 |
|
1ab119
|
144 |
/** 如果数据取不满,把缺失的数据点放在后面 */ |
D |
145 |
if (dataEntityList != null && dataEntityList.size() != 0) { |
|
146 |
logger.info("设置matrix, i = " + i + ", size = " + dataEntityList.size()); |
67f59a
|
147 |
// 调整值 |
D |
148 |
double adjustVal = SampleInfo.getAdjustValueFromDeviation(portIdx, i + 1, sampleInfo.getDeviation()); |
1ab119
|
149 |
for (int k = 0; k < dataEntityList.size(); k++) { |
D |
150 |
Double dataValue = dataEntityList.get(k).getDataValue(); |
|
151 |
if (null != dataValue) { |
67f59a
|
152 |
// 用BigDecimal计算,解决double精度问题 |
D |
153 |
matrix[k][i] = BigDecimal.valueOf(dataValue).add(BigDecimal.valueOf(adjustVal)).doubleValue(); |
1ab119
|
154 |
} |
e691b9
|
155 |
} |
7fd198
|
156 |
} |
1ab119
|
157 |
} catch (Exception e) { |
D |
158 |
e.printStackTrace(); |
|
159 |
throw e; |
7fd198
|
160 |
} |
潘 |
161 |
} |
b3674c
|
162 |
|
潘 |
163 |
portIdx++; |
7fd198
|
164 |
} |
潘 |
165 |
SampleData sampleData = new SampleData(); |
|
166 |
sampleData.setMatrix(matrix); |
|
167 |
sampleDataList.add(sampleData); |
|
168 |
} |
|
169 |
return sampleDataList; |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* getData |
|
174 |
* |
|
175 |
* @param columnItem |
50084d
|
176 |
* @param pointMap |
8bf553
|
177 |
* @param planMap |
75a848
|
178 |
* @param indMap |
7fd198
|
179 |
* @return |
潘 |
180 |
* @throws Exception |
|
181 |
*/ |
75a848
|
182 |
private List<DataValueVO> getData(ColumnItem columnItem, Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap, Map<String, ApiIndItemDTO> indMap) throws Exception { |
7fd198
|
183 |
List<DataValueVO> dataList = new ArrayList<>(); |
潘 |
184 |
String paramType = columnItem.getParamType(); |
a4891a
|
185 |
switch (ModelParamType.getEumByCode(paramType)) { |
潘 |
186 |
case DATAPOINT: |
7fd198
|
187 |
ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
50084d
|
188 |
queryDto.setPointNo(pointMap.get(columnItem.getParamId()).getPointNo()); |
7fd198
|
189 |
queryDto.setStart(columnItem.getStartTime()); |
潘 |
190 |
queryDto.setEnd(columnItem.getEndTime()); |
536b8e
|
191 |
List<ApiPointValueDTO> pointValueList = dataPointApi.queryPointHistoryValue(queryDto); |
50084d
|
192 |
if (CollectionUtils.isEmpty(pointValueList)) { |
D |
193 |
break; |
|
194 |
} |
a4891a
|
195 |
dataList = pointValueList.stream().map(t -> { |
214275
|
196 |
DataValueVO vo = new DataValueVO(); |
潘 |
197 |
vo.setDataTime(t.getT()); |
|
198 |
vo.setDataValue(t.getV()); |
|
199 |
return vo; |
|
200 |
}).collect(Collectors.toList()); |
7fd198
|
201 |
break; |
19a4fb
|
202 |
case NORMALITEM: |
D |
203 |
case MERGEITEM: |
033e69
|
204 |
List<DataValueVO> predictValue = mmItemResultService.getPredictValue(columnItem.getParamId(), columnItem.getStartTime(), columnItem.getEndTime()); |
037fb7
|
205 |
|
50084d
|
206 |
if (CollectionUtils.isEmpty(predictValue)) { |
D |
207 |
break; |
7fd198
|
208 |
} |
50084d
|
209 |
dataList = predictValue; |
7fd198
|
210 |
break; |
50084d
|
211 |
case PLAN: |
D |
212 |
ApiDataQueryDTO queryPlanItemDto = new ApiDataQueryDTO(); |
8bf553
|
213 |
queryPlanItemDto.setItemNo(planMap.get(columnItem.getParamId()).getItemNo()); |
50084d
|
214 |
queryPlanItemDto.setStart(columnItem.getStartTime()); |
D |
215 |
queryPlanItemDto.setEnd(columnItem.getEndTime()); |
|
216 |
List<ApiDataValueDTO> planValueList = planItemApi.queryPlanItemHistoryValue(queryPlanItemDto); |
|
217 |
if (CollectionUtils.isEmpty(planValueList)) { |
|
218 |
break; |
|
219 |
} |
|
220 |
dataList = planValueList.stream().map(t -> { |
|
221 |
DataValueVO vo = new DataValueVO(); |
|
222 |
vo.setDataTime(t.getDataTime()); |
|
223 |
vo.setDataValue(t.getDataValue()); |
|
224 |
return vo; |
|
225 |
}).collect(Collectors.toList()); |
af5604
|
226 |
break; |
75a848
|
227 |
case IND: |
D |
228 |
ApiIndItemQueryDTO queryIndItemDTO = new ApiIndItemQueryDTO(); |
|
229 |
ApiIndItemDTO intItem = indMap.get(columnItem.getParamId()); |
|
230 |
queryIndItemDTO.setItemNo(intItem.getItemNo()); |
|
231 |
queryIndItemDTO.setStart(columnItem.getStartTime()); |
|
232 |
queryIndItemDTO.setEnd(columnItem.getEndTime()); |
|
233 |
List<ApiIndItemValueDTO> indItemValueList = indItemApi.queryIndItemHistoryValue(queryIndItemDTO); |
|
234 |
if (CollectionUtils.isEmpty(indItemValueList)) { |
|
235 |
break; |
|
236 |
} |
|
237 |
|
|
238 |
dataList = indItemValueList.stream().map(t -> { |
|
239 |
DataValueVO vo = new DataValueVO(); |
|
240 |
vo.setDataTime(DateUtil.parse(t.getDataTime())); |
d6606a
|
241 |
vo.setDataValue(Double.valueOf(t.getDataValue().toString())); |
75a848
|
242 |
return vo; |
D |
243 |
}).collect(Collectors.toList()); |
1ab119
|
244 |
break; |
7fd198
|
245 |
default: |
潘 |
246 |
break; |
|
247 |
} |
e9f7b1
|
248 |
// 避免生产环境日志过多,分级打印 |
9f09d4
|
249 |
log.debug("数据获取,columnItem:" + columnItem + ",dataList:" + dataList); |
D |
250 |
log.info("数据获取,columnItem:" + columnItem + ",dataListLength:" + dataList.size()); |
7fd198
|
251 |
return dataList; |
潘 |
252 |
} |
|
253 |
} |