master
chentaisen 2024-09-05 11:26:57 +08:00
parent ccdda9a532
commit f77c58500f
13 changed files with 420 additions and 248 deletions

View File

@ -1,17 +1,17 @@
//package rule.data.engine.basic;
//
///**
// * @ClassName BasicEngin
// * @Description 规则引擎基准
// * @Author Chen
// * @Date 2024/8/29 14:28
// */
//public interface BasicEngine<V> {
// public void set(V dataValue);
//
// public V get();
//
// public void remove();
//
// public void execution();
//}
package rule.data.engine.basic;
/**
* @ClassName BasicEngin
* @Description
* @Author Chen
* @Date 2024/8/29 14:28
*/
public interface BasicEngine<V> {
public void set(V dataValue);
public V get();
public void remove();
public void execution();
}

View File

@ -0,0 +1,31 @@
package rule.data.engine.basic.abstracts;
import rule.data.engine.basic.BasicEngine;
import rule.data.engine.basic.handler.DataEngineValueHandler;
import rule.domain.DataValue;
/**
* @ClassName DataEngineRecordActuator
* @Description
* @Author Chen
* @Date 2024/9/3 10:18
*/
public abstract class DataEngineRecordActuator implements BasicEngine<DataValue> {
@Override
public void set(DataValue dataValue) {
DataEngineValueHandler.set(dataValue);
}
@Override
public DataValue get() {
return DataEngineValueHandler.get();
}
@Override
public void execution() {
this.run();
this.remove();
}
public abstract void run();
}

View File

@ -1,22 +1,22 @@
//package rule.data.engine.basic.abstracts;
//
//
//import rule.data.engine.basic.BasicEngine;
//import rule.data.engine.basic.handler.DataEngineHandler;
//import rule.domain.DataValue;
//
///**
// * @ClassName DataEngineValueActuator
// * @Description 数据值处理对象
// * @Author Chen
// * @Date 2024/8/29 16:10
// */
//public abstract class DataEngineRowActuator implements BasicEngine<DataValue[]> {
// public void set(DataValue[] dataValue) {
// DataEngineHandler.set(dataValue);
// }
//
// public DataValue[] get() {
// return DataEngineHandler.get();
// }
//}
package rule.data.engine.basic.abstracts;
import rule.data.engine.basic.BasicEngine;
import rule.data.engine.basic.handler.DataEngineHandler;
import rule.domain.DataValue;
/**
* @ClassName DataEngineValueActuator
* @Description
* @Author Chen
* @Date 2024/8/29 16:10
*/
public abstract class DataEngineRowActuator implements BasicEngine<DataValue[]> {
public void set(DataValue[] dataValue) {
DataEngineHandler.set(dataValue);
}
public DataValue[] get() {
return DataEngineHandler.get();
}
}

View File

@ -1,33 +1,32 @@
//package rule.data.engine.basic.abstracts;
//
//
//import rule.data.engine.basic.BasicEngine;
//import rule.data.engine.basic.handler.DataEngineValueHandler;
//import rule.domain.DataValue;
//
///**
// * @ClassName DataEngineValueActuator
// * @Description 数据值处理对象
// * @Author Chen
// * @Date 2024/8/29 16:10
// */
//public abstract class DataEngineValueActuator implements BasicEngine<DataValue> {
// @Override
// public void set(DataValue dataValue) {
// DataEngineValueHandler.set(dataValue);
// }
//
// @Override
// public DataValue get() {
// return DataEngineValueHandler.get();
// }
//
// @Override
// public void execution() {
// this.run();
// this.remove();
// }
//
//
// public abstract void run();
//}
package rule.data.engine.basic.abstracts;
import rule.data.engine.basic.BasicEngine;
import rule.data.engine.basic.handler.DataEngineValueHandler;
import rule.domain.DataValue;
/**
* @ClassName DataEngineValueActuator
* @Description
* @Author Chen
* @Date 2024/8/29 16:10
*/
public abstract class DataEngineValueActuator implements BasicEngine<DataValue> {
@Override
public void set(DataValue dataValue) {
DataEngineValueHandler.set(dataValue);
}
@Override
public DataValue get() {
return DataEngineValueHandler.get();
}
@Override
public void execution() {
this.run();
this.remove();
}
public abstract void run();
}

