提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.controller.admin; |
D |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; |
|
6 |
import com.iailab.framework.common.pojo.CommonResult; |
1fea3e
|
7 |
import com.iailab.module.model.mpk.common.MdkConstant; |
449017
|
8 |
import com.iailab.module.model.mpk.common.utils.Readtxt; |
D |
9 |
import com.iailab.module.model.mpk.dto.MdkDTO; |
|
10 |
import io.swagger.v3.oas.annotations.Operation; |
1fea3e
|
11 |
import org.springframework.beans.factory.annotation.Value; |
449017
|
12 |
import org.springframework.util.CollectionUtils; |
D |
13 |
import org.springframework.web.bind.annotation.*; |
|
14 |
import org.springframework.web.multipart.MultipartFile; |
|
15 |
|
1fea3e
|
16 |
import java.io.File; |
449017
|
17 |
import java.lang.reflect.Method; |
1fea3e
|
18 |
import java.net.URL; |
D |
19 |
import java.net.URLClassLoader; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.HashMap; |
|
22 |
import java.util.List; |
449017
|
23 |
|
D |
24 |
import static com.iailab.framework.common.pojo.CommonResult.error; |
|
25 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
26 |
|
|
27 |
/** |
|
28 |
* @author PanZhibao |
|
29 |
* @Description |
|
30 |
* @createTime 2024年08月08日 |
|
31 |
*/ |
|
32 |
@RestController |
|
33 |
@RequestMapping("/model/mpk/api") |
|
34 |
public class MdkController { |
1fea3e
|
35 |
@Value("${mpk.bak-file-path}") |
D |
36 |
private String mpkBakFilePath; |
|
37 |
|
|
38 |
|
449017
|
39 |
@PostMapping("run") |
D |
40 |
public CommonResult<String> run(@RequestBody MdkDTO dto) { |
1fea3e
|
41 |
|
D |
42 |
try { |
|
43 |
File jarFile = new File(mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar"); |
|
44 |
if (!jarFile.exists()) { |
|
45 |
throw new RuntimeException("jar包不存在,请先生成代码。jarPath:" + jarFile.getAbsolutePath()); |
|
46 |
} |
|
47 |
File dllFile = new File(mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll"); |
|
48 |
if (!dllFile.exists()) { |
|
49 |
throw new RuntimeException("dllw文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath()); |
|
50 |
} |
|
51 |
// 加载jar包 |
|
52 |
loadJar(jarFile.getAbsolutePath()); |
|
53 |
// 加载dll |
|
54 |
System.load(dllFile.getAbsolutePath()); |
|
55 |
} catch (Exception e) { |
|
56 |
throw new RuntimeException("加载运行环境失败。"); |
|
57 |
} |
|
58 |
|
449017
|
59 |
System.out.println("runTime=" + System.currentTimeMillis()); |
D |
60 |
try { |
|
61 |
List<String> datas = dto.getDatas(); |
|
62 |
|
|
63 |
int paramLength = dto.getHasModel() ? datas.size() + 2 : datas.size() + 1; |
|
64 |
Object[] paramsValueArray = new Object[paramLength]; |
|
65 |
Class<?>[] paramsArray = new Class[paramLength]; |
|
66 |
|
|
67 |
try { |
|
68 |
for (int i = 0; i < datas.size(); i++) { |
|
69 |
String json = datas.get(i); |
|
70 |
JSONArray jsonArray = JSON.parseArray(json); |
|
71 |
double[][] data = new double[jsonArray.size()][jsonArray.getJSONArray(0).size()]; |
|
72 |
for (int j = 0; j < jsonArray.size(); j++) { |
|
73 |
for (int k = 0; k < jsonArray.getJSONArray(j).size(); k++) { |
|
74 |
data[j][k] = jsonArray.getJSONArray(j).getDoubleValue(k); |
|
75 |
} |
|
76 |
} |
|
77 |
paramsValueArray[i] = data; |
|
78 |
paramsArray[i] = double[][].class; |
|
79 |
} |
|
80 |
} catch (Exception e) { |
|
81 |
e.printStackTrace(); |
|
82 |
return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(),"参数错误,请检查!"); |
|
83 |
} |
|
84 |
|
|
85 |
if (dto.getHasModel()) { |
|
86 |
paramsValueArray[datas.size()] = dto.getModel(); |
|
87 |
paramsValueArray[datas.size() + 1] = dto.getModelSettings(); |
|
88 |
paramsArray[datas.size()] = HashMap.class; |
|
89 |
paramsArray[datas.size() + 1] = HashMap.class; |
|
90 |
}else { |
|
91 |
paramsValueArray[datas.size()] = dto.getModelSettings(); |
|
92 |
paramsArray[datas.size()] = HashMap.class; |
|
93 |
} |
|
94 |
|
|
95 |
Class<?> clazz = Class.forName(dto.getClassName()); |
|
96 |
Method method = clazz.getMethod(dto.getMethodName(), paramsArray); |
|
97 |
HashMap result = (HashMap)method.invoke(clazz.newInstance(), paramsValueArray); |
|
98 |
return success(JSON.toJSONString(result)); |
|
99 |
} catch (Exception ex) { |
|
100 |
ex.printStackTrace(); |
|
101 |
return error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"运行异常"); |
|
102 |
} finally { |
|
103 |
System.gc(); |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
@PostMapping("/import") |
|
108 |
@Operation(summary = "导入参数") |
|
109 |
public CommonResult<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws Exception { |
|
110 |
List<double[][]> datas = Readtxt.readMethodExcel(file); |
|
111 |
List<String> result = new ArrayList<>(); |
|
112 |
if (!CollectionUtils.isEmpty(datas)) { |
|
113 |
for (double[][] data : datas) { |
|
114 |
if (data.length > 0) { |
|
115 |
result.add(JSON.toJSONString(data)); |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
return success(result); |
|
120 |
} |
1fea3e
|
121 |
|
D |
122 |
private void loadJar(String jarPath) throws Exception { |
|
123 |
File jarFile = new File(jarPath); |
|
124 |
if (!jarFile.exists()) { |
|
125 |
throw new Exception("jar沒有找到!"); |
|
126 |
} else { |
|
127 |
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); |
|
128 |
boolean accessible = method.isAccessible(); |
|
129 |
method.setAccessible(true); |
|
130 |
URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader(); |
|
131 |
URL url = jarFile.toURI().toURL(); |
|
132 |
method.invoke(classLoader, url); |
|
133 |
method.setAccessible(accessible); |
|
134 |
} |
|
135 |
} |
449017
|
136 |
} |