修改部分规则代码

This commit is contained in:
zouju
2026-02-09 11:15:26 +08:00
parent a78781782e
commit e07b11ef95
3 changed files with 74 additions and 3 deletions

View File

@@ -1,8 +1,14 @@
package com.solution.web.controller.algo; package com.solution.web.controller.algo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
@@ -37,6 +43,51 @@ public class AlgorithmController extends BaseController {
@Autowired @Autowired
private IAlgorithmService algorithmService; private IAlgorithmService algorithmService;
/**
* 导出规则列表
*/
@ApiOperation("运行python程序")
@PostMapping("/run")
public Object run(@RequestBody Algorithm algorithm) {
List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
ObjectMapper objectMapper = new ObjectMapper();
for (Algorithm al : list) {
String codePath = al.getCodePath();
try {
String jsonInput = objectMapper.writeValueAsString(al);
String pythonExe = "E:\\Apps\\anaconda3\\python.exe";
ProcessBuilder pb = new ProcessBuilder(pythonExe, codePath);
Process process = pb.start();
try (OutputStream os = process.getOutputStream()) {
os.write(jsonInput.getBytes(StandardCharsets.UTF_8));
os.flush();
}
String result;
// 修改你 Java 代码中的这一段
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
result = reader.lines().collect(Collectors.joining("\n"));
}
int exitCode = process.waitFor();
if (exitCode != 0) {
return "Python执行失败错误码" + exitCode;
}
return objectMapper.readTree(result);
} catch (Exception e) {
e.printStackTrace();
return "执行异常: " + e.getMessage();
}
}
return "未找到算法";
}
/** /**
* 查询规则列表 * 查询规则列表
*/ */
@@ -56,7 +107,7 @@ public class AlgorithmController extends BaseController {
@PreAuthorize("@ss.hasPermi('algo:algorithm:export')") @PreAuthorize("@ss.hasPermi('algo:algorithm:export')")
@Log(title = "规则", businessType = BusinessType.EXPORT) @Log(title = "规则", businessType = BusinessType.EXPORT)
@PostMapping("/export") @PostMapping("/export")
public void export(HttpServletResponse response, Algorithm algorithm) { public void export(HttpServletResponse response, @RequestBody Algorithm algorithm) {
List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm); List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
ExcelUtil<Algorithm> util = new ExcelUtil<Algorithm>(Algorithm.class); ExcelUtil<Algorithm> util = new ExcelUtil<Algorithm>(Algorithm.class);
util.exportExcel(response, list, "规则数据"); util.exportExcel(response, list, "规则数据");

View File

@@ -34,6 +34,10 @@ public class Algorithm
@Excel(name = "算法描述") @Excel(name = "算法描述")
private String description; private String description;
/** 算法配置 */
@Excel(name = "算法配置")
private String algoConfig;
/** 算法参数定义信息 */ /** 算法参数定义信息 */
private List<AlgorithmParam> algorithmParamList; private List<AlgorithmParam> algorithmParamList;
@@ -87,6 +91,16 @@ public class Algorithm
return description; return description;
} }
public void setAlgoConfig(String algoConfig)
{
this.algoConfig = algoConfig;
}
public String getAlgoConfig()
{
return algoConfig;
}
public List<AlgorithmParam> getAlgorithmParamList() public List<AlgorithmParam> getAlgorithmParamList()
{ {
return algorithmParamList; return algorithmParamList;
@@ -105,6 +119,7 @@ public class Algorithm
.append("type", getType()) .append("type", getType())
.append("codePath", getCodePath()) .append("codePath", getCodePath())
.append("description", getDescription()) .append("description", getDescription())
.append("algoConfig", getAlgoConfig())
.append("algorithmParamList", getAlgorithmParamList()) .append("algorithmParamList", getAlgorithmParamList())
.toString(); .toString();
} }

View File

@@ -10,6 +10,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="type" column="type" /> <result property="type" column="type" />
<result property="codePath" column="code_path" /> <result property="codePath" column="code_path" />
<result property="description" column="description" /> <result property="description" column="description" />
<result property="algoConfig" column="algo_config" />
</resultMap> </resultMap>
<resultMap id="AlgorithmAlgorithmParamResult" type="Algorithm" extends="AlgorithmResult"> <resultMap id="AlgorithmAlgorithmParamResult" type="Algorithm" extends="AlgorithmResult">
@@ -25,7 +26,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap> </resultMap>
<sql id="selectAlgorithmVo"> <sql id="selectAlgorithmVo">
select id, name, type, code_path, description from algorithm select id, name, type, code_path, description, algo_config from algorithm
</sql> </sql>
<select id="selectAlgorithmList" parameterType="Algorithm" resultMap="AlgorithmAlgorithmParamResult"> <select id="selectAlgorithmList" parameterType="Algorithm" resultMap="AlgorithmAlgorithmParamResult">
@@ -35,11 +36,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="type != null and type != ''"> and type = #{type}</if> <if test="type != null and type != ''"> and type = #{type}</if>
<if test="codePath != null and codePath != ''"> and code_path = #{codePath}</if> <if test="codePath != null and codePath != ''"> and code_path = #{codePath}</if>
<if test="description != null and description != ''"> and description = #{description}</if> <if test="description != null and description != ''"> and description = #{description}</if>
<if test="algoConfig != null and algoConfig != ''"> and algo_config = #{algoConfig}</if>
</where> </where>
</select> </select>
<select id="selectAlgorithmById" parameterType="Long" resultMap="AlgorithmAlgorithmParamResult"> <select id="selectAlgorithmById" parameterType="Long" resultMap="AlgorithmAlgorithmParamResult">
select id, name, type, code_path, description select id, name, type, code_path, description, algo_config
from algorithm from algorithm
where id = #{id} where id = #{id}
</select> </select>
@@ -57,12 +59,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="type != null and type != ''">type,</if> <if test="type != null and type != ''">type,</if>
<if test="codePath != null">code_path,</if> <if test="codePath != null">code_path,</if>
<if test="description != null">description,</if> <if test="description != null">description,</if>
<if test="algoConfig != null">algo_config,</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">#{name},</if> <if test="name != null and name != ''">#{name},</if>
<if test="type != null and type != ''">#{type},</if> <if test="type != null and type != ''">#{type},</if>
<if test="codePath != null">#{codePath},</if> <if test="codePath != null">#{codePath},</if>
<if test="description != null">#{description},</if> <if test="description != null">#{description},</if>
<if test="algoConfig != null">#{algoConfig},</if>
</trim> </trim>
</insert> </insert>
@@ -73,6 +77,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="type != null and type != ''">type = #{type},</if> <if test="type != null and type != ''">type = #{type},</if>
<if test="codePath != null">code_path = #{codePath},</if> <if test="codePath != null">code_path = #{codePath},</if>
<if test="description != null">description = #{description},</if> <if test="description != null">description = #{description},</if>
<if test="algoConfig != null">algo_config = #{algoConfig},</if>
</trim> </trim>
where id = #{id} where id = #{id}
</update> </update>