Jay
5 天以前 eca625c35d5ed64c98277d2f83963e46438f50ce
提交 | 用户 | 时间
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;
eca625 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
eca625 55     public ScheduleResultVO doSchedule(String schemeCode, Date scheduleTime, HashMap<String, Object> params) 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();
50084d 67             List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Schedule.name(), modelId, scheduleTime,scheduleScheme.getName());
07890e 68             log.info("构造模型输入样本消耗时长:" + (System.currentTimeMillis() - now) / 1000 + "秒");
054fb9 69             if (CollectionUtils.isEmpty(sampleDataList)) {
bbc1ee 70                 log.info("调度模型构造样本失败,schemeCode=" + schemeCode);
054fb9 71                 return null;
72             }
73
45520a 74             IAILModel newModelBean = composeNewModelBean(scheduleModel);
D 75             HashMap<String, Object> settings = getScheduleSettingsByModelId(modelId);
76             if (settings == null) {
77                 log.error("模型setting不存在,modelId=" + modelId);
78                 return null;
79             }
eca625 80             //如果输入参数中包含setting,则覆盖默认的setting
J 81             if (!params.isEmpty()){
82                 for (Map.Entry<String, Object> entry : params.entrySet()){
83                     if (settings.containsKey(entry.getKey())){
84                         settings.put(entry.getKey(), entry.getValue());
85                     }
86                 }
87             }
45520a 88             // 校验setting必须有pyFile,否则可能导致程序崩溃
D 89             if (!settings.containsKey(MdkConstant.PY_FILE_KEY)) {
90                 log.error("模型设置参数缺少必要信息【" + MdkConstant.PY_FILE_KEY +  "】,请重新上传模型!");
91                 return null;
92             }
93             int portLength = sampleDataList.size();
94             Object[] param2Values = new Object[portLength + 1];
95             for (int i = 0; i < portLength; i++) {
054fb9 96                 param2Values[i] = sampleDataList.get(i).getMatrix();
97             }
45520a 98             param2Values[portLength] = settings;
054fb9 99
45520a 100             log.info("#######################调度模型 " + scheduleModel.getModelName() + " ##########################");
07890e 101             log.info("参数: " + JSON.toJSONString(param2Values));
45520a 102             //IAILMDK.run
c204b3 103             HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, scheduleScheme.getMpkprojectid());
b2bb7d 104             if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT)) {
11a742 105                 log.info("模型结果异常:" + modelResult);
51c1c2 106             }
8a6b19 107             String statusCode = modelResult.get(CommonConstant.MDK_STATUS_CODE).toString();
11a742 108             HashMap<String, Object> result = new HashMap<>();
109             if (modelResult.containsKey(CommonConstant.MDK_RESULT) && modelResult.get(CommonConstant.MDK_RESULT) != null &&
110                     CommonConstant.MDK_STATUS_100.equals(modelResult.get(CommonConstant.MDK_STATUS_CODE).toString())) {
111                 result = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT);
112             }
054fb9 113             //打印结果
114             JSONObject jsonObjResult = new JSONObject();
b2bb7d 115             jsonObjResult.put("result", result);
054fb9 116             log.info(String.valueOf(jsonObjResult));
117
118             //5.返回调度结果
8a6b19 119             scheduleResult.setResultCode(statusCode);
b2bb7d 120             scheduleResult.setResult(result);
054fb9 121             scheduleResult.setModelId(modelId);
bbc1ee 122             scheduleResult.setSchemeId(scheduleScheme.getId());
054fb9 123             scheduleResult.setScheduleTime(scheduleTime);
124         } catch (Exception ex) {
125             log.error("IAILMDK.run()执行失败");
126             log.error(ex.getMessage());
127             log.error("调用发生异常,异常信息为:{}", ex);
128             ex.printStackTrace();
129         }
130         return scheduleResult;
131     }
132
133     /**
134      * 根据模型id获取参数map
135      *
136      * @param modelId
137      * @return
138      */
45520a 139     private HashMap<String, Object> getScheduleSettingsByModelId(String modelId) {
bbc1ee 140         List<StScheduleModelSettingEntity> list = stScheduleModelSettingService.getByModelId(modelId);
054fb9 141         if (CollectionUtils.isEmpty(list)) {
142             return null;
143         }
144         HashMap<String, Object> result = new HashMap<>();
bbc1ee 145         for (StScheduleModelSettingEntity entry : list) {
45520a 146             String valueType = entry.getValuetype().trim(); //去除两端空格
054fb9 147             if ("int".equals(valueType)) {
45520a 148                 int value = Integer.parseInt(entry.getValue());
054fb9 149                 result.put(entry.getKey(), value);
150             } else if ("double".equals(valueType)) {
45520a 151                 double value = Double.parseDouble(entry.getValue());
054fb9 152                 result.put(entry.getKey(), value);
153             } else if ("string".equals(valueType)) {
45520a 154                 String value = entry.getValue();
054fb9 155                 result.put(entry.getKey(), value);
156             } else if ("decimalArray".equals(valueType)) {
157                 JSONArray valueArray = JSONArray.parseArray(entry.getValue());
158                 double[] value = new double[valueArray.size()];
159                 for (int i = 0; i < valueArray.size(); i++) {
160                     value[i] = Double.parseDouble(valueArray.get(i).toString());
161                 }
162                 result.put(entry.getKey(), value);
163             } else if ("decimal".equals(valueType)) {
164                 double value = Double.parseDouble(entry.getValue());
165                 result.put(entry.getKey(), value);
166             }
167         }
168         return result;
169     }
45520a 170
D 171     private IAILModel composeNewModelBean(StScheduleModelEntity model) {
172         IAILModel newModelBean = new IAILModel();
173         newModelBean.setClassName(model.getClassName().trim());
174         newModelBean.setMethodName(model.getMethodName().trim());
175         //构造参数类型
176         Class<?>[] paramsArray = new Class[model.getPortLength() + 1];
177         for (int i = 0; i < model.getPortLength(); i++) {
178             paramsArray[i] = double[][].class;
179         }
180         paramsArray[model.getPortLength()] = HashMap.class;
181         newModelBean.setParamsArray(paramsArray);
182         //
183 //        HashMap<String, Object> dataMap = new HashMap<>();
184 //        newModelBean.setDataMap(dataMap);
185         return newModelBean;
186     }
054fb9 187 }