11111
parent
f18981b34c
commit
19fec6c66b
|
@ -0,0 +1,47 @@
|
|||
package com.muyu.rule.common.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.muyu.rule.common.domain
|
||||
* @Project:cloud-etl-rule
|
||||
* @name:Data
|
||||
* @Date:2024/8/27 9:18
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DataDescribe {
|
||||
|
||||
/**
|
||||
* 键
|
||||
*/
|
||||
public String key;
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
|
||||
public String type;
|
||||
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
|
||||
public String label;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
|
||||
public Object value;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.muyu.rule.server;
|
||||
|
||||
import com.muyu.rule.common.domain.DataDescribe;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.muyu.rule.server
|
||||
* @Project:cloud-etl-rule
|
||||
* @name:DataTest
|
||||
* @Date:2024/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("张三");
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package com.muyu.rule.server;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.muyu.rule.server.engine.custom;
|
||||
|
||||
import com.muyu.rule.server.exception.ActionDiscard;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.muyu.rule.server.engine
|
||||
* @Project:cloud-etl-rule
|
||||
* @name:DataModelEngine
|
||||
* @Date:2024/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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.muyu.rule.server.engine.custom.child;
|
||||
|
||||
import com.muyu.rule.server.engine.custom.DataModelEngine;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.muyu.rule.server.engine
|
||||
* @Project:cloud-etl-rule
|
||||
* @name:Engine_not_null
|
||||
* @Date:2024/8/26 21:57
|
||||
*/
|
||||
public class Engine_not_null extends DataModelEngine {
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.muyu.rule.server.exception;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.muyu.rule.server.exception
|
||||
* @Project:cloud-etl-rule
|
||||
* @name:ActionDiscard
|
||||
* @Date:2024/8/26 22:08
|
||||
*/
|
||||
public class ActionDiscard extends RuntimeException {
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.muyu.rule.server.exception;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.muyu.rule.server.pool
|
||||
* @Project:cloud-etl-rule
|
||||
* @name:RuleEngineException
|
||||
* @Date:2024/8/22 23:13
|
||||
*/
|
||||
public class RuleEngineException extends RuntimeException {
|
||||
public RuleEngineException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -22,11 +22,9 @@ import java.util.Map;
|
|||
*/
|
||||
public class EngineExecution {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class);
|
||||
|
||||
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);
|
||||
|
@ -47,14 +45,5 @@ log.info("规则引擎 [{}] 调用参数 [{}]",engineKey, Arrays.toString(object
|
|||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
package com.muyu.rule.server.pool;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
* @Package:com.muyu.rule.server.pool
|
||||
* @Project:cloud-etl-rule
|
||||
* @name:RuleEngineException
|
||||
* @Date:2024/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);
|
||||
}
|
||||
}
|
|
@ -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,44 +23,34 @@ 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 Map<String ,Object> methodMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
private static Map<String ,Object> classMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
private static Map<String ,Object> instanceMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
public static void loadEngineInstance(String engineKey, Class<?> clazz) {
|
||||
|
||||
|
||||
Method method = findMethodByClass(EngineConfig.executionMethodName,clazz);
|
||||
Method method = findMethodByClass(EngineConfig.executionMethodName.get(), clazz);
|
||||
|
||||
if (method == null) {
|
||||
log.info("初始化规则引擎 -> {} 失败 缺少 {} 方法",engineKey,EngineConfig.executionMethodName);
|
||||
log.info("初始化规则引擎 -> {} 失败 缺少 {} 方法", engineKey, EngineConfig.executionMethodName.get());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
instanceMap.put(engineKey, clazz.newInstance());
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
} 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<>();
|
||||
|
||||
int parameterTypeLength = parameterTypes.length;
|
||||
for (int i = 0; i < parameterTypeLength; i++) {
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
Annotation[] paramAnn = parameterAnnotations[i];
|
||||
if (paramAnn.length == 0) {
|
||||
throw new RuleEngineException("请给所有参数填写注释!");
|
||||
|
@ -75,50 +65,51 @@ public static void loadEngineInstance(String engineKey,Class<?> clazz){
|
|||
|
||||
if (engineParam == null) {
|
||||
throw new RuleEngineException("请使用EngineParam注解");
|
||||
} else if (engineParam.name() == null) {
|
||||
} 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;
|
||||
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 null;
|
||||
return instanceMap.get(engineKey);
|
||||
}
|
||||
|
||||
|
||||
public static Method getMethodMap(String engineKey) {
|
||||
|
||||
return null;
|
||||
|
||||
return (Method) methodMap.get(engineKey);
|
||||
}
|
||||
|
||||
public static int getSumEngine() {
|
||||
|
||||
return 1;
|
||||
return classMap.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue