算法创建

This commit is contained in:
2025-06-05 11:51:01 +08:00
parent b926506ede
commit 99c291ca09
4 changed files with 76 additions and 3 deletions

View File

@ -73,5 +73,16 @@ public class AlgorithmInfoController {
algorithmInfoService.save(algorithmInfo); algorithmInfoService.save(algorithmInfo);
return OptResult.success("算法创建成功"); return OptResult.success("算法创建成功");
} }
/**
* 算法运行
*/
@PostMapping("/run/{id}")
@Operation(summary = "运行")
public OptResult run(@PathVariable Long id){
log.info("运行",id);
algorithmInfoService.run(id);
return OptResult.success("运行成功");
}
} }

View File

@ -29,4 +29,8 @@ public interface AlgorithmInfoMapper {
@Delete("DELETE FROM algorithm_info WHERE id = #{id}") @Delete("DELETE FROM algorithm_info WHERE id = #{id}")
int deleteById(Long id); int deleteById(Long id);
@Select("select description from algorithm_info where id = #{id}")
String getDescriptionById(Long id);
@Select("select algorithm_file from algorithm_info where id = #{id}")
String getFileById(Long id);
} }

View File

@ -14,4 +14,5 @@ public interface AlgorithmInfoService {
void save(AlgorithmInfo algorithmInfo); void save(AlgorithmInfo algorithmInfo);
void run(Long id);
} }

View File

@ -8,6 +8,9 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
@ -72,11 +75,65 @@ public class AlgorithmInfoServiceImpl implements AlgorithmInfoService {
//查找表里是否有重复的算法,如果有则报错 //查找表里是否有重复的算法,如果有则报错
AlgorithmInfo duplicateName = algorithmInfoMapper.selectByName(algorithmName); AlgorithmInfo duplicateName = algorithmInfoMapper.selectByName(algorithmName);
if (duplicateName != null){ if (duplicateName != null){
throw new RuntimeException("算法已存在"); throw new RuntimeException("算法已存在,请去修改算法");
} }
//todo 算法文件分布式存入分布式存储中
//todo 算法文件分布式存入分布式存储中
algorithmInfoMapper.insert(algorithmInfo); algorithmInfoMapper.insert(algorithmInfo);
} }
/**
* 算法执行
* @param id
*/
@Override
public void run(Long id) {
String description = algorithmInfoMapper.getDescriptionById(id);
/*//拿到传入的描述,并且用逗号分隔
String[] commaParts = description.split(",");
List<String> results = new ArrayList<>();
for (String part : commaParts) {
String[] semicolonParts = part.split(";", 2);
if (semicolonParts.length > 1) {
results.add(semicolonParts[1]);
} else {
throw new RuntimeException("描述格式错误,请修改描述格式");
}
}
String bag = results.getFirst();
String function = results.get(1);*/
String file = algorithmInfoMapper.getFileById(id);
try {
// 构建命令,将 param 作为参数传递给 Python 脚本
ProcessBuilder pb = new ProcessBuilder("python", file, description);
// 设置工作目录(如果 Python 脚本不在当前目录)
// pb.directory(new File("/path/to/script"));
// 启动进程
Process process = pb.start();
// 获取进程输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// 获取错误输出
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorLine;
while ((errorLine = errorReader.readLine()) != null) {
System.err.println("Error: " + errorLine);
}
// 等待进程完成
int exitCode = process.waitFor();
System.out.println("Exit Code: " + exitCode);
} catch (Exception e) {
e.printStackTrace();
}
}
} }