package com.iailab.module.model.mpk.controller.admin;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants;
|
import com.iailab.framework.common.pojo.CommonResult;
|
import com.iailab.module.model.mpk.common.MdkConstant;
|
import com.iailab.module.model.mpk.common.utils.Readtxt;
|
import com.iailab.module.model.mpk.dto.MdkDTO;
|
import io.swagger.v3.oas.annotations.Operation;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
import java.lang.reflect.Method;
|
import java.net.URL;
|
import java.net.URLClassLoader;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
|
import static com.iailab.framework.common.pojo.CommonResult.error;
|
import static com.iailab.framework.common.pojo.CommonResult.success;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2024年08月08日
|
*/
|
@RestController
|
@RequestMapping("/model/mpk/api")
|
public class MdkController {
|
@Value("${mpk.bak-file-path}")
|
private String mpkBakFilePath;
|
|
|
@PostMapping("run")
|
public CommonResult<String> run(@RequestBody MdkDTO dto) {
|
|
try {
|
File jarFile = new File(mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar");
|
if (!jarFile.exists()) {
|
throw new RuntimeException("jar包不存在,请先生成代码。jarPath:" + jarFile.getAbsolutePath());
|
}
|
File dllFile = new File(mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll");
|
if (!dllFile.exists()) {
|
throw new RuntimeException("dllw文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath());
|
}
|
// 加载jar包
|
loadJar(jarFile.getAbsolutePath());
|
// 加载dll
|
System.load(dllFile.getAbsolutePath());
|
} catch (Exception e) {
|
throw new RuntimeException("加载运行环境失败。");
|
}
|
|
System.out.println("runTime=" + System.currentTimeMillis());
|
try {
|
List<String> datas = dto.getDatas();
|
|
int paramLength = dto.getHasModel() ? datas.size() + 2 : datas.size() + 1;
|
Object[] paramsValueArray = new Object[paramLength];
|
Class<?>[] paramsArray = new Class[paramLength];
|
|
try {
|
for (int i = 0; i < datas.size(); i++) {
|
String json = datas.get(i);
|
JSONArray jsonArray = JSON.parseArray(json);
|
double[][] data = new double[jsonArray.size()][jsonArray.getJSONArray(0).size()];
|
for (int j = 0; j < jsonArray.size(); j++) {
|
for (int k = 0; k < jsonArray.getJSONArray(j).size(); k++) {
|
data[j][k] = jsonArray.getJSONArray(j).getDoubleValue(k);
|
}
|
}
|
paramsValueArray[i] = data;
|
paramsArray[i] = double[][].class;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(),"参数错误,请检查!");
|
}
|
|
if (dto.getHasModel()) {
|
paramsValueArray[datas.size()] = dto.getModel();
|
paramsValueArray[datas.size() + 1] = dto.getModelSettings();
|
paramsArray[datas.size()] = HashMap.class;
|
paramsArray[datas.size() + 1] = HashMap.class;
|
}else {
|
paramsValueArray[datas.size()] = dto.getModelSettings();
|
paramsArray[datas.size()] = HashMap.class;
|
}
|
|
Class<?> clazz = Class.forName(dto.getClassName());
|
Method method = clazz.getMethod(dto.getMethodName(), paramsArray);
|
HashMap result = (HashMap)method.invoke(clazz.newInstance(), paramsValueArray);
|
return success(JSON.toJSONString(result));
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
return error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"运行异常");
|
} finally {
|
System.gc();
|
}
|
}
|
|
@PostMapping("/import")
|
@Operation(summary = "导入参数")
|
public CommonResult<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
|
List<double[][]> datas = Readtxt.readMethodExcel(file);
|
List<String> result = new ArrayList<>();
|
if (!CollectionUtils.isEmpty(datas)) {
|
for (double[][] data : datas) {
|
if (data.length > 0) {
|
result.add(JSON.toJSONString(data));
|
}
|
}
|
}
|
return success(result);
|
}
|
|
private void loadJar(String jarPath) throws Exception {
|
File jarFile = new File(jarPath);
|
if (!jarFile.exists()) {
|
throw new Exception("jar沒有找到!");
|
} else {
|
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
boolean accessible = method.isAccessible();
|
method.setAccessible(true);
|
URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
|
URL url = jarFile.toURI().toURL();
|
method.invoke(classLoader, url);
|
method.setAccessible(accessible);
|
}
|
}
|
}
|