dengzedong
3 天以前 7f0bcd00c556889ba890e5e68c681f1b5d4267e1
提交 | 用户 | 时间
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;
054fb9 31
32 /**
33  * @author PanZhibao
34  * @Description
35  * @createTime 2024年09月05日
36  */
37 @Slf4j
38 @Component
39 public class ScheduleModelHandlerImpl implements ScheduleModelHandler {
40
41     @Autowired
bbc1ee 42     private StScheduleSchemeService stScheduleSchemeService;
43
44     @Autowired
054fb9 45     private StScheduleModelService stScheduleModelService;
46
47     @Autowired
bbc1ee 48     private StScheduleModelSettingService stScheduleModelSettingService;
054fb9 49
50     @Autowired
51     private SampleConstructor sampleConstructor;
52
53     @Override
bbc1ee 54     public ScheduleResultVO doSchedule(String schemeCode, Date scheduleTime) throws ModelInvokeException {
054fb9 55         ScheduleResultVO scheduleResult = new ScheduleResultVO();
bbc1ee 56         StScheduleSchemeEntity scheduleScheme = stScheduleSchemeService.getByCode(schemeCode);
b425df 57         StScheduleModelEntity scheduleModel = stScheduleModelService.get(scheduleScheme.getModelId());
bbc1ee 58         if (scheduleModel == null) {
054fb9 59             throw new ModelInvokeException(MessageFormat.format("{0},modelId={1}",
bbc1ee 60                     ModelInvokeException.errorGetModelEntity, scheduleModel.getId()));
054fb9 61         }
bbc1ee 62         String modelId = scheduleModel.getId();
054fb9 63         try {
64             //1.根据模型id构造模型输入样本
07890e 65             long now = System.currentTimeMillis();
054fb9 66             List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Schedule.name(), modelId, scheduleTime);
07890e 67             log.info("构造模型输入样本消耗时长:" + (System.currentTimeMillis() - now) / 1000 + "秒");
054fb9 68             if (CollectionUtils.isEmpty(sampleDataList)) {
bbc1ee 69                 log.info("调度模型构造样本失败,schemeCode=" + schemeCode);
054fb9 70                 return null;
71             }
72
45520a 73             IAILModel newModelBean = composeNewModelBean(scheduleModel);
D 74             HashMap<String, Object> settings = getScheduleSettingsByModelId(modelId);
75             if (settings == null) {
76                 log.error("模型setting不存在,modelId=" + modelId);
77                 return null;
78             }
79             // 校验setting必须有pyFile,否则可能导致程序崩溃
80             if (!settings.containsKey(MdkConstant.PY_FILE_KEY)) {
81                 log.error("模型设置参数缺少必要信息【" + MdkConstant.PY_FILE_KEY +  "】,请重新上传模型!");
82                 return null;
83             }
84             int portLength = sampleDataList.size();
85             Object[] param2Values = new Object[portLength + 1];
86             for (int i = 0; i < portLength; i++) {
054fb9 87                 param2Values[i] = sampleDataList.get(i).getMatrix();
88             }
45520a 89             param2Values[portLength] = settings;
054fb9 90
45520a 91             log.info("#######################调度模型 " + scheduleModel.getModelName() + " ##########################");
07890e 92 //            JSONObject jsonObjNewModelBean = new JSONObject();
D 93 //            jsonObjNewModelBean.put("newModelBean", newModelBean);
94 //            log.info(String.valueOf(jsonObjNewModelBean));
95 //            JSONObject jsonObjParam2Values = new JSONObject();
96 //            jsonObjParam2Values.put("param2Values", param2Values);
97             log.info("参数: " + JSON.toJSONString(param2Values));
054fb9 98
45520a 99             //IAILMDK.run
c204b3 100             HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, scheduleScheme.getMpkprojectid());
51c1c2 101             if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT) ||
102                     !modelResult.get(CommonConstant.MDK_STATUS_CODE).toString().equals(CommonConstant.MDK_STATUS_100)) {
103                 throw new RuntimeException("模型结果异常:" + modelResult);
104             }
c204b3 105             modelResult = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT);
054fb9 106
107             //打印结果
108             JSONObject jsonObjResult = new JSONObject();
c204b3 109             jsonObjResult.put("result", modelResult);
054fb9 110             log.info(String.valueOf(jsonObjResult));
111
112             //5.返回调度结果
c204b3 113             scheduleResult.setResult(modelResult);
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 }