Jay
2025-02-24 c3c7a6918f0e2dfe597c339117e4185b641be95f
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java
@@ -151,6 +151,103 @@
    }
    /**
     * 预测,模拟调整
     *
     * @param predictTime
     * @param predictModel
     * @param itemName
     * @param itemNo
     * @return
     * @throws ModelInvokeException
     */
    @Override
    public synchronized PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel,String itemName,String itemNo, double[][] deviation) throws ModelInvokeException {
        PredictResultVO result = new PredictResultVO();
        if (predictModel == null) {
            throw new ModelInvokeException("modelEntity is null");
        }
        String modelId = predictModel.getId();
        try {
            List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Predict.name(), modelId, predictTime, itemName, new HashMap<>());
            String modelPath = predictModel.getModelpath();
            if (modelPath == null) {
                log.info("模型路径不存在,modelId=" + modelId);
                return null;
            }
            IAILModel newModelBean = composeNewModelBean(predictModel);
            HashMap<String, Object> settings = getPredictSettingsByModelId(modelId);
            // 校验setting必须有pyFile,否则可能导致程序崩溃
            if (!settings.containsKey(MdkConstant.PY_FILE_KEY)) {
                throw new RuntimeException("模型设置参数缺少必要信息【" + MdkConstant.PY_FILE_KEY +  "】,请重新上传模型!");
            }
            if (settings == null) {
                log.error("模型setting不存在,modelId=" + modelId);
                return null;
            }
            int portLength = sampleDataList.size();
            Object[] param2Values = new Object[portLength + 2];
            for (int i = 0; i < portLength; i++) {
                param2Values[i] = sampleDataList.get(i).getMatrix();
            }
            param2Values[portLength] = newModelBean.getDataMap().get("models");
            param2Values[portLength + 1] = settings;
            log.info("####################### 模拟调整 "+ "【itemId:" + predictModel.getItemid() + ",itemName:" + itemName + ",itemNo:" + itemNo + "】 ##########################");
            log.info("参数: " + JSON.toJSONString(param2Values));
            //IAILMDK.run
            HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, predictModel.getMpkprojectid());
            //打印结果
            log.info("预测模型计算完成:modelId=" + modelId + ",modelName=" + predictModel.getMethodname() + ",modelResult=" + JSON.toJSONString(modelResult));
            //判断模型结果
            if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT) ||
                    !modelResult.get(CommonConstant.MDK_STATUS_CODE).toString().equals(CommonConstant.MDK_STATUS_100)) {
                throw new ModelResultErrorException("模型结果异常:" + modelResult);
            }
            modelResult = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT);
            List<MmItemOutputEntity> itemOutputList = mmItemOutputService.getByItemid(predictModel.getItemid());
            Map<MmItemOutputEntity, double[]> predictMatrixs = new HashMap<>();
            for (MmItemOutputEntity output : itemOutputList) {
                if (!modelResult.containsKey(output.getResultstr())) {
                    continue;
                }
                OutResultType outResultType = OutResultType.getEumByCode(output.getResultType());
                switch (outResultType) {
                    case D1:
                        double[] temp1 = (double[]) modelResult.get(output.getResultstr());
                        predictMatrixs.put(output, temp1);
                        break;
                    case D2:
                        double[][] temp2 = (double[][]) modelResult.get(output.getResultstr());
                        double[] tempColumn = new double[temp2.length];
                        for (int i = 0; i < tempColumn.length; i++) {
                            tempColumn[i] = temp2[i][output.getResultIndex()];
                        }
                        predictMatrixs.put(output, tempColumn);
                        break;
                    case D:
                        Double temp3 = (Double) modelResult.get(output.getResultstr());
                        predictMatrixs.put(output, new double[]{temp3});
                        break;
                    default:
                        break;
                }
            }
            result.setPredictMatrixs(predictMatrixs);
            result.setModelResult(modelResult);
            result.setPredictTime(predictTime);
        } catch (ModelResultErrorException ex) {
            log.error("模型结果异常", ex);
            throw ex;
        } catch (Exception ex) {
            log.error("调用发生异常,异常信息为:{0}", ex.getMessage());
            throw new ModelInvokeException(ex.getMessage());
        }
        return result;
    }
    /**
     * 构造IAILMDK.run()方法的newModelBean参数
     *
     * @param predictModel