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.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,20 +73,12 @@ 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());
//执行引擎
actionRecords=this.engineExecute(bytecode,testDataReq.getVersionClass(), testDataReq.getVersionId());
}catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | NullPointerException e) {
log.info("测试失败,{}",e.getMessage());
//修改测试状态
@ -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 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
@ -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 {
log.info("测试失败,{}",e.getMessage());
actionRecords.append(name);
}
}else {
String res = null;
if(Objects.isNull(versionId)){
res="执行";
}else {
res="测试";
//修改测试状态
remoteRuleEngineService.edit(versionId,"0");
throw new ServiceException("测试失败,"+e.getMessage(),Result.SUCCESS);
}
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;
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);
}
}

View File

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

View File

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