feat:规则引擎新增记录动作
parent
38d52196d2
commit
8e0100d909
|
@ -9,6 +9,7 @@ import com.ruoyi.ruleEngine.client.context.DataModelContextHolder;
|
|||
import com.ruoyi.ruleEngine.client.context.DataSetContextHolder;
|
||||
import com.ruoyi.ruleEngine.client.context.RecordContextHolder;
|
||||
import com.ruoyi.ruleEngine.client.dynamicLoad.DynamicLoader;
|
||||
import com.ruoyi.ruleEngine.client.engine.action.ActionRecords;
|
||||
import com.ruoyi.ruleEngine.client.engine.action.ActionRemove;
|
||||
import com.ruoyi.ruleEngine.client.engine.action.ActionReplace;
|
||||
import com.ruoyi.ruleEngine.client.model.DataModel;
|
||||
|
@ -26,6 +27,7 @@ import java.lang.reflect.Method;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 数据抽取Service业务层处理
|
||||
|
@ -71,21 +73,13 @@ public class EngineOperationServiceImpl implements EngineOperationService {
|
|||
}break;
|
||||
default:throw new ServiceException("此作用域暂无法测试",Result.SUCCESS);
|
||||
}
|
||||
StringBuffer actionRecords=new StringBuffer("数据发生动作[");
|
||||
StringBuffer actionRecords=null;
|
||||
// 获取版本对应class文件的字节数组
|
||||
Map<String, byte[]> bytecode = ruleEngineVersionFactory.get(testDataReq.getVersionId());
|
||||
try {
|
||||
// 加载class文件到虚拟机中,然后通过反射执行
|
||||
@SuppressWarnings("resource")
|
||||
DynamicLoader.MemoryClassLoader classLoader = new DynamicLoader.MemoryClassLoader(bytecode);
|
||||
Class<?> clazz = classLoader.loadClass(testDataReq.getVersionClass());
|
||||
// 调用execution方法
|
||||
Method mainMethod = clazz.getDeclaredMethod("execution");
|
||||
mainMethod.invoke(clazz.newInstance());
|
||||
}catch (InvocationTargetException e){
|
||||
// 获取调用的映射方法内的异常
|
||||
this.actionHandler(e.getTargetException(),actionRecords ,testDataReq.getVersionId());
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | NullPointerException e) {
|
||||
//执行引擎
|
||||
actionRecords=this.engineExecute(bytecode,testDataReq.getVersionClass(), testDataReq.getVersionId());
|
||||
}catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | NullPointerException e) {
|
||||
log.info("测试失败,{}",e.getMessage());
|
||||
//修改测试状态
|
||||
remoteRuleEngineService.edit(testDataReq.getVersionId(),"0");
|
||||
|
@ -95,8 +89,8 @@ public class EngineOperationServiceImpl implements EngineOperationService {
|
|||
remoteRuleEngineService.edit(testDataReq.getVersionId(),"1");
|
||||
// 返回结果
|
||||
String res="测试成功";
|
||||
if(actionRecords.toString().length()!=7){
|
||||
res+=","+actionRecords+"]";
|
||||
if(actionRecords.toString().length()!=8){
|
||||
res+=","+actionRecords;
|
||||
}
|
||||
switch (testDataReq.getScope()) {
|
||||
case 2 -> {
|
||||
|
@ -122,6 +116,36 @@ public class EngineOperationServiceImpl implements EngineOperationService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 引擎执行
|
||||
* @param bytecode 存储class文件的map
|
||||
* @param versionClass 版本类名
|
||||
* @param versionId 版本编号
|
||||
* @return 动作记录
|
||||
* @throws NoSuchMethodException 没有找到方法
|
||||
* @throws ClassNotFoundException 没找到类
|
||||
* @throws InstantiationException 实例化错误
|
||||
* @throws IllegalAccessException 非法参数
|
||||
*/
|
||||
public StringBuffer engineExecute(Map<String, byte[]> bytecode,String versionClass,Long versionId) throws NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
//记录发生的动作
|
||||
StringBuffer actionRecords=new StringBuffer("数据发生动作[");
|
||||
try {
|
||||
// 加载class文件到虚拟机中,然后通过反射执行
|
||||
@SuppressWarnings("resource")
|
||||
DynamicLoader.MemoryClassLoader classLoader = new DynamicLoader.MemoryClassLoader(bytecode);
|
||||
Class<?> clazz = classLoader.loadClass(versionClass);
|
||||
// 调用execution方法
|
||||
Method mainMethod = clazz.getDeclaredMethod("execution");
|
||||
mainMethod.invoke(clazz.newInstance());
|
||||
}catch (InvocationTargetException e){
|
||||
Throwable targetException = e.getTargetException();
|
||||
// 获取调用的映射方法内的异常
|
||||
this.actionHandler(targetException,actionRecords ,versionId);
|
||||
}
|
||||
return actionRecords.append("]");
|
||||
}
|
||||
|
||||
/**
|
||||
* 动作处理
|
||||
* @param e 异常
|
||||
|
@ -132,33 +156,52 @@ public class EngineOperationServiceImpl implements EngineOperationService {
|
|||
String records = actionRecords.toString();
|
||||
//判断动作
|
||||
if(e instanceof ActionReplace actionReplace){
|
||||
String name = actionReplace.getName();
|
||||
String newValue = actionReplace.getNewValue();
|
||||
log.info("匹配到替换动作,新值是:{}",newValue);
|
||||
//处理动作
|
||||
actionReplace.getDataModel().setValue(newValue);
|
||||
actionReplace.replace();
|
||||
//记录发生的动作
|
||||
if(!records.endsWith("[")){
|
||||
if(!records.contains("替换")){
|
||||
actionRecords.append("/替换");
|
||||
if(!records.contains(name)){
|
||||
actionRecords.append("/").append(name);
|
||||
}
|
||||
}else {
|
||||
actionRecords.append("替换");
|
||||
actionRecords.append(name);
|
||||
}
|
||||
}else if (e instanceof ActionRemove actionRemove){
|
||||
String name = actionRemove.getName();
|
||||
log.info("匹配到移除动作");
|
||||
actionRemove.getDataModel().setValue(null);
|
||||
if(!actionRecords.toString().endsWith("[")){
|
||||
if(!records.contains("移除")){
|
||||
actionRecords.append("/移除");
|
||||
actionRemove.remove();
|
||||
if(!records.endsWith("[")){
|
||||
if(!records.contains(name)){
|
||||
actionRecords.append("/").append(name);
|
||||
}
|
||||
}else {
|
||||
actionRecords.append("移除");
|
||||
actionRecords.append(name);
|
||||
}
|
||||
}else if (e instanceof ActionRecords actionRecord){
|
||||
String name = actionRecord.getName();
|
||||
log.info("匹配到记录动作");
|
||||
actionRecord.records();
|
||||
if(!records.endsWith("[")){
|
||||
if(!records.contains(name)){
|
||||
actionRecords.append("/").append(name);
|
||||
}
|
||||
}else {
|
||||
actionRecords.append(name);
|
||||
}
|
||||
}else {
|
||||
log.info("测试失败,{}",e.getMessage());
|
||||
//修改测试状态
|
||||
remoteRuleEngineService.edit(versionId,"0");
|
||||
throw new ServiceException("测试失败,"+e.getMessage(),Result.SUCCESS);
|
||||
String res = null;
|
||||
if(Objects.isNull(versionId)){
|
||||
res="执行";
|
||||
}else {
|
||||
res="测试";
|
||||
//修改测试状态
|
||||
remoteRuleEngineService.edit(versionId,"0");
|
||||
}
|
||||
log.info(res+"失败,{}",e.getMessage());
|
||||
throw new ServiceException(res+"失败,"+e.getMessage(),Result.SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,34 @@
|
|||
package com.ruoyi.ruleEngine.client.engine.action;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
/**
|
||||
* 记录动作
|
||||
* @ClassName ActionRecords
|
||||
* @Author 森静若林
|
||||
* @Date 2024/5/13 22:31
|
||||
*/
|
||||
public class ActionRecords {
|
||||
@Getter
|
||||
@Setter
|
||||
@Log4j2
|
||||
public class ActionRecords extends RuntimeException{
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String name="记录";
|
||||
|
||||
/**
|
||||
* 数据模型
|
||||
*/
|
||||
private Object object;
|
||||
|
||||
public ActionRecords(Object object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public void records(){
|
||||
log.info("记录的数据是:{}",this.object);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,10 @@ import lombok.Setter;
|
|||
@Setter
|
||||
@Getter
|
||||
public class ActionRemove extends RuntimeException{
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String name="移除";
|
||||
/**
|
||||
* 数据模型
|
||||
*/
|
||||
|
|
|
@ -12,6 +12,10 @@ import lombok.*;
|
|||
@Getter
|
||||
@Setter
|
||||
public class ActionReplace extends RuntimeException{
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String name="替换";
|
||||
/**
|
||||
* 新值
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue