| | |
| | | package com.iailab.module.model.mdk.predict.impl; |
| | | |
| | | import com.iailab.module.data.api.point.DataPointApi; |
| | | import com.iailab.module.data.api.point.dto.ApiPointDTO; |
| | | import com.iailab.module.data.enums.DataPointFreq; |
| | | import com.iailab.module.model.mdk.common.enums.ItemPredictStatus; |
| | | import com.alibaba.fastjson.JSON; |
| | | 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.sche.service.StAdjustResultService; |
| | | import com.iailab.module.model.mdk.common.exceptions.ItemInvokeException; |
| | | import com.iailab.module.model.mdk.factory.ItemEntityFactory; |
| | | import com.iailab.module.model.mdk.factory.PredictItemFactory; |
| | | import com.iailab.module.model.mdk.predict.PredictItemHandler; |
| | | import com.iailab.module.model.mdk.predict.PredictResultHandler; |
| | | import com.iailab.module.model.mdk.vo.DataValueVO; |
| | | import com.iailab.module.model.mdk.vo.ItemVO; |
| | | import com.iailab.module.model.mdk.vo.MmItemOutputVO; |
| | | import com.iailab.module.model.mdk.vo.PredictResultVO; |
| | | import com.iailab.module.model.mdk.vo.StAdjustDeviationDTO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.sql.Timestamp; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | private ItemEntityFactory itemEntityFactory; |
| | | |
| | | @Autowired |
| | | private DataPointApi dataPointApi; |
| | | private MmItemResultJsonService mmItemResultJsonService; |
| | | |
| | | @Autowired |
| | | private PredictItemFactory predictItemFactory; |
| | | private MmItemOutputService mmItemOutputService; |
| | | |
| | | @Autowired |
| | | private PredictResultHandler predictResultHandler; |
| | | private StAdjustResultService stAdjustResultService; |
| | | |
| | | /** |
| | | * MergeItem预测 |
| | | * |
| | | * @param predictTime |
| | | * @param predictItemDto |
| | | * @return |
| | | * @throws ItemInvokeException |
| | | */ |
| | | @Override |
| | | public PredictResultVO predict(Date predictTime, ItemVO predictItemDto) |
| | | public PredictResultVO predict(Date predictTime, ItemVO predictItemDto, Map<String, double[]> predictValueMap) |
| | | throws ItemInvokeException { |
| | | PredictResultVO predictResult = new PredictResultVO(); |
| | | ItemPredictStatus itemStatus = ItemPredictStatus.PREDICTING; |
| | | String itemId = predictItemDto.getId(); |
| | | try { |
| | | String expression = itemEntityFactory.getMergeItem(itemId).getExpression(); |
| | | int predictLength = itemEntityFactory.getItemById(itemId).getPredictLength(); |
| | | double[][] predictResultMat = new double[predictLength][1]; |
| | | Map<String, List<DataValueVO>> predictValueMap = new HashMap<>(); |
| | | String[] mathItem = expression.split("[\\+ \\-]"); |
| | | double[] predictResultMat = new double[predictLength]; |
| | | String[] mathOutPutId = expression.split("[\\+ \\-]"); |
| | | ArrayList<Character> operator = new ArrayList<>(); |
| | | for (int i = 0; i < expression.length(); i++) { |
| | | if (expression.charAt(i)=='+' || expression.charAt(i)=='-'){ |
| | | if (expression.charAt(i) == '+' || expression.charAt(i) == '-') { |
| | | operator.add(expression.charAt(i)); |
| | | } |
| | | } |
| | | String[] compositionItem = expression.split(String.valueOf("&".toCharArray())); |
| | | //是否为计算预测项 |
| | | if (mathItem.length > 1) { |
| | | for (String itemNo : mathItem) { |
| | | if (itemNo.length() > 4) { |
| | | Date endTime = predictTime; |
| | | ItemVO itemEntity = itemEntityFactory.getItemByItemNo(itemNo); |
| | | List<MmItemOutputVO> outPutList = itemEntityFactory.getOutPutByItemId(itemEntity.getId()); |
| | | ApiPointDTO pointEntity = dataPointApi.getInfoById(outPutList.get(0).getPointId()); |
| | | |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(endTime); |
| | | calendar.add(Calendar.SECOND, (predictLength - 1) * DataPointFreq.getEumByCode(pointEntity.getMinfreqid()).getValue()); |
| | | endTime = new Timestamp(calendar.getTime().getTime()); |
| | | List<DataValueVO> predictValueList = predictResultHandler.getPredictValueByItemNo(itemNo, predictTime, endTime); |
| | | if (predictValueList.size() != predictLength) { |
| | | log.debug("merge项融合失败:缺少子项预测数据,对应子项ItemNo=" + itemNo); |
| | | return null; |
| | | } |
| | | predictValueMap.put(itemNo, predictValueList); |
| | | } |
| | | } |
| | | if (mathOutPutId.length > 1) { |
| | | for (Integer i = 0; i < predictLength; i++) { |
| | | double sum =0.0; |
| | | sum = predictValueMap.get(mathItem[0]).get(i).getDataValue(); |
| | | for (int j = 1; j < mathItem.length; j++) { |
| | | if (operator.get(j-1)=='+') |
| | | {sum += predictValueMap.get(mathItem[j]).get(i).getDataValue();} |
| | | if (operator.get(j-1)=='-') |
| | | {sum -= predictValueMap.get(mathItem[j]).get(i).getDataValue();} |
| | | double sum = 0.0; |
| | | sum = predictValueMap.get(mathOutPutId[0])[i]; |
| | | for (int j = 1; j < mathOutPutId.length; j++) { |
| | | if (operator.get(j - 1) == '+') { |
| | | sum += predictValueMap.get(mathOutPutId[j])[i]; |
| | | } |
| | | if (operator.get(j - 1) == '-') { |
| | | sum -= predictValueMap.get(mathOutPutId[j])[i]; |
| | | } |
| | | } |
| | | predictResultMat[i][0] = sum; |
| | | predictResultMat[i] = sum; |
| | | } |
| | | } |
| | | //是否为组合预测项 |
| | | if (compositionItem.length > 1) { |
| | | Map<String, PredictResultVO> predictResultMap = new HashMap<>(); |
| | | Integer columnTotalNumber = 0; |
| | | Integer rowNumber = 0; |
| | | for (String itemNo : compositionItem) { |
| | | PredictItemHandler predictItem = (PredictItemHandler) predictItemFactory.create(itemEntityFactory. |
| | | getItemByItemNo(itemNo).getId()); |
| | | predictResult = predictItem.predict(predictTime, predictItemDto); |
| | | columnTotalNumber += Integer.valueOf(predictResult.getPredictMatrix().length); |
| | | predictResultMap.put(itemNo, predictItem.predict(predictTime, predictItemDto)); |
| | | } |
| | | double[][] matrix = new double[columnTotalNumber][1]; |
| | | for (String itemNo : compositionItem) { |
| | | for (Integer i = 0; i < predictResultMap.get(itemNo).getPredictMatrix().length; i++) { |
| | | matrix[rowNumber][0] = predictResultMap.get(itemNo).getPredictMatrix()[i][0]; |
| | | rowNumber++; |
| | | } |
| | | } |
| | | predictResult.setPredictMatrix(matrix); |
| | | } |
| | | predictResult.setPredictId(itemId); |
| | | predictResult.setPredictMatrix(predictResultMat); |
| | | List<MmItemOutputEntity> outputServiceByItemid = mmItemOutputService.getByItemid(itemId); |
| | | if (!CollectionUtils.isEmpty(outputServiceByItemid)) { |
| | | Map<MmItemOutputEntity, double[]> predictMatrixs = new HashMap<>(); |
| | | predictMatrixs.put(outputServiceByItemid.get(0), predictResultMat); |
| | | predictResult.setPredictMatrixs(predictMatrixs); |
| | | } |
| | | predictResult.setPredictTime(predictTime); |
| | | //预测项预测成功的状态 |
| | | itemStatus = ItemPredictStatus.SUCCESS; |
| | | } catch (Exception e) { |
| | | //预测项预测失败的状态 |
| | | itemStatus = ItemPredictStatus.FAILED; |
| | | log.debug("merge项预测失败,itemId:" + itemId); |
| | | log.error("merge项预测失败,itemId:" + itemId); |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } |
| | | log.debug("预测完成,itemId:" + itemId + ",itemStatus:" + itemStatus.getValue()); |
| | | log.info("merge项预测完成,itemId:" + itemId + ",结果:" + JSON.toJSONString(predictResult)); |
| | | return predictResult; |
| | | } |
| | | |
| | | @Override |
| | | public PredictResultVO predictAdjust(Date predictTime, ItemVO predictItemDto, List<StAdjustDeviationDTO> deviationList) |
| | | throws ItemInvokeException { |
| | | PredictResultVO predictResult = new PredictResultVO(); |
| | | String itemId = predictItemDto.getId(); |
| | | try { |
| | | String expression = itemEntityFactory.getMergeItem(itemId).getExpression(); |
| | | int predictLength = itemEntityFactory.getItemById(itemId).getPredictLength(); |
| | | double[] predictResultMat = new double[predictLength]; |
| | | String[] mathOutPutId = expression.split("[\\+ \\-]"); |
| | | ArrayList<Character> operator = new ArrayList<>(); |
| | | for (int i = 0; i < expression.length(); i++) { |
| | | if (expression.charAt(i) == '+' || expression.charAt(i) == '-') { |
| | | operator.add(expression.charAt(i)); |
| | | } |
| | | } |
| | | Map<String, double[]> predictValueMap = new HashMap<>(); |
| | | for (int k = 0; k < mathOutPutId.length; k++) { |
| | | String outPutId = mathOutPutId[k]; |
| | | double[] outPutValue = stAdjustResultService.getSimpleData(outPutId, predictTime, predictLength); |
| | | if (outPutValue == null) { |
| | | outPutValue = mmItemResultJsonService.getSimpleData(outPutId, predictTime, predictLength); |
| | | } |
| | | predictValueMap.put(outPutId, outPutValue); |
| | | } |
| | | |
| | | //是否为计算预测项 |
| | | if (mathOutPutId.length > 1) { |
| | | for (int i = 0; i < predictLength; i++) { |
| | | double sum = predictValueMap.get(mathOutPutId[0])[i]; |
| | | for (int j = 1; j < mathOutPutId.length; j++) { |
| | | if (operator.get(j - 1) == '+') { |
| | | sum += predictValueMap.get(mathOutPutId[j])[i]; |
| | | } |
| | | if (operator.get(j - 1) == '-') { |
| | | sum -= predictValueMap.get(mathOutPutId[j])[i]; |
| | | } |
| | | } |
| | | predictResultMat[i] = sum; |
| | | } |
| | | } |
| | | predictResult.setPredictId(itemId); |
| | | List<MmItemOutputEntity> outputServiceByItemid = mmItemOutputService.getByItemid(itemId); |
| | | if (!CollectionUtils.isEmpty(outputServiceByItemid)) { |
| | | Map<MmItemOutputEntity, double[]> predictMatrixs = new HashMap<>(); |
| | | predictMatrixs.put(outputServiceByItemid.get(0), predictResultMat); |
| | | predictResult.setPredictMatrixs(predictMatrixs); |
| | | } |
| | | predictResult.setPredictTime(predictTime); |
| | | } catch (Exception e) { |
| | | log.error("merge项预测失败,itemId:" + itemId); |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } |
| | | log.info("merge项预测完成,itemId:" + itemId + ",结果:" + JSON.toJSONString(predictResult)); |
| | | return predictResult; |
| | | } |
| | | } |