master
zhang chengzhi 2024-08-27 11:49:55 +08:00
parent f18981b34c
commit 19fec6c66b
13 changed files with 319 additions and 170 deletions

View File

@ -0,0 +1,47 @@
package com.muyu.rule.common.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author
* @Packagecom.muyu.rule.common.domain
* @Projectcloud-etl-rule
* @nameData
* @Date2024/8/27 9:18
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DataDescribe {
/**
*
*/
public String key;
/**
*
*/
public String type;
/**
*
*/
public String label;
/**
*
*/
public Object value;
}

View File

@ -0,0 +1,25 @@
package com.muyu.rule.server;
import com.muyu.rule.common.domain.DataDescribe;
/**
* @Author
* @Packagecom.muyu.rule.server
* @Projectcloud-etl-rule
* @nameDataTest
* @Date2024/8/27 9:43
*/
public class DataTest {
public static void main(String[] args) {
DataDescribe describe = new DataDescribe();
describe.setKey("name");
describe.setType("String");
describe.setLabel("姓名");
describe.setValue("张三");
}
}

View File

@ -1,5 +1,7 @@
package com.muyu.rule.server;
import java.util.concurrent.atomic.AtomicReference;
/**
* @Author
* @Packagecom.muyu.rule.server
@ -9,20 +11,17 @@ package com.muyu.rule.server;
*/
public class EngineConfig {
public static Object executionMethodName;
public static final AtomicReference<String> executionMethodName = new AtomicReference<>("execute");
/**
*
*/
private String pack ="com.muyu.rule.server.";
private String pack = "com.muyu.rule.server.engine.";
// E:\\临时\\2024年8月23日\\
/**
*
*/
private String location ="E:\\临时\\2024年8月23日\\";
private String location = "E:\\practical_training\\cloud-etl-rule\\cloud-rule-server\\src\\main\\java\\com\\muyu\\rule\\server\\engine\\";
public String getPack() {
return pack;
@ -39,8 +38,4 @@ public class EngineConfig {
public void setLocation(String location) {
this.location = location;
}
}

View File

@ -26,16 +26,16 @@ public class EngineTest {
SourceCodeComplier.javaCompilerPath(engineConfig.getLocation());
//对class文件进行自定义类加载规则引擎
Map<String ,Class<?>> stringClassMap = JavaBinaryClassLoader.loadClassesByLocation(engineConfig.getPack(),engineConfig.getLocation());
//
// stringClassMap.forEach((key,value)->{
// EngineContainer.loadEngineInstance(key,value);
// });
//
// Map<String, Object> params = new HashMap<>();
//params.put("idcard","142021 200212215977");
// Object engineObject = EngineExecution.engineExe("Engine_2024_8_23_2347", params);
//
// System.out.println(engineObject);
stringClassMap.forEach((key,value)->{
EngineContainer.loadEngineInstance(key,value);
});
Map<String, Object> params = new HashMap<>();
params.put("idcard","142021200212215977");
Object engineObject = EngineExecution.engineExe("Engine_2024_8_23_2347", params);
System.out.println("====>"+engineObject);

View File

@ -0,0 +1,57 @@
package com.muyu.rule.server.engine;
import com.muyu.rule.server.annotation.EngineParam;
import com.muyu.rule.server.pool.container.EngineContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
public class Engine_2024_8_23_2347 {
private static final Logger log = LoggerFactory.getLogger(Engine_2024_8_23_2347.class);
public String execute( @EngineParam(name = "idcard") String idcard) {
String msg = "";
System.out.println();
try {
// 提取年份
String yearStr = idcard.substring(6, 10);
Integer year = Integer.parseInt(yearStr);
// 获取当前年份
LocalDate currentDate = LocalDate.now();
Integer currentYear = currentDate.getYear();
// 计算年龄
Period age = Period.between(LocalDate.of(year, 1, 1), currentDate);
msg = "这个身份证的年龄是: " + age.getYears();
// 判断性别
String genderStr = idcard.substring(16, 17);
Integer gender = Integer.parseInt(genderStr);
if (gender % 2 == 0) {
msg += ", 这个身份证是女生";
} else {
msg += ", 这个身份证是男生";
}
// 获取规则引擎数量
Integer sumEngine = EngineContainer.getSumEngine();
log.info("项目中规则引擎数量为: {}", sumEngine);
// 可选:获取 User 对象并打印信息
// User user = Springutils.getBean(User.class);
// log.info("{} --- {}", user.getName(), user.getAge());
return msg;
} catch (Exception e) {
log.error("处理身份证号码时发生错误: {}", e.getMessage(), e);
return "处理身份证号码时发生错误: " + e.getMessage();
}
}
}

View File

@ -0,0 +1,35 @@
package com.muyu.rule.server.engine.custom;
import com.muyu.rule.server.exception.ActionDiscard;
/**
* @Author
* @Packagecom.muyu.rule.server.engine
* @Projectcloud-etl-rule
* @nameDataModelEngine
* @Date2024/8/26 22:00
*/
public class DataModelEngine {
public void execution () {
Object value = getValue();
if (value == null || "".equals(value) || "null".equals(value)) {
throw new ActionDiscard();
}
}
public Object getValue(){
Class<?> field;
return null;
}
}

View File

@ -0,0 +1,16 @@
package com.muyu.rule.server.engine.custom.child;
import com.muyu.rule.server.engine.custom.DataModelEngine;
/**
* @Author
* @Packagecom.muyu.rule.server.engine
* @Projectcloud-etl-rule
* @nameEngine_not_null
* @Date2024/8/26 21:57
*/
public class Engine_not_null extends DataModelEngine {
}

View File

@ -0,0 +1,11 @@
package com.muyu.rule.server.exception;
/**
* @Author
* @Packagecom.muyu.rule.server.exception
* @Projectcloud-etl-rule
* @nameActionDiscard
* @Date2024/8/26 22:08
*/
public class ActionDiscard extends RuntimeException {
}

View File

@ -0,0 +1,14 @@
package com.muyu.rule.server.exception;
/**
* @Author
* @Packagecom.muyu.rule.server.pool
* @Projectcloud-etl-rule
* @nameRuleEngineException
* @Date2024/8/22 23:13
*/
public class RuleEngineException extends RuntimeException {
public RuleEngineException(String message) {
super(message);
}
}

View File

@ -22,39 +22,28 @@ import java.util.Map;
*/
public class EngineExecution {
private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class);
public static Object engineExe(String engineKey, Map<String,Object> params){
Object instance = EngineContainer.getInstanceMap(engineKey);
List<String > methodEngineParams = EngineContainer.getMethodEngineParamMap(engineKey);
Method method = EngineContainer.getMethodMap(engineKey);
try{
int methodEngineParamsLength = methodEngineParams.size();
Object[] objects = new Object[methodEngineParamsLength];
for (int i = 0; i < methodEngineParamsLength; i++) {
objects[i] = params.get(methodEngineParams.get(i));
}
log.info("规则引擎 [{}] 调用参数 [{}]",engineKey, Arrays.toString(objects));
Object invoke = method.invoke(instance, objects);
log.info("规则引擎 [{}] 调用结果,[{}]",engineKey,invoke);
return invoke;
}catch (IllegalAccessException e){
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private static final Logger log = LoggerFactory.getLogger(EngineExecution.class);
public static Object engineExe(String engineKey, Map<String, Object> params) {
Object instance = EngineContainer.getInstanceMap(engineKey);
List<String> methodEngineParams = EngineContainer.getMethodEngineParamMap(engineKey);
Method method = EngineContainer.getMethodMap(engineKey);
try {
int methodEngineParamsLength = methodEngineParams.size();
Object[] objects = new Object[methodEngineParamsLength];
for (int i = 0; i < methodEngineParamsLength; i++) {
objects[i] = params.get(methodEngineParams.get(i));
}
log.info("规则引擎 [{}] 调用参数 [{}]", engineKey, Arrays.toString(objects));
Object invoke = method.invoke(instance, objects);
log.info("规则引擎 [{}] 调用结果,[{}]", engineKey, invoke);
return invoke;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -44,7 +44,6 @@ private static Class<?> loadClassByNameAndLocation(String name,String pack,File
//将class文件的数据读入到byte数组中 转成二进制文件
byte[] datas = loader.loadClassData(name,location);
pack= loader.convert(pack);
System.out.println(pack+"======"+name);
//通过byte数组加载Class对象
String cc = pack+name;

View File

@ -1,30 +0,0 @@
package com.muyu.rule.server.pool;
/**
* @Author
* @Packagecom.muyu.rule.server.pool
* @Projectcloud-etl-rule
* @nameRuleEngineException
* @Date2024/8/22 23:13
*/
public class RuleEngineException extends RuntimeException{
public RuleEngineException() {
super();
}
public RuleEngineException(String message) {
super(message);
}
public RuleEngineException(String message, Throwable cause) {
super(message, cause);
}
public RuleEngineException(Throwable cause) {
super(cause);
}
protected RuleEngineException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -2,14 +2,14 @@ package com.muyu.rule.server.pool.container;
import com.muyu.rule.server.EngineConfig;
import com.muyu.rule.server.annotation.EngineParam;
import com.muyu.rule.server.load.JavaBinaryClassLoader;
import com.muyu.rule.server.pool.RuleEngineException;
import com.muyu.rule.server.exception.RuleEngineException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -23,102 +23,93 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class EngineContainer {
private static final Logger log = LoggerFactory.getLogger(EngineContainer.class);
private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class);
private static final Map<String, Object> methodMap = new ConcurrentHashMap<>(16);
private static final Map<String, Object> classMap = new ConcurrentHashMap<>(16);
private static final Map<String, Object> instanceMap = new ConcurrentHashMap<>(16);
private static Map<String ,Object> methodMap = new ConcurrentHashMap<>(16);
public static void loadEngineInstance(String engineKey, Class<?> clazz) {
Method method = findMethodByClass(EngineConfig.executionMethodName.get(), clazz);
private static Map<String ,Object> classMap = new ConcurrentHashMap<>(16);
if (method == null) {
log.info("初始化规则引擎 -> {} 失败 缺少 {} 方法", engineKey, EngineConfig.executionMethodName.get());
return;
}
private static Map<String ,Object> instanceMap = new ConcurrentHashMap<>(16);
try {
instanceMap.put(engineKey, clazz.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
classMap.put(engineKey, clazz);
methodMap.put(engineKey, method);
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
ArrayList<String> engineParams = new ArrayList<>();
for (int i = 0; i < parameterTypes.length; i++) {
Annotation[] paramAnn = parameterAnnotations[i];
if (paramAnn.length == 0) {
throw new RuleEngineException("请给所有参数填写注释!");
}
if (paramAnn.length > 1) {
throw new RuleEngineException("参数仅支持一个注释!");
}
EngineParam engineParam = null;
if (paramAnn[0] instanceof EngineParam) {
engineParam = (EngineParam) paramAnn[0];
}
public static void loadEngineInstance(String engineKey,Class<?> clazz){
Method method = findMethodByClass(EngineConfig.executionMethodName,clazz);
if (method ==null){
log.info("初始化规则引擎 -> {} 失败 缺少 {} 方法",engineKey,EngineConfig.executionMethodName);
return;
}
try {
instanceMap.put(engineKey,clazz.newInstance());
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
classMap.put(engineKey,clazz);
methodMap.put(engineKey,method);
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
//获取参数
ArrayList<String> engineParams = new ArrayList<>();
int parameterTypeLength = parameterTypes.length;
for (int i = 0; i < parameterTypeLength; i++) {
Annotation[] paramAnn = parameterAnnotations[i];
if (paramAnn.length == 0){
throw new RuleEngineException("请给所有参数填写注释!");
}
if (paramAnn.length>1){
throw new RuleEngineException("参数仅支持一个注释!");
}
EngineParam engineParam =null;
if (paramAnn[0] instanceof EngineParam){
engineParam = (EngineParam) paramAnn[0];
}
if (engineParam ==null){
throw new RuleEngineException("请使用EngineParam注解");
} else if (engineParam.name() == null) {
throw new RuleEngineException("engineParam -- name 不可为null");
}
engineParams.add(engineParam.name());
if (engineParam == null) {
throw new RuleEngineException("请使用EngineParam注解");
} else if (engineParam.name() == null || engineParam.name().isEmpty()) {
throw new RuleEngineException("engineParam -- name 不可为null");
}
engineParams.add(engineParam.name());
}
}
}
private static Method findMethodByClass(Object executionMethodName, Class<?> clazz) {
return null;
}
private static Method findMethodByClass(String methodName, Class<?> clazz) {
Method[] methods = clazz.getDeclaredMethods();
return Arrays.stream(methods)
.filter(method -> method.getName().equals(methodName))
.findFirst()
.orElse(null);
}
public static List<String> getMethodEngineParamMap(String engineKey) {
return null;
}
public static Object getInstanceMap(String engineKey) {
return null;
}
public static Method getMethodMap(String engineKey) {
return null;
}
public static int getSumEngine() {
return 1;
Method method = (Method) methodMap.get(engineKey);
if (method == null) {
return new ArrayList<>();
}
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
List<String> params = new ArrayList<>();
for (int i = 0; i < parameterTypes.length; i++) {
Annotation[] paramAnn = parameterAnnotations[i];
if (paramAnn.length == 0) {
continue;
}
EngineParam engineParam = (EngineParam) paramAnn[0];
if (engineParam != null && !engineParam.name().isEmpty()) {
params.add(engineParam.name());
}
}
return params;
}
public static Object getInstanceMap(String engineKey) {
return instanceMap.get(engineKey);
}
public static Method getMethodMap(String engineKey) {
return (Method) methodMap.get(engineKey);
}
public static int getSumEngine() {
return classMap.size();
}
}