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