package com.iailab.module.model.handler;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.iailab.common.utils.PythonUtil;
|
import com.iailab.module.mcs.dto.StModelDTO;
|
import com.iailab.module.mcs.dto.StModelRequestLogDTO;
|
import com.iailab.module.mcs.dto.StModelRunlogDTO;
|
import com.iailab.module.mcs.service.*;
|
import com.iailab.module.model.IAILModel;
|
import com.iailab.module.model.command.MLCommand;
|
import com.iailab.module.model.command.PyCommand;
|
import com.iailab.module.model.sample.constructor.SampleConstructor;
|
import com.iailab.module.model.sample.dto.SampleData;
|
import lombok.extern.slf4j.Slf4j;
|
import javax.annotation.Resource;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.*;
|
|
/**
|
* 浮选模型处理
|
*
|
* @author PanZhibao
|
* @Description
|
* @createTime 2023年05月16日 13:33:00
|
*/
|
@Slf4j
|
@Component
|
public class ModelHandler {
|
|
@Resource
|
private PyCommand pyCommand;
|
|
@Resource
|
private MLCommand mLCommand;
|
|
@Resource
|
private StModelService stModelService;
|
|
@Resource
|
private StModelSettingService stModelSettingService;
|
|
@Resource
|
private StModelRunlogService runlogService;
|
|
@Resource
|
private StModelResultService stModelResultService;
|
|
@Resource
|
private SampleConstructor sampleConstructor;
|
|
@Resource
|
private StModelRequestLogService stModelRequestLogService;
|
|
/**
|
* 运行模型
|
*
|
* @param modelCode
|
* @param runTime
|
* @return
|
* @throws Exception
|
*/
|
public Map<String, Object> run(String modelCode, Date runTime) throws Exception {
|
Map<String, Object> result = new HashMap<>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(runTime);
|
calendar.set(Calendar.SECOND, 0);
|
Date dataTime = calendar.getTime();
|
try {
|
// 1、查找模型
|
StModelDTO stModel = stModelService.getByCode(modelCode);
|
if (stModel == null) {
|
throw new Exception("modelCode错误!");
|
}
|
|
// 2、构建输入参数
|
List<SampleData> sampleDataList = sampleConstructor.constructSample(stModel.getId(), dataTime);
|
List<Object> params = new ArrayList<>();
|
if (!CollectionUtils.isEmpty(sampleDataList)) {
|
sampleDataList.forEach(item -> {
|
params.add(item.getMatrix());
|
});
|
}
|
|
// 3、构建配置参数
|
Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId());
|
|
// 4、执行算法
|
log.info("################ modelCode=" + modelCode + " ###################");
|
String[] command = pyCommand.getCommand(stModel, params, settings);
|
log.info("command=" + JSONArray.toJSONString(command));
|
result = PythonUtil.execPy(command);
|
log.info("result=" + JSON.toJSONString(result));
|
stModelResultService.addML(stModel.getId(), result, runTime);
|
stModelService.updateTime(stModel.getId(), runTime);
|
|
// 6、插入运行日志
|
String jsonString = dealResult(result);
|
this.addLog(stModel.getId(), modelCode, runTime, jsonString);
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
throw ex;
|
}
|
return result;
|
}
|
|
public String dealResult(Map<String, Object> result) {
|
ObjectMapper mapper = new ObjectMapper();
|
String jsonString = "";
|
try {
|
jsonString = mapper.writeValueAsString(result);
|
jsonString = jsonString.replace("mv_fl_kd_307", "307分流阀开度");
|
jsonString = jsonString.replace("mv_hj_water_valve_main_307", "315合介桶加水阀开度");
|
jsonString = jsonString.replace("mv_xj_water_valve_307", "333稀介桶加水阀开度");
|
jsonString = jsonString.replace("mv_hj_water_valve_small_307", "315合介桶密度微调阀开度");
|
jsonString = jsonString.replace("mv_mn_water_valve_307", "325煤泥桶加水阀开度");
|
|
jsonString = jsonString.replace("mv_fl_kd_308", "308分流阀开度");
|
jsonString = jsonString.replace("mv_hj_water_valve_main_308", "316合介桶加水阀开度");
|
jsonString = jsonString.replace("mv_xj_water_valve_308", "332稀介桶加水阀开度");
|
jsonString = jsonString.replace("mv_hj_water_valve_small_308", "316合介桶密度微调阀开度");
|
jsonString = jsonString.replace("mv_mn_water_valve_308", "325煤泥桶加水阀开度");
|
|
jsonString = jsonString.replace("mv_valve_fl_3318", "3318分流阀开度");
|
jsonString = jsonString.replace("mv_pump_hl_3318", "3308混料桶排水泵频率");
|
jsonString = jsonString.replace("mv_pump_hj_3318", "3344合介桶排水泵频率");
|
jsonString = jsonString.replace("mv_valve_hj_water_3318", "3344合介桶加水阀开度");
|
jsonString = jsonString.replace("mv_valve_jj_3318", "3344合介桶加介阀开度");
|
jsonString = jsonString.replace("mv_valve_xj_water_3318", "3348稀介桶加水阀开度");
|
jsonString = jsonString.replace("mv_pump_xj_3318", "3348稀介桶排水泵频率");
|
jsonString = jsonString.replace("mv_pump_mn_3318", "4402煤泥桶排水泵频率");
|
jsonString = jsonString.replace("mv_valve_mn_water_3318", "4402煤泥桶加水阀开度");
|
jsonString = jsonString.replace("target_md_3318", "3318目标合介密度");
|
|
|
jsonString = jsonString.replace("mv_valve_fl_3319", "3319分流阀开度");
|
jsonString = jsonString.replace("mv_pump_hl_3319", "3317混料桶排水泵频率");
|
jsonString = jsonString.replace("mv_pump_hj_3319", "3345合介桶排水泵频率");
|
jsonString = jsonString.replace("mv_valve_hj_water_3319", "3345合介桶加水阀开度");
|
jsonString = jsonString.replace("mv_valve_jj_3319", "3345合介桶加介阀开度");
|
jsonString = jsonString.replace("mv_valve_xj_water_3319", "3349稀介桶加水阀开度");
|
jsonString = jsonString.replace("mv_pump_xj_3319", "3349稀介桶排水泵频率");
|
jsonString = jsonString.replace("mv_pump_mn_3319", "4403煤泥桶排水泵频率");
|
jsonString = jsonString.replace("mv_valve_mn_water_3319", "4403煤泥桶加水阀开度");
|
jsonString = jsonString.replace("target_md_3319", "3319目标合介密度");
|
|
jsonString = jsonString.replace("status_first_start", "第一组风机启动信号");
|
jsonString = jsonString.replace("status_first_stop", "第一组风机停止信号");
|
jsonString = jsonString.replace("status_second_start", "第二组风机启动信号");
|
jsonString = jsonString.replace("status_second_stop", "第二组风机停止信号");
|
jsonString = jsonString.replace("mv_1_346", "346高压风机频率设定");
|
jsonString = jsonString.replace("mv_1_347", "347高压风机频率设定");
|
jsonString = jsonString.replace("mv_2_4454", "4454高压风机频率设定");
|
|
jsonString = jsonString.replace("Thickener_OF_XL", "501 浓缩池循环水池浊度预测值");
|
jsonString = jsonString.replace("Thickener_BF_XL", "501浓缩池出料浓度预测值");
|
jsonString = jsonString.replace("Thickener_H_XL", "501浓缩池污泥厚度预测值");
|
jsonString = jsonString.replace("Dos_yin_det1_XL", "507_1加药泵频率");
|
jsonString = jsonString.replace("Dos_yang_det1_XL", "508_1加药泵频率");
|
jsonString = jsonString.replace("Dos_yin_det2_XL", "507_2加药泵频率");
|
jsonString = jsonString.replace("Dos_yang_det2_XL", "508_2加药泵频率");
|
|
jsonString = jsonString.replace("Thickener_OF_QC", "浓缩池循环水池浊度预测值");
|
jsonString = jsonString.replace("Thickener_BF_QC", "浓缩池出料浓度预测值");
|
jsonString = jsonString.replace("Thickener_H_QC", "浓缩池污泥厚度预测值");
|
jsonString = jsonString.replace("Dos_yin_det_QC", "350加药系统频率");
|
jsonString = jsonString.replace("Dos_yang_det_QC", "351加药系统频率");
|
|
jsonString = jsonString.replace("UF_pumb1", "503底流泵启停");
|
jsonString = jsonString.replace("UF_pumb2", "502底流泵启停");
|
jsonString = jsonString.replace("zt4418_res", "4418启停信号");
|
jsonString = jsonString.replace("zt4419_res", "4419启停信号");
|
jsonString = jsonString.replace("zt4420_res", "4420启停信号");
|
jsonString = jsonString.replace("zt4421_res", "4421启停信号");
|
jsonString = jsonString.replace("zt4422_res", "4422启停信号");
|
jsonString = jsonString.replace("zt4428_res", "4428启停信号");
|
jsonString = jsonString.replace("zt4429_res", "4429启停信号");
|
jsonString = jsonString.replace("zt4430_res", "4430启停信号");
|
jsonString = jsonString.replace("zt4431_res", "4431启停信号");
|
jsonString = jsonString.replace("zt4432_res", "4432启停信号");
|
|
|
} catch (JsonProcessingException e) {
|
e.printStackTrace();
|
}
|
return jsonString;
|
}
|
|
/**
|
* 调用模型
|
*
|
* @param modelCode
|
* @param runTime
|
* @param sampleDataList
|
* @param appKey
|
* @return
|
* @throws Exception
|
*/
|
public Map<String, Object> run(String modelCode, Date runTime, List<double[][]> sampleDataList, String appKey) throws Exception {
|
Map<String, Object> result = new HashMap<>();
|
try {
|
// 1、查找模型
|
StModelDTO stModel = stModelService.getByCode(modelCode);
|
if (stModel == null) {
|
throw new Exception("modelCode错误!");
|
}
|
|
// 2、构建输入参数
|
List<Object> params = new ArrayList<>();
|
if (!CollectionUtils.isEmpty(sampleDataList)) {
|
sampleDataList.forEach(item -> {
|
params.add(item);
|
});
|
}
|
|
// 3、构建配置参数
|
Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId());
|
|
// 4、执行算法
|
log.info("################ modelCode=" + modelCode + " ###################");
|
String[] command = pyCommand.getCommand(stModel, params, settings);
|
log.info("command=" + JSONArray.toJSONString(command));
|
result = PythonUtil.execPy(command);
|
log.info("result=" + JSON.toJSONString(result));
|
|
// 5、插入调用日志
|
StModelRequestLogDTO logDto = new StModelRequestLogDTO();
|
logDto.setModelId(stModel.getId());
|
logDto.setAppKey(appKey);
|
logDto.setRequestTime(runTime);
|
logDto.setRequestParams(JSONArray.toJSONString(sampleDataList));
|
logDto.setResponseResult(JSONObject.toJSONString(result));
|
stModelRequestLogService.save(logDto);
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
throw ex;
|
}
|
return result;
|
}
|
|
/**
|
* 调用模型
|
*
|
* @param modelCode
|
* @param sampleDataList
|
* @return
|
* @throws Exception
|
*/
|
public Map<String, Object> run(String modelCode, List<double[][]> sampleDataList) throws Exception {
|
Map<String, Object> result = new HashMap<>();
|
try {
|
// 1、查找模型
|
StModelDTO stModel = stModelService.getByCode(modelCode);
|
if (stModel == null) {
|
throw new Exception("modelCode错误!");
|
}
|
|
// 2、构建输入参数
|
List<Object> params = new ArrayList<>();
|
if (!CollectionUtils.isEmpty(sampleDataList)) {
|
sampleDataList.forEach(item -> {
|
params.add(item);
|
});
|
}
|
|
// 3、构建配置参数
|
Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId());
|
|
// 4、执行算法
|
log.info("################ modelCode=" + modelCode + " ###################");
|
String[] command = pyCommand.getCommand(stModel, params, settings);
|
log.info("command=" + JSONArray.toJSONString(command));
|
result = PythonUtil.execPy(command);
|
log.info("result=" + JSON.toJSONString(result));
|
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
throw ex;
|
}
|
return result;
|
}
|
|
/**
|
* 调用模型
|
*
|
* @param modelCode
|
* @param sampleDataList
|
* @param settings
|
* @return
|
* @throws Exception
|
*/
|
public Map<String, Object> run(String modelCode, List<double[][]> sampleDataList, Map<String, Object> settings) throws Exception {
|
Map<String, Object> result = new HashMap<>();
|
try {
|
// 1、查找模型
|
StModelDTO stModel = stModelService.getByCode(modelCode);
|
if (stModel == null) {
|
throw new Exception("modelCode错误!");
|
}
|
|
// 2、构建输入参数
|
List<Object> params = new ArrayList<>();
|
if (!CollectionUtils.isEmpty(sampleDataList)) {
|
sampleDataList.forEach(item -> {
|
params.add(item);
|
});
|
}
|
|
// 4、执行算法
|
log.info("################ modelCode=" + modelCode + " ###################");
|
String[] command = pyCommand.getCommand(stModel, params, settings);
|
log.info("command=" + JSONArray.toJSONString(command));
|
result = PythonUtil.execPy(command);
|
log.info("result=" + JSON.toJSONString(result));
|
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
throw ex;
|
}
|
return result;
|
}
|
|
/**
|
* 构造IAILMDK.run()方法的newModelBean参数
|
*
|
* @param stModel
|
* @return
|
*/
|
private IAILModel composeNewModelBean(StModelDTO stModel) {
|
IAILModel newModelBean = new IAILModel();
|
newModelBean.setClassName(stModel.getClassName().trim());
|
newModelBean.setMethodName(stModel.getMethodName().trim());
|
//构造参数类型
|
String[] paArStr = stModel.getParamsStructure().trim().split(",");
|
Class<?>[] paramsArray = new Class[paArStr.length];
|
for (int i = 0; i < paArStr.length; i++) {
|
if ("[[D".equals(paArStr[i])) {
|
paramsArray[i] = double[][].class;
|
} else if ("Map".equals(paArStr[i]) || "java.util.HashMap".equals(paArStr[i])) {
|
paramsArray[i] = HashMap.class;
|
}
|
}
|
newModelBean.setParamsArray(paramsArray);
|
return newModelBean;
|
}
|
|
public void addLog(String modelId, String modelCode, Date runTime, String jsonString) {
|
StModelRunlogDTO logDto = new StModelRunlogDTO();
|
logDto.setModelid(modelId);
|
logDto.setRunTime(runTime);
|
logDto.setRunType(modelCode);
|
logDto.setRunResult(jsonString);
|
runlogService.add(logDto);
|
}
|
}
|