View File

@ -1,23 +1,23 @@
//package rule.data.engine.basic.handler;
//
///**
// * @ClassName DataEngineHandler
// * @Description 规则引擎作用域
// * @Author Chen
// * @Date 2024/8/29 14:35
// */
//public class DataEngineHandler {
// private static final ThreadLocal<Object> dataEngineHandler = new ThreadLocal<>();
//
// public static void set(final Object handler) {
// dataEngineHandler.set(handler);
// }
//
// public static <T> T get() {
// return (T) dataEngineHandler.get();
// }
//
// public static void remove() {
// dataEngineHandler.remove();
// }
//}
package rule.data.engine.basic.handler;
/**
* @ClassName DataEngineHandler
* @Description
* @Author Chen
* @Date 2024/8/29 14:35
*/
public class DataEngineHandler {
private static final ThreadLocal<Object> dataEngineHandler = new ThreadLocal<>();
public static void set(final Object handler) {
dataEngineHandler.set(handler);
}
public static <T> T get() {
return (T) dataEngineHandler.get();
}
public static void remove() {
dataEngineHandler.remove();
}
}

View File

@ -1,20 +1,20 @@
//package rule.data.engine.basic.handler;
//
//import rule.domain.DataValue;
//
///**
// * @ClassName DataEngineValueHandler
// * @Description 数据值作用域 数据记录
// * @Author Chen
// * @Date 2024/8/29 14:41
// */
//public class DataEngineRowHandler {
// public static void set(DataValue[] dataValue) {
// DataEngineHandler.set(dataValue);
// }
//
// public DataValue[] get() {
// return DataEngineHandler.get();
// }
//
//}
package rule.data.engine.basic.handler;
import rule.domain.DataValue;
/**
* @ClassName DataEngineValueHandler
* @Description
* @Author Chen
* @Date 2024/8/29 14:41
*/
public class DataEngineRowHandler {
public static void set(DataValue[] dataValue) {
DataEngineHandler.set(dataValue);
}
public DataValue[] get() {
return DataEngineHandler.get();
}
}

View File

@ -1,33 +1,33 @@
//package rule.data.engine.basic.handler;
//
//import com.muyu.common.core.text.Convert;
//import rule.domain.DataValue;
//
///**
// * @ClassName DataEngineValueHandler
// * @Description 数据值作用域
// * @Author Chen
// * @Date 2024/8/29 14:41
// */
//public class DataEngineValueHandler {
// public static void set(DataValue dataValue) {
// DataEngineHandler.set(dataValue);
// }
//
// public static DataValue get() {
// return DataEngineHandler.get();
// }
//
// public static void remove() {
// DataEngineHandler.remove();
// }
// public static Object getValue() {
// return get().getValue();
// }
//
// public static Integer getIntegerValue() {
// return Convert.toInt(getValue(), null);
// }
//
//
//}
package rule.data.engine.basic.handler;
import com.muyu.common.core.text.Convert;
import rule.domain.DataValue;
/**
* @ClassName DataEngineValueHandler
* @Description
* @Author Chen
* @Date 2024/8/29 14:41
*/
public class DataEngineValueHandler {
public static void set(DataValue dataValue) {
DataEngineHandler.set(dataValue);
}
public static DataValue get() {
return DataEngineHandler.get();
}
public static void remove() {
DataEngineHandler.remove();
}
public static Object getValue() {
return get().getValue();
}
public static Integer getIntegerValue() {
return Convert.toInt(getValue(), null);
}
}

View File

@ -1,27 +1,27 @@
//package rule.data.engine.service;
//
//import rule.data.engine.basic.abstracts.DataEngineValueActuator;
//import rule.domain.DataValue;
//
///**
// * @ClassName ENGINE_VALUE_JDIE3456711
// * @Description 判空
// * @Author Chen
// * @Date 2024/8/29 16:26
// */
//public class ENGINE_VALUE_JDIE3456711_V1 extends DataEngineValueActuator {
//
//
// @Override
// public void run() {
// DataValue dataValue = get();
// if (dataValue.getValue()==null){
// System.out.println("数据为空,需要丢弃");
// }
// }
//
// @Override
// public void remove() {
//
// }
//}
package rule.data.engine.service;
import rule.data.engine.basic.abstracts.DataEngineValueActuator;
import rule.domain.DataValue;
/**
* @ClassName ENGINE_VALUE_JDIE3456711
* @Description
* @Author Chen
* @Date 2024/8/29 16:26
*/
public class ENGINE_VALUE_JDIE3456711_V1 extends DataEngineValueActuator {
@Override
public void run() {
DataValue dataValue = get();
if (dataValue.getValue()==null){
System.out.println("数据为空,需要丢弃");
}
}
@Override
public void remove() {
}
}

