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