fix: 规则引擎完善
parent
b92a8cef22
commit
6b69b29567
|
@ -11,44 +11,8 @@ import java.net.URLClassLoader;
|
||||||
* @Author Xin.Yao
|
* @Author Xin.Yao
|
||||||
* @Date 2024/5/1 19:37
|
* @Date 2024/5/1 19:37
|
||||||
*/
|
*/
|
||||||
public class CustomClassLoader extends URLClassLoader {
|
public class CustomClassLoader extends ClassLoader {
|
||||||
public CustomClassLoader(ClassLoader parent, String classPath) throws MalformedURLException {
|
public Class<?> defineClassFromBytes(String name, byte[] data) {
|
||||||
super(new URL[]{new URL("file:" + classPath)},parent);
|
return defineClass(name, data, 0, data.length);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
|
||||||
try {
|
|
||||||
byte[] classBytes = loadClassData(name);
|
|
||||||
return defineClass(name, classBytes, 0, classBytes.length);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new ClassNotFoundException(name, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] loadClassData(String className) throws IOException {
|
|
||||||
String fileName = className.replace('.', File.separatorChar) + ".class";
|
|
||||||
File classFile = new File(getPathForResource(fileName));
|
|
||||||
if (!classFile.exists()) {
|
|
||||||
throw new FileNotFoundException("Class file not found: " + classFile.getAbsolutePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
try (InputStream in = new FileInputStream(classFile)) {
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
byte[] buffer = new byte[4096];
|
|
||||||
int n;
|
|
||||||
while ((n = in.read(buffer)) != -1) {
|
|
||||||
out.write(buffer, 0, n);
|
|
||||||
}
|
|
||||||
return out.toByteArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getPathForResource(String resource) {
|
|
||||||
URL url = super.getResource(resource);
|
|
||||||
if (url == null) {
|
|
||||||
throw new IllegalArgumentException("Resource not found: " + resource);
|
|
||||||
}
|
|
||||||
return url.getPath();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,5 +18,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
public class MuYuRuleEngineApplication {
|
public class MuYuRuleEngineApplication {
|
||||||
public static void main (String[] args) {
|
public static void main (String[] args) {
|
||||||
SpringApplication.run(MuYuRuleEngineApplication.class, args);
|
SpringApplication.run(MuYuRuleEngineApplication.class, args);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,38 +17,48 @@ public class EngineMaintenance extends BaseEntity {
|
||||||
/** $column.columnComment */
|
/** $column.columnComment */
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
|
|
||||||
/** 规则引擎名称 */
|
/** 规则引擎名称 */
|
||||||
@Excel(name = "规则引擎名称")
|
@Excel(name = "规则引擎名称")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
|
||||||
/** 规则引擎类型 */
|
/** 规则引擎类型 */
|
||||||
@Excel(name = "规则引擎类型")
|
@Excel(name = "规则引擎类型")
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
|
|
||||||
/** 规则引擎激活状态 */
|
/** 规则引擎激活状态 */
|
||||||
@Excel(name = "规则引擎激活状态")
|
@Excel(name = "规则引擎激活状态")
|
||||||
private String isActivate;
|
private String isActivate;
|
||||||
|
|
||||||
|
|
||||||
/** 规则引擎状态 */
|
/** 规则引擎状态 */
|
||||||
@Excel(name = "规则引擎状态")
|
@Excel(name = "规则引擎状态")
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
|
|
||||||
/** 规则引擎描述 */
|
/** 规则引擎描述 */
|
||||||
@Excel(name = "规则引擎描述")
|
@Excel(name = "规则引擎描述")
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** 规则引擎编码 */
|
/** 规则引擎编码 */
|
||||||
@Excel(name = "规则引擎编码")
|
@Excel(name = "规则引擎编码")
|
||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
|
|
||||||
/** 规则引擎级别 */
|
/** 规则引擎级别 */
|
||||||
@Excel(name = "规则引擎级别")
|
@Excel(name = "规则引擎级别")
|
||||||
private String level;
|
private String level;
|
||||||
|
|
||||||
|
|
||||||
/** 编辑代码文本 */
|
/** 编辑代码文本 */
|
||||||
@Excel(name = "编辑代码文本")
|
@Excel(name = "编辑代码文本")
|
||||||
private String codeText;
|
private String codeText;
|
||||||
|
|
||||||
|
|
||||||
public String getCodeText() {
|
public String getCodeText() {
|
||||||
return codeText;
|
return codeText;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package com.muyu.rule.engine.service.impl;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -120,13 +122,14 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
||||||
//创建源文件
|
//创建源文件
|
||||||
String className = "Rule"+Character.toUpperCase(engineMaintenance.getCode().charAt(0)) + engineMaintenance.getCode().substring(1)+"Class";
|
String className = "Rule"+Character.toUpperCase(engineMaintenance.getCode().charAt(0)) + engineMaintenance.getCode().substring(1)+"Class";
|
||||||
// 源文件路径和名称
|
// 源文件路径和名称
|
||||||
String javaPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\src\\main\\java\\com\\muyu\\rule\\engine\\domain\\";
|
String javaPath = "D:\\idea\\ruoyi-could-server-muyu\\muyu-modules\\muyu-rule-engine\\src\\main\\java\\com\\muyu\\rule\\engine\\domain\\";
|
||||||
String classPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\target\\classes\\";
|
String classPath = "D:\\idea\\ruoyi-could-server-muyu\\muyu-modules\\muyu-rule-engine\\target\\classes\\";
|
||||||
String filename = javaPath+className+".java";
|
String filename = javaPath+className+".java";
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
// 确保源文件所在的目录存在
|
// 确保源文件所在的目录存在
|
||||||
File fileParent = file.getParentFile();
|
File fileParent = file.getParentFile();
|
||||||
|
|
||||||
|
|
||||||
if (!fileParent.exists()) {
|
if (!fileParent.exists()) {
|
||||||
fileParent.mkdir();
|
fileParent.mkdir();
|
||||||
}
|
}
|
||||||
|
@ -167,13 +170,13 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result testMethod(String code) {
|
public Result testMethod(String code) {
|
||||||
String className = "Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class";
|
String className = "com.muyu.rule.engine.domain.Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class";
|
||||||
String classPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\target\\classes\\";
|
String classPath = "D:\\idea\\ruoyi-could-server-muyu\\muyu-modules\\muyu-rule-engine\\target\\classes"+className.replace(".", "\\")+".class";
|
||||||
try {
|
try {
|
||||||
// 使用CustomClassLoader加载class到内存
|
// 读取类文件
|
||||||
CustomClassLoader customClassLoader = new CustomClassLoader(ClassLoader.getSystemClassLoader(), classPath);
|
byte[] classData = Files.readAllBytes(Paths.get(classPath));
|
||||||
// 加载类
|
CustomClassLoader customClassLoader = new CustomClassLoader();
|
||||||
Class<?> aClass = customClassLoader.loadClass("com.muyu.rule.engine.domain."+className);
|
Class<?> aClass = customClassLoader.defineClassFromBytes(className,classData);
|
||||||
// 实例化类
|
// 实例化类
|
||||||
Object obj = aClass.newInstance();
|
Object obj = aClass.newInstance();
|
||||||
//获取类中所有方法
|
//获取类中所有方法
|
||||||
|
@ -189,4 +192,29 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
||||||
|
|
||||||
return Result.success("测试成功");
|
return Result.success("测试成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTypeCodeText(EngineMaintenance engineMaintenance) {
|
||||||
|
String className =Character.toUpperCase(engineMaintenance.getCode().charAt(0)) + engineMaintenance.getCode().substring(1);
|
||||||
|
switch (engineMaintenance.getType()) {
|
||||||
|
case "data-set":
|
||||||
|
className = className+"DataSetContext";
|
||||||
|
return "package com.muyu.rule.engine.domain;\n" +
|
||||||
|
"\n" +
|
||||||
|
"public class "+className+" {\n" +
|
||||||
|
"\n" +
|
||||||
|
" private final RecordContext recordContext;\n" +
|
||||||
|
"\n" +
|
||||||
|
" public DataSetContext (RecordContext recordContext) {\n" +
|
||||||
|
" this.recordContext = recordContext;\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}\n";
|
||||||
|
case "data-record":
|
||||||
|
return "记录";
|
||||||
|
default:
|
||||||
|
return "数据字段";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue