feat:规则引擎新增记录动作

dev
gtl 2024-05-14 15:00:01 +08:00
parent 38d52196d2
commit 8e0100d909
4 changed files with 102 additions and 28 deletions

View File

@ -9,6 +9,7 @@ import com.ruoyi.ruleEngine.client.context.DataModelContextHolder;
import com.ruoyi.ruleEngine.client.context.DataSetContextHolder; import com.ruoyi.ruleEngine.client.context.DataSetContextHolder;
import com.ruoyi.ruleEngine.client.context.RecordContextHolder; import com.ruoyi.ruleEngine.client.context.RecordContextHolder;
import com.ruoyi.ruleEngine.client.dynamicLoad.DynamicLoader; 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.ActionRemove;
import com.ruoyi.ruleEngine.client.engine.action.ActionReplace; import com.ruoyi.ruleEngine.client.engine.action.ActionReplace;
import com.ruoyi.ruleEngine.client.model.DataModel; import com.ruoyi.ruleEngine.client.model.DataModel;
@ -26,6 +27,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* Service * Service
@ -71,21 +73,13 @@ public class EngineOperationServiceImpl implements EngineOperationService {
}break; }break;
default:throw new ServiceException("此作用域暂无法测试",Result.SUCCESS); default:throw new ServiceException("此作用域暂无法测试",Result.SUCCESS);
} }
StringBuffer actionRecords=new StringBuffer("数据发生动作["); StringBuffer actionRecords=null;
// 获取版本对应class文件的字节数组 // 获取版本对应class文件的字节数组
Map<String, byte[]> bytecode = ruleEngineVersionFactory.get(testDataReq.getVersionId()); Map<String, byte[]> bytecode = ruleEngineVersionFactory.get(testDataReq.getVersionId());
try { try {
// 加载class文件到虚拟机中然后通过反射执行 //执行引擎
@SuppressWarnings("resource") actionRecords=this.engineExecute(bytecode,testDataReq.getVersionClass(), testDataReq.getVersionId());
DynamicLoader.MemoryClassLoader classLoader = new DynamicLoader.MemoryClassLoader(bytecode); }catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | NullPointerException e) {
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) {
log.info("测试失败,{}",e.getMessage()); log.info("测试失败,{}",e.getMessage());
//修改测试状态 //修改测试状态
remoteRuleEngineService.edit(testDataReq.getVersionId(),"0"); remoteRuleEngineService.edit(testDataReq.getVersionId(),"0");
@ -95,8 +89,8 @@ public class EngineOperationServiceImpl implements EngineOperationService {
remoteRuleEngineService.edit(testDataReq.getVersionId(),"1"); remoteRuleEngineService.edit(testDataReq.getVersionId(),"1");
// 返回结果 // 返回结果
String res="测试成功"; String res="测试成功";
if(actionRecords.toString().length()!=7){ if(actionRecords.toString().length()!=8){
res+=","+actionRecords+"]"; res+=","+actionRecords;
} }
switch (testDataReq.getScope()) { switch (testDataReq.getScope()) {
case 2 -> { case 2 -> {
@ -122,6 +116,36 @@ public class EngineOperationServiceImpl implements EngineOperationService {
} }
} }
/**
*
* @param bytecode classmap
* @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 * @param e
@ -132,33 +156,52 @@ public class EngineOperationServiceImpl implements EngineOperationService {
String records = actionRecords.toString(); String records = actionRecords.toString();
//判断动作 //判断动作
if(e instanceof ActionReplace actionReplace){ if(e instanceof ActionReplace actionReplace){
String name = actionReplace.getName();
String newValue = actionReplace.getNewValue(); String newValue = actionReplace.getNewValue();
log.info("匹配到替换动作,新值是:{}",newValue); log.info("匹配到替换动作,新值是:{}",newValue);
//处理动作 //处理动作
actionReplace.getDataModel().setValue(newValue); actionReplace.replace();
//记录发生的动作 //记录发生的动作
if(!records.endsWith("[")){ if(!records.endsWith("[")){
if(!records.contains("替换")){ if(!records.contains(name)){
actionRecords.append("/替换"); actionRecords.append("/").append(name);
} }
}else { }else {
actionRecords.append("替换"); actionRecords.append(name);
} }
}else if (e instanceof ActionRemove actionRemove){ }else if (e instanceof ActionRemove actionRemove){
String name = actionRemove.getName();
log.info("匹配到移除动作"); log.info("匹配到移除动作");
actionRemove.getDataModel().setValue(null); actionRemove.remove();
if(!actionRecords.toString().endsWith("[")){ if(!records.endsWith("[")){
if(!records.contains("移除")){ if(!records.contains(name)){
actionRecords.append("/移除"); actionRecords.append("/").append(name);
} }
}else { }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 { }else {
log.info("测试失败,{}",e.getMessage()); String res = null;
//修改测试状态 if(Objects.isNull(versionId)){
remoteRuleEngineService.edit(versionId,"0"); res="执行";
throw new ServiceException("测试失败,"+e.getMessage(),Result.SUCCESS); }else {
res="测试";
//修改测试状态
remoteRuleEngineService.edit(versionId,"0");
}
log.info(res+"失败,{}",e.getMessage());
throw new ServiceException(res+"失败,"+e.getMessage(),Result.SUCCESS);
} }
} }
} }

View File

@ -1,11 +1,34 @@
package com.ruoyi.ruleEngine.client.engine.action; package com.ruoyi.ruleEngine.client.engine.action;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
/** /**
* *
* @ClassName ActionRecords * @ClassName ActionRecords
* @Author * @Author
* @Date 2024/5/13 22:31 * @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);
}
} }

View File

@ -13,6 +13,10 @@ import lombok.Setter;
@Setter @Setter
@Getter @Getter
public class ActionRemove extends RuntimeException{ public class ActionRemove extends RuntimeException{
/**
*
*/
private String name="移除";
/** /**
* *
*/ */

View File

@ -12,6 +12,10 @@ import lombok.*;
@Getter @Getter
@Setter @Setter
public class ActionReplace extends RuntimeException{ public class ActionReplace extends RuntimeException{
/**
*
*/
private String name="替换";
/** /**
* *
*/ */