潘志宝
5 天以前 6c2ff1c7c8e0aba3af92110b76687b78f8278e86
提交 | 用户 | 时间
054fb9 1 package com.iailab.module.model.mdk.schedule.impl;
2
07890e 3 import com.alibaba.fastjson.JSON;
054fb9 4 import com.alibaba.fastjson.JSONArray;
5 import com.alibaba.fastjson.JSONObject;
6 import com.iail.model.IAILModel;
51c1c2 7 import com.iailab.module.model.common.enums.CommonConstant;
054fb9 8 import com.iailab.module.model.mcs.sche.entity.StScheduleModelEntity;
bbc1ee 9 import com.iailab.module.model.mcs.sche.entity.StScheduleModelSettingEntity;
10 import com.iailab.module.model.mcs.sche.entity.StScheduleSchemeEntity;
054fb9 11 import com.iailab.module.model.mcs.sche.service.StScheduleModelService;
bbc1ee 12 import com.iailab.module.model.mcs.sche.service.StScheduleModelSettingService;
13 import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService;
054fb9 14 import com.iailab.module.model.mdk.common.enums.TypeA;
15 import com.iailab.module.model.mdk.common.exceptions.ModelInvokeException;
16 import com.iailab.module.model.mdk.sample.SampleConstructor;
17 import com.iailab.module.model.mdk.sample.dto.SampleData;
18 import com.iailab.module.model.mdk.schedule.ScheduleModelHandler;
19 import com.iailab.module.model.mdk.vo.ScheduleResultVO;
45520a 20 import com.iailab.module.model.mpk.common.MdkConstant;
51c1c2 21 import com.iailab.module.model.mpk.common.utils.DllUtils;
054fb9 22 import lombok.extern.slf4j.Slf4j;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Component;
25 import org.springframework.util.CollectionUtils;
26
27 import java.text.MessageFormat;
45520a 28 import java.util.Date;
D 29 import java.util.HashMap;
30 import java.util.List;
6c2ff1 31 import java.util.Map;
054fb9 32
33 /**
34  * @author PanZhibao
35  * @Description
36  * @createTime 2024年09月05日
37  */
38 @Slf4j
39 @Component
40 public class ScheduleModelHandlerImpl implements ScheduleModelHandler {
41
42     @Autowired
bbc1ee 43     private StScheduleSchemeService stScheduleSchemeService;
44
45     @Autowired
054fb9 46     private StScheduleModelService stScheduleModelService;
47
48     @Autowired
bbc1ee 49     private StScheduleModelSettingService stScheduleModelSettingService;
054fb9 50
51     @Autowired
52     private SampleConstructor sampleConstructor;
53
54     @Override
6c2ff1 55     public ScheduleResultVO doSchedule(String schemeCode, Date scheduleTime, Map<Integer, Integer> dynamicDataLength) throws ModelInvokeException {
054fb9 56         ScheduleResultVO scheduleResult = new ScheduleResultVO();
bbc1ee 57         StScheduleSchemeEntity scheduleScheme = stScheduleSchemeService.getByCode(schemeCode);
b425df 58         StScheduleModelEntity scheduleModel = stScheduleModelService.get(scheduleScheme.getModelId());
bbc1ee 59         if (scheduleModel == null) {
054fb9 60             throw new ModelInvokeException(MessageFormat.format("{0},modelId={1}",
bbc1ee 61                     ModelInvokeException.errorGetModelEntity, scheduleModel.getId()));
054fb9 62         }
bbc1ee 63         String modelId = scheduleModel.getId();
054fb9 64         try {
65             //1.根据模型id构造模型输入样本
07890e 66             long now = System.currentTimeMillis();
6c2ff1 67             List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Schedule.name(), modelId, scheduleTime,
68                     scheduleScheme.getName(), dynamicDataLength);
07890e 69             log.info("构造模型输入样本消耗时长:" + (System.currentTimeMillis() - now) / 1000 + "秒");
054fb9 70             if (CollectionUtils.isEmpty(sampleDataList)) {
bbc1ee 71                 log.info("调度模型构造样本失败,schemeCode=" + schemeCode);
054fb9 72                 return null;
73             }
74
45520a 75             IAILModel newModelBean = composeNewModelBean(scheduleModel);
D 76             HashMap<String, Object> settings = getScheduleSettingsByModelId(modelId);
77             if (settings == null) {
78                 log.error("模型setting不存在,modelId=" + modelId);
79                 return null;
80             }
81             // 校验setting必须有pyFile,否则可能导致程序崩溃
82             if (!settings.containsKey(MdkConstant.PY_FILE_KEY)) {
83                 log.error("模型设置参数缺少必要信息【" + MdkConstant.PY_FILE_KEY +  "】,请重新上传模型!");
84                 return null;
85             }
86             int portLength = sampleDataList.size();
87             Object[] param2Values = new Object[portLength + 1];
88             for (int i = 0; i < portLength; i++) {
054fb9 89                 param2Values[i] = sampleDataList.get(i).getMatrix();
90             }
45520a 91             param2Values[portLength] = settings;
054fb9 92
45520a 93             log.info("#######################调度模型 " + scheduleModel.getModelName() + " ##########################");
07890e 94             log.info("参数: " + JSON.toJSONString(param2Values));
45520a 95             //IAILMDK.run
c204b3 96             HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, scheduleScheme.getMpkprojectid());
b2bb7d 97             if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT)) {
11a742 98                 log.info("模型结果异常:" + modelResult);
51c1c2 99             }
8a6b19 100             String statusCode = modelResult.get(CommonConstant.MDK_STATUS_CODE).toString();
11a742 101             HashMap<String, Object> result = new HashMap<>();
102             if (modelResult.containsKey(CommonConstant.MDK_RESULT) && modelResult.get(CommonConstant.MDK_RESULT) != null &&
103                     CommonConstant.MDK_STATUS_100.equals(modelResult.get(CommonConstant.MDK_STATUS_CODE).toString())) {
104                 result = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT);
105             }
054fb9 106             //打印结果
107             JSONObject jsonObjResult = new JSONObject();
b2bb7d 108             jsonObjResult.put("result", result);
054fb9 109             log.info(String.valueOf(jsonObjResult));
110
111             //5.返回调度结果
8a6b19 112             scheduleResult.setResultCode(statusCode);
b2bb7d 113             scheduleResult.setResult(result);
054fb9 114             scheduleResult.setModelId(modelId);
bbc1ee 115             scheduleResult.setSchemeId(scheduleScheme.getId());
054fb9 116             scheduleResult.setScheduleTime(scheduleTime);
117         } catch (Exception ex) {
118             log.error("IAILMDK.run()执行失败");
119             log.error(ex.getMessage());
120             log.error("调用发生异常,异常信息为:{}", ex);
121             ex.printStackTrace();
122         }
123         return scheduleResult;
124     }
125
126     /**
127      * 根据模型id获取参数map
128      *
129      * @param modelId
130      * @return
131      */
45520a 132     private HashMap<String, Object> getScheduleSettingsByModelId(String modelId) {
bbc1ee 133         List<StScheduleModelSettingEntity> list = stScheduleModelSettingService.getByModelId(modelId);
054fb9 134         if (CollectionUtils.isEmpty(list)) {
135             return null;
136         }
137         HashMap<String, Object> result = new HashMap<>();
bbc1ee 138         for (StScheduleModelSettingEntity entry : list) {
45520a 139             String valueType = entry.getValuetype().trim(); //去除两端空格
054fb9 140             if ("int".equals(valueType)) {
45520a 141                 int value = Integer.parseInt(entry.getValue());
054fb9 142                 result.put(entry.getKey(), value);
143             } else if ("double".equals(valueType)) {
45520a 144                 double value = Double.parseDouble(entry.getValue());
054fb9 145                 result.put(entry.getKey(), value);
146             } else if ("string".equals(valueType)) {
45520a 147                 String value = entry.getValue();
054fb9 148                 result.put(entry.getKey(), value);
149             } else if ("decimalArray".equals(valueType)) {
150                 JSONArray valueArray = JSONArray.parseArray(entry.getValue());
151                 double[] value = new double[valueArray.size()];
152                 for (int i = 0; i < valueArray.size(); i++) {
153                     value[i] = Double.parseDouble(valueArray.get(i).toString());
154                 }
155                 result.put(entry.getKey(), value);
156             } else if ("decimal".equals(valueType)) {
157                 double value = Double.parseDouble(entry.getValue());
158                 result.put(entry.getKey(), value);
159             }
160         }
161         return result;
162     }
45520a 163
D 164     private IAILModel composeNewModelBean(StScheduleModelEntity model) {
165         IAILModel newModelBean = new IAILModel();
166         newModelBean.setClassName(model.getClassName().trim());
167         newModelBean.setMethodName(model.getMethodName().trim());
168         //构造参数类型
169         Class<?>[] paramsArray = new Class[model.getPortLength() + 1];
170         for (int i = 0; i < model.getPortLength(); i++) {
171             paramsArray[i] = double[][].class;
172         }
173         paramsArray[model.getPortLength()] = HashMap.class;
174         newModelBean.setParamsArray(paramsArray);
175         //
176 //        HashMap<String, Object> dataMap = new HashMap<>();
177 //        newModelBean.setDataMap(dataMap);
178         return newModelBean;
179     }
054fb9 180 }