View File

@ -1,27 +1,25 @@
//package rule.domain;
//
//import lombok.AllArgsConstructor;
//import lombok.Builder;
//import lombok.Data;
//import lombok.NoArgsConstructor;
//
//import java.net.SocketOption;
//
///**
// * @ClassName DataValue
// * @Description 描述
// * @Author Chen
// * @Date 2024/8/29 15:05
// */
//@Data
//@Builder
//@AllArgsConstructor
//@NoArgsConstructor
//public class DataValue {
// private Object value;
// private String type;
// private String key;
// private String label;
//
//
//}
package rule.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.net.SocketOption;
/**
* @ClassName DataValue
* @Description
* @Author Chen
* @Date 2024/8/29 15:05
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DataValue {
private Object value;
private String type;
private String key;
private String label;
}

View File

@ -1,25 +1,25 @@
//package rule.domain;
//
//import lombok.AllArgsConstructor;
//import lombok.Data;
//import lombok.NoArgsConstructor;
//
///**
// * @ClassName RuleAll
// * @Description 描述
// * @Author Chen
// * @Date 2024/8/27 09:04
// */
//@Data
//@AllArgsConstructor
//@NoArgsConstructor
//public class RuleDescribe {
// //字段名
// private String key;
// private Object value;
// //元类型 目标类型 逻辑类型
// private Object type;
// //描述
// private String label;
//}
//
package rule.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName RuleAll
* @Description
* @Author Chen
* @Date 2024/8/27 09:04
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RuleDescribe {
//字段名
private String key;
private Object value;
//元类型 目标类型 逻辑类型
private Object type;
//描述
private String label;
}

View File

