package com.iailab.module.model.mdk.sample; import cn.hutool.core.date.DateUtil; import com.iailab.module.data.api.ind.IndItemApi; import com.iailab.module.data.api.ind.dto.ApiIndItemDTO; import com.iailab.module.data.api.ind.dto.ApiIndItemQueryDTO; import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO; 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.utils.ASCIIUtil; 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.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.math.BigDecimal; 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 IndItemApi indItemApi; @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(); Map<String, ApiIndItemDTO> indMap = sampleInfo.getIndMap(); // æ ¡éªŒæ•°æ® for (ColumnItemPort itemPort : sampleInfo.getColumnInfo()) { for (ColumnItem columnItem : itemPort.getColumnItemList()) { if (columnItem.getParamType().equals(ModelParamType.IND_ASCII.getCode())) { if (columnItem.getModelParamOrder() != 1 || itemPort.getColumnItemList().size() != 1) { throw new RuntimeException("模型输入数æ®å¼‚常:IND_ASCII类型输入独å 一个端å£ï¼›ParamPortOrder:" + columnItem.getModelParamPortOrder() + ",ParamOrder:" + columnItem.getModelParamOrder()); } } } } int portIdx = 1; //对æ¯ä¸ªçˆªåˆ†åˆ«è¿›è¡Œè®¡ç®— for (ColumnItemPort entry : sampleInfo.getColumnInfo()) { double[][] matrix = new double[0][0]; // 特殊处ç†IND_ASCII类型 if (entry.getColumnItemList().get(0).getParamType().equals(ModelParamType.IND_ASCII.getCode())) { // 获å–æŒ‡æ ‡æ•°æ® ColumnItem columnItem = entry.getColumnItemList().get(0); ApiIndItemQueryDTO queryIndItemDTO = new ApiIndItemQueryDTO(); ApiIndItemDTO intItem = indMap.get(columnItem.getParamId()); queryIndItemDTO.setItemNo(intItem.getItemNo()); queryIndItemDTO.setStart(columnItem.getStartTime()); queryIndItemDTO.setEnd(columnItem.getEndTime()); List<ApiIndItemValueDTO> indItemValueList = indItemApi.queryIndItemHistoryValue(queryIndItemDTO); if (!CollectionUtils.isEmpty(indItemValueList)) { matrix = new double[entry.getDataLength()][0]; if (indItemValueList.size() > entry.getDataLength()) { indItemValueList = indItemValueList.subList(0, entry.getDataLength()); } for (int i = 0; i < indItemValueList.size(); i++) { String stringValue = indItemValueList.get(i).getDataValue().toString(); double[] asciiArray = ASCIIUtil.stringToAsciiArray(stringValue); matrix[i] = asciiArray; } } } else { //å…ˆä¾æ®çˆªå†…æ•°æ®é¡¹çš„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 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, indMap); //è¡¥å…¨æ•°æ® ColumnItem columnItem = entry.getColumnItemList().get(i); dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.getParamType(), columnItem.getGranularity()); /** 如果数æ®å–ä¸æ»¡ï¼ŒæŠŠç¼ºå¤±çš„æ•°æ®ç‚¹æ”¾åœ¨åŽé¢ */ if (CollectionUtils.isEmpty(dataEntityList)) { continue; } logger.info("设置matrix, i = " + i + ", size = " + dataEntityList.size()); // 调整值 double adjustVal = SampleInfo.getAdjustValueFromDeviation(portIdx, i + 1, sampleInfo.getDeviation()); for (int k = 0; k < dataEntityList.size(); k++) { Double dataValue = dataEntityList.get(k).getDataValue(); if (null == dataValue) { continue; } // 用BigDecimal计算,解决double精度问题 matrix[k][i] = BigDecimal.valueOf(dataValue).add(BigDecimal.valueOf(adjustVal)).doubleValue(); } } catch (Exception e) { e.printStackTrace(); throw e; } } portIdx++; } SampleData sampleData = new SampleData(); sampleData.setMatrix(matrix); sampleDataList.add(sampleData); } return sampleDataList; } /** * getData * * @param columnItem * @param pointMap * @param planMap * @param indMap * @return * @throws Exception */ private List<DataValueVO> getData(ColumnItem columnItem, Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap, Map<String, ApiIndItemDTO> indMap) 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: List<DataValueVO> predictValue = mmItemResultService.getPredictValue(columnItem.getParamId(), 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()); break; case IND: ApiIndItemQueryDTO queryIndItemDTO = new ApiIndItemQueryDTO(); ApiIndItemDTO intItem = indMap.get(columnItem.getParamId()); queryIndItemDTO.setItemNo(intItem.getItemNo()); queryIndItemDTO.setStart(columnItem.getStartTime()); queryIndItemDTO.setEnd(columnItem.getEndTime()); List<ApiIndItemValueDTO> indItemValueList = indItemApi.queryIndItemHistoryValue(queryIndItemDTO); if (CollectionUtils.isEmpty(indItemValueList)) { break; } dataList = indItemValueList.stream().map(t -> { DataValueVO vo = new DataValueVO(); vo.setDataTime(DateUtil.parse(t.getDataTime())); vo.setDataValue(Double.valueOf(t.getDataValue().toString())); return vo; }).collect(Collectors.toList()); break; default: break; } // é¿å…ç”Ÿäº§çŽ¯å¢ƒæ—¥å¿—è¿‡å¤šï¼Œåˆ†çº§æ‰“å° log.debug("æ•°æ®èŽ·å–,columnItem:" + columnItem + ",dataList:" + dataList); log.info("æ•°æ®èŽ·å–,columnItem:" + columnItem + ",dataListLength:" + dataList.size()); return dataList; } }