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