@ -6,6 +6,7 @@ import com.muyu.common.core.exception.ServiceException;
import com.muyu.rule.compile.SourceCodeCompiler;
import com.muyu.rule.service.RuleEditionService;
import com.muyu.rule.test.OSSFileDownload;
import com.muyu.rule.test.TestService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
@ -29,6 +30,8 @@ import java.util.List;
public class RuleEditionController {
@Autowired
private RuleEditionService ruleEditionService;
@Autowired
private TestService testService;
/**
*
@ -63,27 +66,27 @@ public class RuleEditionController {
@Operation(summary = "添加版本", description = "添加版本")
public Result insert(@RequestBody @Validated RuleEdition ruleEdition) {
//java OSS
UploadOss.uploadFiles(ruleEdition.getRuleEngine(), ruleEdition.getRuleKind());
OSSFileDownload.streamingDownload(ruleEdition.getRuleKind());
//对路径里的.java文件进行编译
SourceCodeCompiler.javaCompilerPath("/home");
//指定的输出目录
File outputDir = new File("/home");
//获取输出目录中文件
File[] classFiles = outputDir.listFiles();
if (classFiles != null) {
for (File classFile : classFiles) {
if (classFile.getName().endsWith(".class")) {
log.info("找到class文件 " + classFile.getName());
}
}
} else {
log.info("没有找到文件");
}
// //java OSS
// UploadOss.uploadFiles(ruleEdition.getRuleEngine(), ruleEdition.getRuleKind());
//
// OSSFileDownload.streamingDownload(ruleEdition.getRuleKind());
//
// //对路径里的.java文件进行编译
// SourceCodeCompiler.javaCompilerPath("/home");
// //指定的输出目录
// File outputDir = new File("/home");
// //获取输出目录中文件
// File[] classFiles = outputDir.listFiles();
// if (classFiles != null) {
// for (File classFile : classFiles) {
// if (classFile.getName().endsWith(".class")) {
// log.info("找到class文件 " + classFile.getName());
// }
// }
// } else {
// log.info("没有找到文件");
// }
testService.test();
LambdaQueryWrapper<RuleEdition> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(
RuleEdition::getRuleKind, ruleEdition.getRuleKind()
@ -178,6 +181,7 @@ public class RuleEditionController {
/**
*
*
* @param ruleEdition
* @return
*/

View File

@ -0,0 +1,23 @@
package com.muyu.rule.test;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
public class ExternalClassLoader extends URLClassLoader {
public ExternalClassLoader(URL[] urls) {
super(urls, Thread.currentThread().getContextClassLoader());
}
public Class<?> defineClassFromBytes(byte[] classBytes, String className) throws IOException {
return super.defineClass(className, classBytes, 0, classBytes.length);
}
public Class<?> loadClassFromPath(Path classFilePath, String className) throws IOException {
byte[] classData = Files.readAllBytes(classFilePath);
return defineClassFromBytes(classData, className);
}
}

View File

@ -0,0 +1,117 @@
package com.muyu.rule.test;
import org.springframework.stereotype.Service;
import rule.data.engine.basic.BasicEngine;
import rule.domain.DataValue;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class TestService {
static Map<String, BasicEngine<DataValue>> engineMap = new ConcurrentHashMap<>();
public static final String engineWorkSourcePath = "D:\\work\\cc";
// public static final String engineWorkClassPath = engineWorkSourcePath;
public static final String className = "rule.data.engine.service.ENGINE_VALUE_JDIE3456711_V1";
// package com.muyu.data.engine.service;
public static final String code =
"""
package com.muyu.data.engine.service;
import com.muyu.data.engine.basic.abstracts.DataEngineValueActuator;
import com.muyu.etl.core.domain.DataValue;
/**
* @Author: cc
* @date: 2024/9/29
* @Description:
* @Version: 1.0
*/
public class ENGINE_VALUE_JDIES979972_V2 extends DataEngineValueActuator {
@Override
public void run () {
DataValue dataValue = get();
if (dataValue.getValue() == null){
System.out.println("数据为空,需要丢弃");
}else{
System.out.println("数据非空:"+dataValue.getValue());
}
}
}
""";
public void test() {
try {
FileWriter writer;
writer = new FileWriter(engineWorkSourcePath + "\\ENGINE_VALUE_JDIE3456711_V1.java");
writer.write(code);
writer.flush();
writer.close();
// 获取java编译器
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
//
InputStream first = null; // 程序的输入 null 用 system.in
OutputStream second = null; // 程序的输出 null 用 system.out
OutputStream third = null; // 程序的错误输出 .,null 用 system.err
// 程序编译参数 注意 我们编译目录是我们的项目目录
String[] strings = {"-classpath", engineWorkSourcePath, "-verbose", "-d", engineWorkSourcePath, engineWorkSourcePath + "\\ENGINE_VALUE_JDIE3456711_V1.java"};
// 0 表示成功, 其他表示出现了错误
System.out.println(Arrays.toString(strings));
int i = javaCompiler.run(first, second, third, strings);
if (i == 0) {
System.out.println("成功");
} else {
System.out.println("错误");
}
// 假设这是你的外部类文件路径
String externalClassFilePath =
engineWorkSourcePath + "\\com\\muyu\\rule\\test\\ENGINE_VALUE_JDIE3456711_V1.class";
Path classFilePath = Paths.get(externalClassFilePath);
// 获取外部类所在的目录
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('\\'));
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
// 创建自定义类加载器
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
// 加载类
// 注意类名必须是完全限定名(包括包名)
Class<?> clazz = externalClassLoader.loadClassFromPath(classFilePath, className);
// Class<?> clazz = customClassLoader.defineClassFromBytes(externalClassBytes);
// 创建类的实例
Object instance = clazz.getDeclaredConstructor().newInstance();
engineMap.put("ENGINE_VALUE_JDIE3456711_V1", (BasicEngine<DataValue>) instance);
} catch (IllegalAccessException | IOException | InstantiationException |
NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
DataValue dataValue = DataValue.builder()
.type("String")
.label("姓名")
.key("name")
.value("张三")
.build();
BasicEngine<DataValue> engine = engineMap.get("ENGINE_VALUE_JDIE3456711_V1");
engine.set(dataValue);
engine.execution();
}
}